Dada.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace common\components\delivery\platform\dada;
  3. class Dada
  4. {
  5. // API 配置常量
  6. const API_VERSION = '1.0';
  7. const FORMAT = 'json';
  8. const RESPONSE_TYPE = 'code';
  9. // 测试环境和生产环境基础 URL
  10. const TEST_BASE_URL = 'https://newopen.qa.imdada.cn';
  11. const PROD_BASE_URL = 'https://newopen.imdada.cn';
  12. protected $baseUrl;
  13. protected $appKey;
  14. protected $appSecret;
  15. protected $sourceId;
  16. protected $isSandbox;
  17. /**
  18. * 初始化达达适配器
  19. * 根据运行环境加载对应的配置信息
  20. */
  21. public function __construct()
  22. {
  23. $isProduction = getenv('YII_ENV') == 'dev'; // TODO
  24. if ($isProduction) {
  25. // 生产环境配置(需要替换为实际的生产环境凭证)
  26. $this->baseUrl = self::PROD_BASE_URL;
  27. $this->appKey = 'dadaf2279d901a5ba40';
  28. $this->appSecret = '828a03677a1c00a3a5b3c59209c4433d';
  29. $this->sourceId = '499021274';
  30. } else {
  31. // 测试环境配置(需要替换为实际的测试环境凭证)
  32. $this->baseUrl = self::TEST_BASE_URL;
  33. $this->appKey = 'dadaf2279d901a5ba40';
  34. $this->appSecret = '828a03677a1c00a3a5b3c59209c4433d';
  35. $this->sourceId = '499021274';
  36. }
  37. $this->isSandbox = !$isProduction;
  38. }
  39. /**
  40. * 构建达达 API 请求参数
  41. *
  42. * 根据达达 API 规范构建完整的请求参数,包括签名
  43. * 参数说明详见接口协议文档
  44. *
  45. * @param string $apiMethod 接口方法名(不包括基础 URL)
  46. * @param string $body 业务参数 JSON 字符串
  47. * @return array 完整的 POST 请求参数
  48. */
  49. protected function buildRequestPayload($apiMethod, $body = '')
  50. {
  51. // 获取当前时间戳(单位:秒)
  52. $timestamp = (string)time();
  53. // 构建待签名的参数(不包括 signature)
  54. $params = [
  55. 'app_key' => $this->appKey,
  56. 'v' => self::API_VERSION,
  57. 'format' => self::FORMAT,
  58. 'source_id' => $this->sourceId,
  59. 'timestamp' => $timestamp,
  60. 'body' => $body,
  61. ];
  62. // 计算签名(按达达 API 规范)
  63. $signature = $this->generateSignature($params);
  64. // 加入签名到请求参数
  65. $params['signature'] = $signature;
  66. return $params;
  67. }
  68. /**
  69. * 生成达达 API 签名
  70. *
  71. * 达达签名算法:
  72. * 1. 将所有参数(除 signature 外)按 key 升序排列
  73. * 2. 拼接为 key1value1key2value2...keyNvalueN 的形式
  74. * 3. 在字符串后面加上 app_secret
  75. * 4. 对结果进行 MD5 加密,得到 32 位大写字符串
  76. *
  77. * @param array $params 待签名参数(不包括 signature)
  78. * @return string 签名值(32 位大写)
  79. */
  80. protected function generateSignature($params)
  81. {
  82. // 按 key 升序排列参数
  83. ksort($params);
  84. // 拼接参数字符串
  85. $str = '';
  86. foreach ($params as $key => $value) {
  87. // 跳过 null 值,但保留空字符串(如 body 为空时)
  88. if ($value === null) {
  89. continue;
  90. }
  91. $str .= $key . $value;
  92. }
  93. // 加上 app_secret
  94. $str = $this->appSecret . $str . $this->appSecret;
  95. // MD5 加密,返回 32 位大写字符串
  96. return strtoupper(md5($str));
  97. }
  98. }