Browse Source

蜂鸟订单回调

shizhongqi 8 months ago
parent
commit
2a1dd1abce

+ 16 - 13
app-ghs/controllers/DeliveryController.php

@@ -539,26 +539,29 @@ class DeliveryController extends BaseController
         }
         Yii::info('fengniao 回调 -- post: ' . json_encode($callbackData));
 
-        $get = Yii::$app->request->get();
-        Yii::info('fengniao 回调 -- get: ' . json_encode($get));
-
-        $fa = new \common\components\delivery\services\adapter\FengniaoAdapter();
-        $all_sign = $fa->second_geneSignature($callbackData);
-        $arr = json_decode($callbackData['business_data'], true);
-        $sec_sign = $fa->second_geneSignature($arr);
-        Yii::info('fengniao sign: ' . $all_sign . ', $sec_sign: ' . $sec_sign);
-
         // 逆向回调接口结果的加签
         $param = [
             'app_id' => $callbackData['app_id'],
             'timestamp' => $callbackData['timestamp'],
             'business_data' => $callbackData['business_data']
         ];
-        $paramSign_1 = $fa->second_geneSignature($param);
-        $paramSign_2 = $fa->generateSignature($param);
-        Yii::info('$paramSign_1 sign: ' . $paramSign_1 . ', $paramSign_2: ' . $paramSign_2);
 
-        util::complete('蜂鸟回调接口');
+        $fa = new \common\components\delivery\services\adapter\FengniaoAdapter();
+        $paramSign = $fa->generateSignature($param);
+        if($paramSign == $callbackData['signature']){
+            $businessData = isset($callbackData['business_data']) ? $callbackData['business_data'] : '';
+            if(empty($businessData)){
+                $this->asJson(['return_code' => -1, 'return_msg' => 'business data is empty']);
+            }
+            $businessData = json_decode($businessData, true);
+            $cbnObj = new \common\components\delivery\platform\fengniao\CallBackNotify();
+            $ret = $cbnObj->handle($businessData['callback_business_type'], $businessData['param']);
+
+            return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
+        }else{
+            Yii::error('蜂鸟回调接口签名出错');
+
+        }
     }
 
     /**

+ 56 - 0
common/components/delivery/platform/fengniao/CallBackNotify.php

@@ -0,0 +1,56 @@
+<?php
+namespace common\components\delivery\platform\fengniao;
+
+use bizGhs\express\classes\GhsDeliveryOrderClass;
+
+//回调业务类型
+const TYPE_ORDER_STATUS_NOTIFY = 'orderStatusNotify'; //订单状态回调
+const TYPE_ABNORMAL_REPORT_NOTIFY = 'abnormalReportNotify'; //异常报备回调
+const TYPE_COOKING_FINISH_NOTIFY = 'cookingFinishNotify'; //商户出餐回调
+const TYPE_CHAINSTORE_STATUS_NOTIFY = 'chainstoreStatusNotify'; //门店状态变更回调
+const TYPE_CHAINSTORE_SERVICE_STATUS_NOTIFY = 'chainstoreServiceStatusNotify'; //门店采购服务变更回调
+const TYPE_CHAINSTORE_BUSINESS_TIME_NOTIFY = 'chainstoreBusinessTimeNotify'; //门店营业时间变更回调 
+/**
+ * Class OrderStatusNotify
+ * @package common\components\delivery\platform\fengniao
+ * 各回调业务类型处理
+ */
+class CallBackNotify
+{
+    public function handle($type, $data)
+    {
+        switch ($type) {
+            case TYPE_ORDER_STATUS_NOTIFY:
+                return self::handleOrderStatusNotify($data);
+            case TYPE_ABNORMAL_REPORT_NOTIFY:
+                return self::handleAbnormalReportNotify($data);
+            case TYPE_COOKING_FINISH_NOTIFY:
+                return self::handleCookingFinishNotify($data);
+        }
+    }
+
+    private function handleOrderStatusNotify($data)
+    {
+        $order = GhsDeliveryOrderClass::getByCondition(['deliveryId'=>'fengniao', 'orderId'=>$data['order_id']], true);
+        // 将蜂鸟平台的订单状态转换为系统内的 orderStatus
+        $order->orderStatus = OrderStatusUtil::convertOrderStatus($data['order_status']);
+        $order->save();
+
+        return true;
+    }
+
+    private function handleAbnormalReportNotify($data)
+    {
+        return $data;
+    }
+
+    private function handleCookingFinishNotify($data)
+    {
+        return $data;
+    }
+
+    private function handleChainstoreStatusNotify($data)
+    {
+        return $data;
+    }
+}

