| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- namespace common\components\delivery\platform\shunfeng;
- use bizGhs\express\classes\DeliveryAuthTokenClass;
- 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 $deliveryAuthToken; // xhDeliveryAuthToken 表数据
- protected $apiVersion = 19;
- // 自定义设置
- protected $isPersonDirect = 0; // is_person_direct -- 店铺是否支持专人直送,0不支持 1支持 2只能发独享专送
- public function __construct($deliveryAuthToken, $shopId = 0)
- {
- // 根据环境设置基础URL
- if (getenv('YII_ENV') == 'production') {
- $this->baseUrl = 'https://openic.sf-express.com/open/api/external/';
- $this->devId = 1594082399;
- $this->appSecret = '9025c4f5ad93e8e08e947f02d0cde652';
- } else {
- // $this->baseUrl = 'https://openic.sf-express.com/open/api/external/';
- // $this->devId = 1741530942;
- // $this->appSecret = '07b3f83d19a4a9f89322976c936faf92';
- $this->baseUrl = 'https://openic.sf-express.com/open/api/external/';
- $this->devId = 1594082399;
- $this->appSecret = '9025c4f5ad93e8e08e947f02d0cde652';
- }
- $this->shopId = $shopId;
- //保存 xhDeliveryAuthToken 表数据
- $this->deliveryAuthToken = $deliveryAuthToken;
- //自定义设置取出与设置
- $customSettingsStr = $deliveryAuthToken['customSettings'];
- $customSettings = json_decode($customSettingsStr, true);
- if(isset($customSettings['is_person_direct'])){
- $this->isPersonDirect = $customSettings['is_person_direct'];
- }
- }
- /**
- * 获取店铺信息
- *
- * 2025/2/17更新,返回参数更新,支持返回物流产品,详见logistic_type_list
- * 开发者可通过该接⼝获取到店铺的基础信息(非连锁店铺可以额外获得等级信息和补贴信息)
- *
- * @param string $shopId 店铺ID
- * @param int $shopType 店铺ID类型 1:顺丰店铺ID 2:接⼊⽅店铺ID
- * @return mixed
- */
- public function getShopInfo($shopId=0, $shopType = 1)
- {
- if($shopId == 0){
- $shopId = $this->shopId;
- }
- $params = [
- 'shop_id' => $shopId,
- 'shop_type' => $shopType,
- 'push_time' => time()
- ];
- $response = $this->httpRequest('getshopinfo', $params);
- // 检查API响应
- if (isset($response['error_code']) && $response['error_code'] == 0) {
- // 更新自定义中的json数据
- $delivery = DeliveryAuthTokenClass::getByCondition(['mainId'=>$this->deliveryAuthToken['mainId'], 'platform'=>'shunfeng'], true);
- $customSettingsJson = $delivery->customSettings;
- $customSettings = json_decode($customSettingsJson, true);
- $customSettings['is_person_direct'] = $response['result']['shop_info']['is_person_direct']; // 店铺是否支持专人直送,0不支持 1支持 2只能发独享专送
- $customSettingsJson = json_encode($customSettings);
- $delivery->customSettings = $customSettingsJson;
- $delivery->save();
-
- return [
- 'success' => true,
- 'shop_info' => $response['result']['shop_info'] ?? [],
- 'message' => '获取店铺信息成功',
- ];
- }
- return [
- 'success' => false,
- 'shop_info' => [],
- 'message' => $response['error_msg'] ?? '获取店铺信息失败',
- ];
- }
- public function getIsPersonDirect()
- {
- return $this->isPersonDirect;
- }
- /**
- * 构建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;
- }
- }
|