|
|
@@ -0,0 +1,132 @@
|
|
|
+<?php
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | likeshop100%开源免费商用商城系统
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
|
|
+// | 开源版本可自由商用,可去除界面版权logo
|
|
|
+// | 商业版本务必购买商业授权,以免引起法律纠纷
|
|
|
+// | 禁止对系统程序代码以任何目的,任何形式的再发布
|
|
|
+// | gitee下载:https://gitee.com/likeshop_gitee
|
|
|
+// | github下载:https://github.com/likeshop-github
|
|
|
+// | 访问官网:https://www.likeshop.cn
|
|
|
+// | 访问社区:https://home.likeshop.cn
|
|
|
+// | 访问手册:http://doc.likeshop.cn
|
|
|
+// | 微信公众号:likeshop技术社区
|
|
|
+// | likeshop团队 版权所有 拥有最终解释权
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+// | author: likeshopTeam
|
|
|
+// +----------------------------------------------------------------------
|
|
|
+
|
|
|
+namespace app\openapi\service;
|
|
|
+
|
|
|
+use think\facade\Cache;
|
|
|
+
|
|
|
+/**
|
|
|
+ * OpenAPI服务类
|
|
|
+ * Class OpenApiService
|
|
|
+ * @package app\openapi\service
|
|
|
+ */
|
|
|
+class OpenApiService
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 获取应用信息
|
|
|
+ * @param string $appId
|
|
|
+ * @return array|null
|
|
|
+ */
|
|
|
+ public static function getAppInfo(string $appId): ?array
|
|
|
+ {
|
|
|
+ // 这里可以从数据库或配置文件中获取应用信息
|
|
|
+ // 示例数据,实际应用中应该从数据库获取
|
|
|
+ $apps = [
|
|
|
+ '371083' => [
|
|
|
+ 'app_id' => 'rsgxsyt371083',
|
|
|
+ 'app_name' => '农发商城',
|
|
|
+ 'app_secret' => 'T3t6wnKJ6CBn7eaH',
|
|
|
+ 'status' => 1, // 1:启用 0:禁用
|
|
|
+ 'created_time' => time(),
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+
|
|
|
+ return $apps[$appId] ?? null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证签名
|
|
|
+ * @param array $params 请求参数
|
|
|
+ * @param string $appSecret 应用密钥
|
|
|
+ * @param int $timestamp 时间戳
|
|
|
+ * @param string $nonce 随机数
|
|
|
+ * @param string $signature 签名
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public static function verifySignature(array $params, string $appSecret, int $timestamp, string $nonce, string $signature): bool
|
|
|
+ {
|
|
|
+ // 构建签名字符串
|
|
|
+ $signString = self::buildSignString($params, $appSecret, $timestamp, $nonce);
|
|
|
+
|
|
|
+ // 计算签名
|
|
|
+ $expectedSignature = hash('sha256', $signString);
|
|
|
+ outFileLog($expectedSignature,'sign','$expectedSignature');
|
|
|
+ return hash_equals($expectedSignature, $signature);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建签名字符串
|
|
|
+ * @param array $params
|
|
|
+ * @param string $appSecret
|
|
|
+ * @param int $timestamp
|
|
|
+ * @param string $nonce
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ private static function buildSignString(array $params, string $appSecret, int $timestamp, string $nonce): string
|
|
|
+ {
|
|
|
+
|
|
|
+ // 移除签名相关参数
|
|
|
+ unset($params['signature']);
|
|
|
+
|
|
|
+ // 参数排序
|
|
|
+ ksort($params);
|
|
|
+
|
|
|
+ // 构建查询字符串
|
|
|
+ $queryString = http_build_query($params);
|
|
|
+
|
|
|
+ // 构建签名字符串: 参数字符串 + 时间戳 + 随机数 + 密钥
|
|
|
+ return $queryString . $timestamp . $nonce . $appSecret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证随机数(防重放攻击)
|
|
|
+ * @param string $appId
|
|
|
+ * @param string $nonce
|
|
|
+ * @param int $timestamp
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ public static function verifyNonce(string $appId, string $nonce, int $timestamp): bool
|
|
|
+ {
|
|
|
+ $cacheKey = 'openapi_nonce_' . $appId . '_' . $nonce;
|
|
|
+
|
|
|
+ // 检查随机数是否已使用
|
|
|
+ if (Cache::has($cacheKey)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 缓存随机数,过期时间为10分钟
|
|
|
+ Cache::set($cacheKey, $timestamp, 600);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成签名(供客户端使用)
|
|
|
+ * @param array $params
|
|
|
+ * @param string $appSecret
|
|
|
+ * @param int $timestamp
|
|
|
+ * @param string $nonce
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function generateSignature(array $params, string $appSecret, int $timestamp, string $nonce): string
|
|
|
+ {
|
|
|
+ $signString = self::buildSignString($params, $appSecret, $timestamp, $nonce);
|
|
|
+ return hash('sha256', $signString);
|
|
|
+ }
|
|
|
+}
|