Shunfeng.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace common\components\delivery\platform\shunfeng;
  3. use common\components\delivery\helpers\HttpClient;
  4. /**
  5. * Class Shunfeng 核心类,保存顺风同城平台的开发者信息
  6. * @package common\components\delivery\platform\shunfeng
  7. */
  8. class Shunfeng
  9. {
  10. protected $baseUrl;
  11. protected $devId;
  12. protected $appSecret;
  13. protected $shopId;
  14. protected $accessToken;
  15. protected $apiVersion = 19;
  16. public function __construct($accessToken = '', $shopId = 0)
  17. {
  18. // 根据环境设置基础URL
  19. if (getenv('YII_ENV') == 'production') { // TODO 先变更为测试 --- production / dev
  20. $this->baseUrl = 'https://openic.sf-express.com/open/api/external/';
  21. $this->devId = 1594082399;
  22. $this->appSecret = '9025c4f5ad93e8e08e947f02d0cde652';
  23. $this->shopId = $shopId;
  24. } else {
  25. $this->baseUrl = 'https://openic.sf-express.com/open/api/external/';
  26. $this->devId = 1741530942;
  27. $this->appSecret = '07b3f83d19a4a9f89322976c936faf92';
  28. $this->shopId = $shopId;
  29. }
  30. // 配置信息(从Auth类获取)
  31. $this->accessToken = $accessToken;
  32. }
  33. /**
  34. * 构建API请求参数
  35. *
  36. * 根据蜂鸟API规范构建完整的请求参数,包括签名
  37. *
  38. * @param string $method API方法名
  39. * @param array $businessData 业务参数
  40. * @return array 完整的请求参数
  41. */
  42. protected function httpRequest($method, $businessData = [])
  43. {
  44. // 构建参数
  45. $payload = [
  46. 'dev_id' => $this->devId,
  47. 'shop_id' => $this->shopId,
  48. 'shop_type' => 1, //1:顺丰店铺ID 2:接入方店铺ID
  49. ];
  50. $payload = array_merge($payload, $businessData);
  51. // 计算签名
  52. $signature = $this->generateSignature($payload);
  53. $url = $this->baseUrl . $method . '?sign=' . $signature;
  54. $resp = HttpClient::post($url, $payload, [
  55. 'Content-Type' => 'application/json',
  56. ]);
  57. return $resp;
  58. }
  59. public function generateSignature($params)
  60. {
  61. $post_data = json_encode($params);
  62. $sign_char = $post_data . "&{$this->devId}&{$this->appSecret}";
  63. $sign = base64_encode(MD5($sign_char)); // 注:md5出来的结果是32位小写16进制字符串,$sign 的最终结果末尾包含等号=
  64. return $sign;
  65. }
  66. }