Didi.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace common\components\delivery\platform\didi;
  3. class Didi
  4. {
  5. protected $baseUrl;
  6. protected $appKey; // URL参数中的appKey
  7. protected $appId; // Body参数中的appId
  8. protected $appSecret; // 签名用的secret
  9. protected $accessToken;
  10. protected $apiVersion = '1.0.0';
  11. protected $isSandbox;
  12. public function __construct($accessToken = '')
  13. {
  14. if (getenv('YII_ENV') == 'production') {
  15. $cfg = [
  16. 'base_url' => 'https://freight.xiaojukeji.com',
  17. 'app_key' => '6724e08ff1ac4afdbf1c000cfe722449',
  18. // TODO: 请填入正式环境的 appId 和 secret
  19. 'app_id' => '',
  20. 'secret' => '',
  21. ];
  22. $this->isSandbox = false;
  23. } else {
  24. $cfg = [
  25. 'base_url' => 'http://pinzhi.didichuxing.com/kop_osim',
  26. 'app_key' => '4329d266f40144829ca5fd47a025e106',
  27. // 请填入测试环境的 appId 和 secret
  28. 'app_id' => 'MDdovjIS',
  29. 'secret' => 'b61603625ebcf69a6364fdc30ae47944630c738c',
  30. ];
  31. $this->isSandbox = true;
  32. }
  33. $this->baseUrl = $cfg['base_url'];
  34. $this->appKey = $cfg['app_key'];
  35. $this->appId = $cfg['app_id'];
  36. $this->appSecret = $cfg['secret'];
  37. $this->accessToken = $accessToken;
  38. }
  39. /**
  40. * 构建API请求参数
  41. *
  42. * @param array $businessParams 业务参数
  43. * @return array
  44. */
  45. public function buildRequestPayload($businessParams = [])
  46. {
  47. // 1. 准备公共参数
  48. $timestamp = (int)(microtime(true) * 1000); // 毫秒级时间戳
  49. // 2. 处理业务参数 logisticsParam (JSON字符串)
  50. $logisticsParam = json_encode($businessParams, JSON_UNESCAPED_UNICODE); // 注意:根据文档,logisticsParam 是 json 字符串
  51. // 3. 生成签名
  52. $sign = $this->generateSignature($logisticsParam, $timestamp);
  53. // 4. 构建最终请求体
  54. $payload = [
  55. 'appId' => $this->appId,
  56. 'timestamp' => $timestamp,
  57. 'appSign' => $sign,
  58. 'logisticsParam' => $logisticsParam,
  59. ];
  60. // Token 可选,如果存在则添加
  61. if (!empty($this->accessToken)) {
  62. $payload['token'] = $this->accessToken;
  63. }
  64. // 默认自主模式 businessMode=0,如果有需要可扩展
  65. // $payload['businessMode'] = 0;
  66. return $payload;
  67. }
  68. /**
  69. * 生成签名
  70. * 签名规则:MD5(logisticsParam + secret + timestamp)
  71. *
  72. * @param string $logisticsParam json字符串
  73. * @param int $timestamp 毫秒时间戳
  74. * @return string
  75. */
  76. protected function generateSignature($logisticsParam, $timestamp)
  77. {
  78. $str = $logisticsParam . $this->appSecret . $timestamp;
  79. return md5($str);
  80. }
  81. /**
  82. * 获取完整的API请求URL
  83. *
  84. * @param string $apiMethod API名称
  85. * @return string
  86. */
  87. public function getApiUrl($apiMethod)
  88. {
  89. // url格式:{host}/gateway?api=xxx&apiVersion=1.0.0&appKey=xxx
  90. $queryParams = [
  91. 'api' => $apiMethod,
  92. 'apiVersion' => $this->apiVersion,
  93. 'appKey' => $this->appKey,
  94. ];
  95. return $this->baseUrl . '/gateway?' . http_build_query($queryParams);
  96. }
  97. public function getIsSandbox()
  98. {
  99. return $this->isSandbox;
  100. }
  101. }