| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace common\components\delivery\platform\shunfeng;
- use common\components\delivery\helpers\HttpClient;
- /**
- * Class Shunfeng 核心类,保存顺风同城平台的开发者信息
- * @package common\components\delivery\platform\shunfeng
- */
- class Shunfeng
- {
- protected $baseUrl;
- protected $devId;
- protected $appSecret;
- protected $shopId;
- protected $accessToken;
- protected $apiVersion = 19;
- public function __construct($accessToken = '', $shopId = 0)
- {
- // 根据环境设置基础URL
- if (getenv('YII_ENV') == 'production') { // TODO 先变更为测试 --- production / dev
- $this->baseUrl = 'https://openic.sf-express.com/open/api/external/';
- $this->devId = 1594082399;
- $this->appSecret = '9025c4f5ad93e8e08e947f02d0cde652';
- $this->shopId = $shopId;
- } else {
- $this->baseUrl = 'https://openic.sf-express.com/open/api/external/';
- $this->devId = 1741530942;
- $this->appSecret = '07b3f83d19a4a9f89322976c936faf92';
- $this->shopId = $shopId;
- }
- // 配置信息(从Auth类获取)
- $this->accessToken = $accessToken;
- }
- /**
- * 构建API请求参数
- *
- * 根据蜂鸟API规范构建完整的请求参数,包括签名
- *
- * @param string $method API方法名
- * @param array $businessData 业务参数
- * @return array 完整的请求参数
- */
- protected function httpRequest($method, $businessData = [])
- {
- // 构建参数
- $payload = [
- 'dev_id' => $this->devId,
- 'shop_id' => $this->shopId,
- 'shop_type' => 1, //1:顺丰店铺ID 2:接入方店铺ID
- ];
- $payload = array_merge($payload, $businessData);
- // 计算签名
- $signature = $this->generateSignature($payload);
- $url = $this->baseUrl . $method . '?sign=' . $signature;
- $resp = HttpClient::post($url, $payload, [
- 'Content-Type' => 'application/json',
- ]);
- return $resp;
- }
- public function generateSignature($params)
- {
- $post_data = json_encode($params);
- $sign_char = $post_data . "&{$this->devId}&{$this->appSecret}";
- $sign = base64_encode(MD5($sign_char)); // 注:md5出来的结果是32位小写16进制字符串,$sign 的最终结果末尾包含等号=
- return $sign;
- }
- }
|