+ 117 - 0
common/components/delivery/platform/fengniao/OrderStatusUtil.php

@@ -0,0 +1,117 @@
+<?php
+namespace common\components\delivery\platform\fengniao;
+
+/**
+ * Class FengniaoStatusUtil
+ * 蜂鸟配送平台状态转换工具类
+ * @package common\components\delivery\helpers
+ */
+class OrderStatusUtil
+{
+    /**
+     * 蜂鸟平台订单状态到系统 orderStatus 的转换映射
+     * 
+     * 蜂鸟平台状态码说明:
+     *  0 - 订单生成
+     *  1 - 运单生成成功
+     * 20 - 骑手接单
+     * 80 - 骑手到店
+     *  2 - 配送中
+     *  3 - 已完成
+     *  4 - 已取消
+     *  5 - 配送异常
+     * 
+     * 系统内 orderStatus 状态码说明:
+     *  0 - 订单生成
+     *  1 - 系统已接单
+     *  2 - 派单中
+     *  3 - 待取货
+     *  4 - 配送中
+     *  5 - 已送达
+     * -1 - 已取消
+     * -2 - 异常
+     * 10 - 改派中
+     * 20 - 已分配骑手
+     * 30 - 骑手已到店
+     * 40 - 申请取消中
+     * 50 - 客服介入处理中
+     */
+    const STATUS_MAPPING = [
+        0  => 0,   // 订单生成 -> 订单生成
+        1  => 1,   // 运单生成成功 -> 系统已接单
+        20 => 20,  // 骑手接单 -> 已分配骑手
+        80 => 30,  // 骑手到店 -> 骑手已到店
+        2  => 4,   // 配送中 -> 配送中
+        3  => 5,   // 已完成 -> 已送达
+        4  => -1,  // 已取消 -> 已取消
+        5  => -2,  // 配送异常 -> 异常
+    ];
+
+    /**
+     * 将蜂鸟平台的订单状态转换为系统内的 orderStatus
+     * 
+     * @param int $fengniaoStatus 蜂鸟平台的订单状态码
+     * @return int 转换后的系统 orderStatus 状态码
+     * @throws \Exception 当状态码无法转换时抛出异常
+     */
+    public static function convertOrderStatus($fengniaoStatus)
+    {
+        $fengniaoStatus = (int)$fengniaoStatus;
+        
+        if (!isset(self::STATUS_MAPPING[$fengniaoStatus])) {
+            throw new \Exception("无效的蜂鸟订单状态码:{$fengniaoStatus}");
+        }
+        
+        return self::STATUS_MAPPING[$fengniaoStatus];
+    }
+
+    /**
+     * 获取蜂鸟平台的状态描述
+     * 
+     * @param int $fengniaoStatus 蜂鸟平台的订单状态码
+     * @return string 状态描述
+     */
+    public static function getStatusDesc($fengniaoStatus)
+    {
+        $statusDescMap = [
+            0  => '订单生成',
+            1  => '运单生成成功',
+            20 => '骑手接单',
+            80 => '骑手到店',
+            2  => '配送中',
+            3  => '已完成',
+            4  => '已取消',
+            5  => '配送异常',
+        ];
+        
+        return $statusDescMap[$fengniaoStatus] ?? '未知状态';
+    }
+
+    /**
+     * 获取系统 orderStatus 的状态描述
+     * 
+     * @param int $orderStatus 系统的 orderStatus 状态码
+     * @return string 状态描述
+     */
+    public static function getOrderStatusDesc($orderStatus)
+    {
+        $statusDescMap = [
+            0   => '订单生成',
+            1   => '系统已接单',
+            2   => '派单中',
+            3   => '待取货',
+            4   => '配送中',
+            5   => '已送达',
+            -1  => '已取消',
+            -2  => '异常',
+            10  => '改派中',
+            20  => '已分配骑手',
+            30  => '骑手已到店',
+            40  => '申请取消中',
+            50  => '客服介入处理中',
+        ];
+        
+        return $statusDescMap[$orderStatus] ?? '未知状态';
+    }
+}
+