Sfoglia il codice sorgente

达达平台接入与代码修改

shizhongqi 9 mesi fa
parent
commit
477841f08d

+ 10 - 0
app-ghs/controllers/DeliveryController.php

@@ -770,6 +770,16 @@ class DeliveryController extends BaseController
         Yii::info('达达授权回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'dada');
     }
 
+    /**
+     * 达达回调接口
+     */
+    public function actionDadaCallback()
+    {
+        $post = Yii::$app->request->post();
+        Yii::info('达达回调:' . json_encode($post, JSON_UNESCAPED_UNICODE), 'dada');
+        return $this->asJson(['status' => 'ok']);
+    }
+
     /**
      * 顺丰授权与取消授权回调接口
      */

+ 106 - 2
common/components/delivery/platform/dada/Dada.php

@@ -1,10 +1,114 @@
 <?php
-
-
 namespace common\components\delivery\platform\dada;
 
 
 class Dada
 {
+    // API 配置常量
+    const API_VERSION = '1.0';
+    const FORMAT = 'json';
+    const RESPONSE_TYPE = 'code';
+
+    // 测试环境和生产环境基础 URL
+    const TEST_BASE_URL = 'https://newopen.qa.imdada.cn';
+    const PROD_BASE_URL = 'https://newopen.imdada.cn';
+
+    protected $baseUrl;
+    protected $appKey;
+    protected $appSecret;
+    protected $sourceId;
+    protected $isSandbox;
+
+    /**
+     * 初始化达达适配器
+     * 根据运行环境加载对应的配置信息
+     */
+    public function __construct()
+    {
+        $isProduction = getenv('YII_ENV') == 'production';
+
+        if ($isProduction) {
+            // 生产环境配置(需要替换为实际的生产环境凭证)
+            $this->baseUrl = self::PROD_BASE_URL;
+            $this->appKey = 'dadaf2279d901a5ba40';
+            $this->appSecret = '828a03677a1c00a3a5b3c59209c4433d';
+            $this->sourceId = '499021274';
+        } else {
+            // 测试环境配置(需要替换为实际的测试环境凭证)
+            $this->baseUrl = self::TEST_BASE_URL;
+            $this->appKey = 'dadaf2279d901a5ba40';
+            $this->appSecret = '828a03677a1c00a3a5b3c59209c4433d';
+            $this->sourceId = '499021274';
+        }
+
+        $this->isSandbox = !$isProduction;
+    }
+
+    /**
+     * 构建达达 API 请求参数
+     *
+     * 根据达达 API 规范构建完整的请求参数,包括签名
+     * 参数说明详见接口协议文档
+     *
+     * @param string $apiMethod 接口方法名(不包括基础 URL)
+     * @param string $body 业务参数 JSON 字符串
+     * @return array 完整的 POST 请求参数
+     */
+    protected function buildRequestPayload($apiMethod, $body = '')
+    {
+        // 获取当前时间戳(单位:秒)
+        $timestamp = (string)time();
+
+        // 构建待签名的参数(不包括 signature)
+        $params = [
+            'app_key' => $this->appKey,
+            'v' => self::API_VERSION,
+            'format' => self::FORMAT,
+            'source_id' => $this->sourceId,
+            'timestamp' => $timestamp,
+            'body' => $body,
+        ];
+
+        // 计算签名(按达达 API 规范)
+        $signature = $this->generateSignature($params);
+
+        // 加入签名到请求参数
+        $params['signature'] = $signature;
+
+        return $params;
+    }
+
+    /**
+     * 生成达达 API 签名
+     *
+     * 达达签名算法:
+     * 1. 将所有参数(除 signature 外)按 key 升序排列
+     * 2. 拼接为 key1value1key2value2...keyNvalueN 的形式
+     * 3. 在字符串后面加上 app_secret
+     * 4. 对结果进行 MD5 加密,得到 32 位大写字符串
+     *
+     * @param array $params 待签名参数(不包括 signature)
+     * @return string 签名值(32 位大写)
+     */
+    protected function generateSignature($params)
+    {
+        // 按 key 升序排列参数
+        ksort($params);
+
+        // 拼接参数字符串
+        $str = '';
+        foreach ($params as $key => $value) {
+            // 跳过空值
+            if ($value === '' || $value === null) {
+                continue;
+            }
+            $str .= $key . $value;
+        }
+
+        // 加上 app_secret
+        $str .= $this->appSecret;
 
+        // MD5 加密,返回 32 位大写字符串
+        return strtoupper(md5($str));
+    }
 }

+ 12 - 109
common/components/delivery/services/adapter/DadaAdapter.php

