Fengniao.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace common\components\delivery\platform\fengniao;
  3. use common\components\delivery\helpers\SignHelper;
  4. use Yii;
  5. class Fengniao
  6. {
  7. protected $baseUrl;
  8. protected $appId;
  9. protected $appSecret;
  10. protected $merchantId;
  11. protected $shopId;
  12. protected $accessToken;
  13. protected $apiVersion = '1.0';
  14. public function __construct($accessToken = '')
  15. {
  16. // 根据环境设置基础URL
  17. if (getenv('YII_ENV') == 'production') {
  18. $this->baseUrl = 'https://open-anubis.ele.me/anubis-webapi/v3/invoke';
  19. } else {
  20. $this->baseUrl = 'https://exam-anubis.ele.me/anubis-webapi/v3/invoke';
  21. }
  22. // 配置信息(从Auth类获取)
  23. $this->appId = '6587209115185920913';
  24. $this->appSecret = '3f935d5f-bf65-467e-a61c-72cfb1d53960';
  25. $this->accessToken = $accessToken;
  26. }
  27. /**
  28. * 设置商户ID
  29. *
  30. * @param string $merchantId 商户ID
  31. * @return $this
  32. */
  33. public function setMerchantId($merchantId)
  34. {
  35. $this->merchantId = $merchantId;
  36. return $this;
  37. }
  38. /**
  39. * 设置门店ID
  40. *
  41. * @param string $shopId 门店ID
  42. * @return $this
  43. */
  44. public function setShopId($shopId)
  45. {
  46. $this->shopId = $shopId;
  47. return $this;
  48. }
  49. /**
  50. * 获取门店ID
  51. *
  52. * @return int $shopId
  53. */
  54. public function getShopId()
  55. {
  56. return $this->shopId;
  57. }
  58. /**
  59. * 构建API请求参数
  60. *
  61. * 根据蜂鸟API规范构建完整的请求参数,包括签名
  62. *
  63. * @param string $method API方法名
  64. * @param array $businessData 业务参数
  65. * @return array 完整的请求参数
  66. */
  67. protected function buildRequestPayload($method, $businessData = [])
  68. {
  69. // 生成时间戳(毫秒)
  70. $timestamp = (string)(time() * 1000);
  71. // 构建参数
  72. $payload = [
  73. 'app_id' => $this->appId,
  74. 'timestamp' => $timestamp,
  75. 'merchant_id' => $this->merchantId,
  76. 'access_token' => $this->accessToken,
  77. 'business_data' => json_encode($businessData, JSON_UNESCAPED_UNICODE),
  78. 'version' => $this->apiVersion,
  79. ];
  80. // 计算签名
  81. $signature = $this->generateSignature($payload);
  82. $payload['signature'] = $signature;
  83. return $payload;
  84. }
  85. /**
  86. * 生成签名
  87. *
  88. * 根据蜂鸟API规范:
  89. * 签名计算:先按 key 排序,拼接成字符串,前面加上 appSecret,使用 SHA-256 加密
  90. *
  91. * @param array $params 待签名参数(不包括signature)
  92. * @return string 签名值
  93. */
  94. public function generateSignature(array $params)
  95. {
  96. // Step 1: 过滤空值 -- 不能清除,不然测试环境 code 就被排除了
  97. // $params = array_filter($params, function ($v) {
  98. // return $v !== null && $v !== '';
  99. // });
  100. // Step 2: 按字典序排序
  101. ksort($params);
  102. // Step 3: 拼接成字符串
  103. $paramStr = '';
  104. foreach ($params as $key => $value) {
  105. $paramStr .= "{$key}={$value}&";
  106. }
  107. $paramStr = rtrim($paramStr, '&');
  108. // Step 4: 拼接 appSecret
  109. $signBefore = $this->appSecret . $paramStr;
  110. // Step 5: 使用SHA-256加密
  111. $signature = hash('sha256', $signBefore);
  112. // Yii::info("[FengniaAuth] Sign Before: {$signBefore}, Signature: {$signature}");
  113. return $signature;
  114. }
  115. public function second_geneSignature(array $params)
  116. {
  117. $signature = strtolower(SignHelper::makeSign($params, $this->appSecret, 'sha256', true, 'fengniao'));
  118. return $signature;
  119. }
  120. }