OpenApiService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\openapi\service;
  20. use think\facade\Cache;
  21. /**
  22. * OpenAPI服务类
  23. * Class OpenApiService
  24. * @package app\openapi\service
  25. */
  26. class OpenApiService
  27. {
  28. /**
  29. * 获取应用信息
  30. * @param string $appId
  31. * @return array|null
  32. */
  33. public static function getAppInfo(string $appId): ?array
  34. {
  35. // 这里可以从数据库或配置文件中获取应用信息
  36. // 示例数据,实际应用中应该从数据库获取
  37. $apps = [
  38. '371083' => [
  39. 'app_id' => 'rsgxsyt371083',
  40. 'app_name' => '农发商城',
  41. 'app_secret' => 'T3t6wnKJ6CBn7eaH',
  42. 'status' => 1, // 1:启用 0:禁用
  43. 'created_time' => time(),
  44. ]
  45. ];
  46. return $apps[$appId] ?? null;
  47. }
  48. /**
  49. * 验证签名
  50. * @param array $params 请求参数
  51. * @param string $appSecret 应用密钥
  52. * @param int $timestamp 时间戳
  53. * @param string $nonce 随机数
  54. * @param string $signature 签名
  55. * @return bool
  56. */
  57. public static function verifySignature(array $params, string $appSecret, int $timestamp, string $nonce, string $signature): bool
  58. {
  59. // 构建签名字符串
  60. $signString = self::buildSignString($params, $appSecret, $timestamp, $nonce);
  61. // 计算签名
  62. $expectedSignature = hash('sha256', $signString);
  63. outFileLog($expectedSignature,'sign','$expectedSignature');
  64. return hash_equals($expectedSignature, $signature);
  65. }
  66. /**
  67. * 构建签名字符串
  68. * @param array $params
  69. * @param string $appSecret
  70. * @param int $timestamp
  71. * @param string $nonce
  72. * @return string
  73. */
  74. private static function buildSignString(array $params, string $appSecret, int $timestamp, string $nonce): string
  75. {
  76. // 移除签名相关参数
  77. unset($params['signature']);
  78. // 参数排序
  79. ksort($params);
  80. // 构建查询字符串
  81. $queryString = http_build_query($params);
  82. // 构建签名字符串: 参数字符串 + 时间戳 + 随机数 + 密钥
  83. return $queryString . $timestamp . $nonce . $appSecret;
  84. }
  85. /**
  86. * 验证随机数(防重放攻击)
  87. * @param string $appId
  88. * @param string $nonce
  89. * @param int $timestamp
  90. * @return bool
  91. */
  92. public static function verifyNonce(string $appId, string $nonce, int $timestamp): bool
  93. {
  94. $cacheKey = 'openapi_nonce_' . $appId . '_' . $nonce;
  95. // 检查随机数是否已使用
  96. if (Cache::has($cacheKey)) {
  97. return false;
  98. }
  99. // 缓存随机数,过期时间为10分钟
  100. Cache::set($cacheKey, $timestamp, 600);
  101. return true;
  102. }
  103. /**
  104. * 生成签名(供客户端使用)
  105. * @param array $params
  106. * @param string $appSecret
  107. * @param int $timestamp
  108. * @param string $nonce
  109. * @return string
  110. */
  111. public static function generateSignature(array $params, string $appSecret, int $timestamp, string $nonce): string
  112. {
  113. $signString = self::buildSignString($params, $appSecret, $timestamp, $nonce);
  114. return hash('sha256', $signString);
  115. }
  116. }