Преглед изворни кода

跑腿新增:加小费功能

shizhongqi пре 8 месеци
родитељ
комит
089f8cbfcb

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

@@ -491,6 +491,7 @@ class DeliveryController extends BaseController
             $ghsOrder->sendDistance = 0;
             $ghsOrder->sendStatus = -4;
             $ghsOrder->deliveryId = '';
+            $ghsOrder->sendTip = 0;
             if(!$ghsOrder->save()) {
                 Yii::error('保存批发订单失败: ' . json_encode($ghsOrder->errors));
                 util::fail('保存批发订单失败');
@@ -529,6 +530,43 @@ class DeliveryController extends BaseController
         }
     }
 
+    /**
+     * 添加小费
+     */
+    public function actionAddTip()
+    {
+        $post = Yii::$app->request->post();
+        $ghsOrderId = $post['ghsOrderId'];
+        $platform = $post['platform'];
+        $tips = intval($post['tips']);
+
+        if($tips < 0) {
+            util::fail('小费金额至少为1元');
+        }
+        $tips *= 100;
+
+        $gdo = GhsDeliveryOrderClass::getByCondition(['mainId'=>$this->mainId, 'ghsOrderId'=>$ghsOrderId, 'orderStatus'=>['in',[0,1,2,10]]], true);
+        if($gdo == null){
+            util::fail('当前配送状态无法加小费');
+        }
+
+        $ds = new DispatchService($this->mainId, $platform);
+        $ret = $ds->addTip(['orderId'=>$gdo->orderId, 'tips'=>$tips]);
+        if($ret['code'] == 0){
+            $gdo->tip = $tips + $gdo->tip;
+            $gdo->save();
+
+            $orderObj = OrderClass::getById($gdo->ghsOrderId, true, 'id,sendTip');
+            $orderObj->sendTip = $tips/100 + $orderObj->sendTip;
+            $orderObj->save();
+
+            util::success($ret['data']);
+        }else{
+            Yii::error('获取订单取消原因:'.json_encode($ret));
+            util::fail($ret['msg']);
+        }
+    }
+
 
     // ---------------------------------------------------- 普通回调接口 ----------------------------------------------------------
     // 第三方回调入口

+ 9 - 0
common/components/delivery/services/DispatchService.php

@@ -88,6 +88,15 @@ class DispatchService
         return $adapter->createOrder($orderData);
     }
 