@@ -3,6 +3,7 @@ namespace common\components\delivery\services\adapter;
 
 use common\components\delivery\helpers\HttpClient;
 use common\components\delivery\helpers\SignHelper;
+use common\components\delivery\platform\dada\Dada;
 use Yii;
 
 /**
@@ -11,48 +12,8 @@ use Yii;
  * 基于达达开放平台 API 接口规范进行封装
  * API 文档参考:达达开放平台 - 接口调用协议与签名算法
  */
-class DadaAdapter implements Adapter
+class DadaAdapter extends Dada implements Adapter
 {
-    // API 配置常量
-    const API_VERSION = '1.0';
-    const FORMAT = 'json';
-    const RESPONSE_TYPE = 'code';
-    
-    // 测试环境和生产环境基础 URL
-    const TEST_BASE_URL = 'https://newopen.qa.imdada.cn';
-    const PROD_BASE_URL = 'https://newopen.imdada.cn';
-
-    protected $baseUrl;
-    protected $appKey;
-    protected $appSecret;
-    protected $sourceId;
-    protected $isSandbox;
-
-    /**
-     * 初始化达达适配器
-     * 根据运行环境加载对应的配置信息
-     */
-    public function __construct()
-    {
-        $isProduction = getenv('YII_ENV') == 'production';
-        
-        if ($isProduction) {
-            // 生产环境配置(需要替换为实际的生产环境凭证)
-            $this->baseUrl = self::PROD_BASE_URL;
-            $this->appKey = 'dadaf2279d901a5ba40';
-            $this->appSecret = '828a03677a1c00a3a5b3c59209c4433d';
-            $this->sourceId = '499021274';
-        } else {
-            // 测试环境配置(需要替换为实际的测试环境凭证)
-            $this->baseUrl = self::TEST_BASE_URL;
-            $this->appKey = 'dadaf2279d901a5ba40';
-            $this->appSecret = '828a03677a1c00a3a5b3c59209c4433d';
-            $this->sourceId = '499021274';
-        }
-        
-        $this->isSandbox = !$isProduction;
-    }
-
     /**
      * 获取已开通城市列表
      * 
@@ -263,74 +224,6 @@ class DadaAdapter implements Adapter
         ];
     }
 
-    /**
-     * 构建达达 API 请求参数
-     * 
-     * 根据达达 API 规范构建完整的请求参数,包括签名
-     * 参数说明详见接口协议文档
-     * 
-     * @param string $apiMethod 接口方法名(不包括基础 URL)
-     * @param string $body 业务参数 JSON 字符串
-     * @return array 完整的 POST 请求参数
-     */
-    protected function buildRequestPayload($apiMethod, $body = '')
-    {
-        // 获取当前时间戳(单位:秒)
-        $timestamp = (string)time();
-
-        // 构建待签名的参数(不包括 signature)
-        $params = [
-            'app_key' => $this->appKey,
-            'v' => self::API_VERSION,
-            'format' => self::FORMAT,
-            'source_id' => $this->sourceId,
-            'timestamp' => $timestamp,
-            'body' => $body,
-        ];
-
-        // 计算签名(按达达 API 规范)
-        $signature = $this->generateSignature($params);
-
-        // 加入签名到请求参数
-        $params['signature'] = $signature;
-
-        return $params;
-    }
-
-    /**
-     * 生成达达 API 签名
-     * 
-     * 达达签名算法:
-     * 1. 将所有参数(除 signature 外)按 key 升序排列
-     * 2. 拼接为 key1value1key2value2...keyNvalueN 的形式
-     * 3. 在字符串后面加上 app_secret
-     * 4. 对结果进行 MD5 加密,得到 32 位大写字符串
-     * 
-     * @param array $params 待签名参数(不包括 signature)
-     * @return string 签名值(32 位大写)
-     */
-    protected function generateSignature($params)
-    {
-        // 按 key 升序排列参数
-        ksort($params);
-
-        // 拼接参数字符串
-        $str = '';
-        foreach ($params as $key => $value) {
-            // 跳过空值
-            if ($value === '' || $value === null) {
-                continue;
-            }
-            $str .= $key . $value;
-        }
-
-        // 加上 app_secret
-        $str .= $this->appSecret;
-
-        // MD5 加密,返回 32 位大写字符串
-        return strtoupper(md5($str));
-    }
-
     /**
      * 生成随机数字符串(备用方法,如需要使用)
      * 
@@ -346,4 +239,14 @@ class DadaAdapter implements Adapter
         }
         return $str;
     }
+
+    public function addTip($data)
+    {
+        // TODO: Implement addTip() method.
+    }
+
+    public function buildPriceRequest($data, $orderTime)
+    {
+        // TODO: Implement buildPriceRequest() method.
+    }
 }