Shunfeng.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace common\components\delivery\platform\shunfeng;
  3. use bizGhs\express\classes\DeliveryAuthTokenClass;
  4. use common\components\delivery\helpers\HttpClient;
  5. /**
  6. * Class Shunfeng 核心类,保存顺风同城平台的开发者信息
  7. * @package common\components\delivery\platform\shunfeng
  8. */
  9. class Shunfeng
  10. {
  11. protected $baseUrl;
  12. protected $devId;
  13. protected $appSecret;
  14. protected $shopId;
  15. protected $deliveryAuthToken; // xhDeliveryAuthToken 表数据
  16. protected $apiVersion = 19;
  17. // 自定义设置
  18. protected $isPersonDirect = 0; // is_person_direct -- 店铺是否支持专人直送,0不支持 1支持 2只能发独享专送
  19. public function __construct($deliveryAuthToken, $shopId = 0)
  20. {
  21. // 根据环境设置基础URL
  22. if (getenv('YII_ENV') == 'production') {
  23. $this->baseUrl = 'https://openic.sf-express.com/open/api/external/';
  24. $this->devId = 1594082399;
  25. $this->appSecret = '9025c4f5ad93e8e08e947f02d0cde652';
  26. } else {
  27. // $this->baseUrl = 'https://openic.sf-express.com/open/api/external/';
  28. // $this->devId = 1741530942;
  29. // $this->appSecret = '07b3f83d19a4a9f89322976c936faf92';
  30. $this->baseUrl = 'https://openic.sf-express.com/open/api/external/';
  31. $this->devId = 1594082399;
  32. $this->appSecret = '9025c4f5ad93e8e08e947f02d0cde652';
  33. }
  34. $this->shopId = $shopId;
  35. //保存 xhDeliveryAuthToken 表数据
  36. $this->deliveryAuthToken = $deliveryAuthToken;
  37. //自定义设置取出与设置
  38. $customSettingsStr = $deliveryAuthToken['customSettings'];
  39. $customSettings = json_decode($customSettingsStr, true);
  40. if(isset($customSettings['is_person_direct'])){
  41. $this->isPersonDirect = $customSettings['is_person_direct'];
  42. }
  43. }
  44. /**
  45. * 获取店铺信息
  46. *
  47. * 2025/2/17更新,返回参数更新,支持返回物流产品,详见logistic_type_list
  48. * 开发者可通过该接⼝获取到店铺的基础信息(非连锁店铺可以额外获得等级信息和补贴信息)
  49. *
  50. * @param string $shopId 店铺ID
  51. * @param int $shopType 店铺ID类型 1:顺丰店铺ID 2:接⼊⽅店铺ID
  52. * @return mixed
  53. */
  54. public function getShopInfo($shopId=0, $shopType = 1)
  55. {
  56. if($shopId == 0){
  57. $shopId = $this->shopId;
  58. }
  59. $params = [
  60. 'shop_id' => $shopId,
  61. 'shop_type' => $shopType,
  62. 'push_time' => time()
  63. ];
  64. $response = $this->httpRequest('getshopinfo', $params);
  65. // 检查API响应
  66. if (isset($response['error_code']) && $response['error_code'] == 0) {
  67. // 更新自定义中的json数据
  68. $delivery = DeliveryAuthTokenClass::getByCondition(['mainId'=>$this->deliveryAuthToken['mainId'], 'platform'=>'shunfeng'], true);
  69. $customSettingsJson = $delivery->customSettings;
  70. $customSettings = json_decode($customSettingsJson, true);
  71. $customSettings['is_person_direct'] = $response['result']['shop_info']['is_person_direct']; // 店铺是否支持专人直送,0不支持 1支持 2只能发独享专送
  72. $customSettingsJson = json_encode($customSettings);
  73. $delivery->customSettings = $customSettingsJson;
  74. $delivery->save();
  75. return [
  76. 'success' => true,
  77. 'shop_info' => $response['result']['shop_info'] ?? [],
  78. 'message' => '获取店铺信息成功',
  79. ];
  80. }
  81. return [
  82. 'success' => false,
  83. 'shop_info' => [],
  84. 'message' => $response['error_msg'] ?? '获取店铺信息失败',
  85. ];
  86. }
  87. public function getIsPersonDirect()
  88. {
  89. return $this->isPersonDirect;
  90. }
  91. /**
  92. * 构建API请求参数
  93. *
  94. * 根据API规范构建完整的请求参数,包括签名
  95. *
  96. * @param string $method API方法名
  97. * @param array $businessData 业务参数
  98. * @return array 完整的请求参数
  99. */
  100. protected function httpRequest($method, $businessData = [])
  101. {
  102. // 构建参数
  103. $payload = [
  104. 'dev_id' => $this->devId,
  105. 'shop_id' => $this->shopId,
  106. 'shop_type' => 1, //1:顺丰店铺ID 2:接入方店铺ID
  107. ];
  108. $payload = array_merge($payload, $businessData);
  109. // 计算签名
  110. $signature = $this->generateSignature($payload);
  111. $url = $this->baseUrl . $method . '?sign=' . $signature;
  112. $resp = HttpClient::post($url, $payload, [
  113. 'Content-Type' => 'application/json',
  114. ]);
  115. return $resp;
  116. }
  117. public function generateSignature($params)
  118. {
  119. $post_data = json_encode($params);
  120. $sign_char = $post_data . "&{$this->devId}&{$this->appSecret}";
  121. $sign = base64_encode(MD5($sign_char)); // 注:md5出来的结果是32位小写16进制字符串,$sign 的最终结果末尾包含等号=
  122. return $sign;
  123. }
  124. }