| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace common\components\delivery\platform\fengniao;
- use common\components\delivery\helpers\SignHelper;
- use Yii;
- class Fengniao
- {
- protected $baseUrl;
- protected $appId;
- protected $appSecret;
- protected $merchantId;
- protected $shopId;
- protected $accessToken;
- protected $apiVersion = '1.0';
- public function __construct($accessToken = '')
- {
- // 根据环境设置基础URL
- if (getenv('YII_ENV') == 'production') {
- $this->baseUrl = 'https://open-anubis.ele.me/anubis-webapi/v3/invoke';
- } else {
- $this->baseUrl = 'https://exam-anubis.ele.me/anubis-webapi/v3/invoke';
- }
- // 配置信息(从Auth类获取)
- $this->appId = '6587209115185920913';
- $this->appSecret = '3f935d5f-bf65-467e-a61c-72cfb1d53960';
- $this->accessToken = $accessToken;
- }
- /**
- * 设置商户ID
- *
- * @param string $merchantId 商户ID
- * @return $this
- */
- public function setMerchantId($merchantId)
- {
- $this->merchantId = $merchantId;
- return $this;
- }
- /**
- * 设置门店ID
- *
- * @param string $shopId 门店ID
- * @return $this
- */
- public function setShopId($shopId)
- {
- $this->shopId = $shopId;
- return $this;
- }
- /**
- * 获取门店ID
- *
- * @return int $shopId
- */
- public function getShopId()
- {
- return $this->shopId;
- }
- /**
- * 构建API请求参数
- *
- * 根据蜂鸟API规范构建完整的请求参数,包括签名
- *
- * @param string $method API方法名
- * @param array $businessData 业务参数
- * @return array 完整的请求参数
- */
- protected function buildRequestPayload($method, $businessData = [])
- {
- // 生成时间戳(毫秒)
- $timestamp = (string)(time() * 1000);
- // 构建参数
- $payload = [
- 'app_id' => $this->appId,
- 'timestamp' => $timestamp,
- 'merchant_id' => $this->merchantId,
- 'access_token' => $this->accessToken,
- 'business_data' => json_encode($businessData, JSON_UNESCAPED_UNICODE),
- 'version' => $this->apiVersion,
- ];
- // 计算签名
- $signature = $this->generateSignature($payload);
- $payload['signature'] = $signature;
- return $payload;
- }
- /**
- * 生成签名
- *
- * 根据蜂鸟API规范:
- * 签名计算:先按 key 排序,拼接成字符串,前面加上 appSecret,使用 SHA-256 加密
- *
- * @param array $params 待签名参数(不包括signature)
- * @return string 签名值
- */
- public function generateSignature(array $params)
- {
- // Step 1: 过滤空值 -- 不能清除,不然测试环境 code 就被排除了
- // $params = array_filter($params, function ($v) {
- // return $v !== null && $v !== '';
- // });
- // Step 2: 按字典序排序
- ksort($params);
- // Step 3: 拼接成字符串
- $paramStr = '';
- foreach ($params as $key => $value) {
- $paramStr .= "{$key}={$value}&";
- }
- $paramStr = rtrim($paramStr, '&');
- // Step 4: 拼接 appSecret
- $signBefore = $this->appSecret . $paramStr;
-
- // Step 5: 使用SHA-256加密
- $signature = hash('sha256', $signBefore);
- // Yii::info("[FengniaAuth] Sign Before: {$signBefore}, Signature: {$signature}");
- return $signature;
- }
- public function second_geneSignature(array $params)
- {
- $signature = strtolower(SignHelper::makeSign($params, $this->appSecret, 'sha256', true, 'fengniao'));
- return $signature;
- }
- }
|