+    /**
+     *
+     */
+    public function addTip($data)
+    {
+        $adapter = $this->getAdapter();
+        return $adapter->addTip($data);
+    }
+
     /**
      * @param $orderId 平台订单号
      * @param string $thirdOrderNo xhGhsOrder.orderSn

+ 1 - 1
common/components/delivery/services/adapter/Adapter.php

@@ -8,6 +8,6 @@ interface Adapter
 {
     public function cityList();
     public function createOrder($order);
-    //public function selectOrder($data);
+    public function addTip($data);
     public function buildPriceRequest($data, $orderTime);
 }

+ 6 - 1
common/components/delivery/services/adapter/FengniaoAdapter.php

@@ -995,8 +995,13 @@ class FengniaoAdapter extends Fengniao implements Adapter
     public function addTip($data = [])
     {
         // 验证 order_id 和 partner_order_code 必填一个
-        $orderId = $data['order_id'] ?? null;
+        $orderId = $data['orderId'] ?? null;
         $partnerOrderCode = $data['partner_order_code'] ?? null;
+        $tips = $data['tips'] ?? null;
+
+        if($tips != null){
+            $data['add_tip_amount_cent'] = $tips;
+        }
         
         if (empty($orderId) && empty($partnerOrderCode)) {
             Yii::warning("[FengniaoAdapter] addTip failed: order_id and partner_order_code cannot both be empty");

+ 50 - 0
common/components/delivery/services/adapter/HuolalaAdapter.php

@@ -611,4 +611,54 @@ class HuolalaAdapter extends Huolala implements Adapter
             'data' => null
         ];
     }
+
+    public function addTip($data)
+    {
+        $orderDisplayId = $data['orderId'] ?? null;
+        $totalTipsFen = $data['tips'] ?? null;
+
+        if (empty($orderDisplayId)) {
+            return [
+                'code' => -1,
+                'platform' => 'huolala',
+                'msg' => 'order_display_id不能为空',
+                'data' => null,
+            ];
+        }
+
+        if (!isset($totalTipsFen) || (int)$totalTipsFen <= 0) {
+            return [
+                'code' => -1,
+                'platform' => 'huolala',
+                'msg' => 'total_tips_fen必须为正整数(单位:分)',
+                'data' => null,
+            ];
+        }
+
+        $apiData = [
+            'order_display_id' => $orderDisplayId,
+            'total_tips_fen' => (int)$totalTipsFen,
+        ];
+        $payload = $this->buildRequestPayload(
+            'u-order-addTips',
+            $apiData,
+            $this->accessToken
+        );
+        $resp = HttpClient::post($this->baseUrl, $payload, ['Content-type' => 'application/json']);
+
+        if (isset($resp['ret']) && $resp['ret'] == 0) {
+            return [
+                'code' => 0,
+                'platform' => 'huolala',
+                'data' => $resp['data'] ?? [],
+            ];
+        }
+
+        return [
+            'code' => $resp['ret'] ?? -1,
+            'platform' => 'huolala',
+            'msg' => $resp['msg'] ?? 'Unknown error',
+            'data' => null,
+        ];
+    }
 }

+ 64 - 0
common/components/delivery/services/adapter/ShansongAdapter.php

@@ -310,6 +310,70 @@ class ShansongAdapter extends Shansong implements Adapter
         return null;
     }
 
+    public function addTip($data)
+    {
+        $orderNo = $data['orderId'] ?? null;
+        $additionAmount = $data['tips'] ?? null;
+
+        if (empty($orderNo)) {
+            return [
+                'code' => -1,
+                'platform' => 'shansong',
+                'msg' => 'issOrderNo不能为空',
+                'data' => null,
+            ];
+        }
+
+        if (!isset($additionAmount) || (int)$additionAmount <= 0) {
+            return [
+                'code' => -1,
+                'platform' => 'shansong',
+                'msg' => 'additionAmount必须为正整数(单位:分)',
+                'data' => null,
+            ];
+        }
+
+        if (((int)$additionAmount) % 100 !== 0) {
+            return [
+                'code' => -1,
+                'platform' => 'shansong',
+                'msg' => 'additionAmount需被100整除(最小单位为1元)',
+                'data' => null,
+            ];
+        }
+
+        $requestData = json_encode([
+            'issOrderNo' => $orderNo,
+            'additionAmount' => (int)$additionAmount,
+        ], JSON_UNESCAPED_UNICODE);
+        $payload = [
+            'clientId' => $this->clientId,
+            'accessToken' => $this->accessToken,
+            'timestamp' => (int)(microtime(true) * 1000),
+            'data' => $requestData,
+        ];
+        $sign = SignHelper::makeSign($payload, $this->appSecret, 'md5', true, 'shansong');
+        $payload['sign'] = $sign;
+        $resp = HttpClient::post("{$this->baseUrl}/openapi/developer/v5/addition", $payload);
+
+        if (isset($resp['status']) && $resp['status'] == 200) {
+            return [
+                'code' => 0,
+                'platform' => 'shansong',
+                'data' => $resp['data'] ?? null,
+            ];
+        }
+
+        Yii::error('[ShansongAdapter] addTip failed: ' . json_encode($resp, JSON_UNESCAPED_UNICODE));
+
+        return [
+            'code' => $resp['status'] ?? -1,
+            'platform' => 'shansong',
+            'msg' => $resp['msg'] ?? 'Unknown error',
+            'data' => null,
+        ];
+    }
+
     /**
      * 递归过滤数组中的空值(null、空字符串、空数组)
      * 确保签名时不包含空值参数

+ 61 - 0
common/components/delivery/services/adapter/ShunfengAdapter.php

@@ -393,4 +393,65 @@ class ShunfengAdapter extends Shunfeng implements Adapter
             'timeout' => 10.0,
         ];
     }
+
+    public function addTip($data)
+    {
+        $orderId = $data['orderId'] ?? null;
+        $gratuityFee = $data['tips'] ?? null;
+
+        if (empty($orderId)) {
+            return [
+                'code' => -1,
+                'platform' => 'shunfeng',
+                'msg' => 'order_id不能为空',
+                'data' => null,
+            ];
+        }
+
+        if (!isset($gratuityFee) || (int)$gratuityFee < 100) {
+            return [
+                'code' => -1,
+                'platform' => 'shunfeng',
+                'msg' => 'gratuity_fee必须≥100(单位:分)',
+                'data' => null,
+            ];
+        }
+
+        $payload = [
+            'order_id' => $orderId,
+            'gratuity_fee' => (int)$gratuityFee,
+            'push_time' => isset($data['push_time']) ? (int)$data['push_time'] : time(),
+        ];
+        if (isset($data['order_type'])) {
+            $payload['order_type'] = (int)$data['order_type'];
+        }
+        if (!empty($data['shop_id'])) {
+            $payload['shop_id'] = $data['shop_id'];
+        }
+        if (isset($data['shop_type'])) {
+            $payload['shop_type'] = (int)$data['shop_type'];
+        }
+        if (!empty($data['serial_number'])) {
+            $payload['serial_number'] = (string)$data['serial_number'];
+        }
+        $resp = $this->httpRequest('addordergratuityfee', $payload);
+
+        if (isset($resp['error_code']) && (int)$resp['error_code'] === 0) {
+            $result = is_string($resp['result'] ?? null) ? json_decode($resp['result'], true) : ($resp['result'] ?? []);
+            return [
+                'code' => 0,
+                'platform' => 'shunfeng',
+                'data' => $result,
+            ];
+        }
+
+        Yii::error('[ShunfengAdapter] addTip failed: ' . json_encode($resp, JSON_UNESCAPED_UNICODE));
+
+        return [
+            'code' => (int)($resp['error_code'] ?? -1),
+            'platform' => 'shunfeng',
+            'msg' => $resp['error_msg'] ?? '添加小费失败',
+            'data' => null,
+        ];
+    }
 }