Просмотр исходного кода

货拉拉跑腿平台代码调整及优化

shizhongqi 2 месяцев назад
Родитель
Сommit
bd8ddea046

+ 7 - 5
app-ghs/controllers/DeliveryController.php

@@ -304,9 +304,9 @@ class DeliveryController extends BaseController
         $form->loadAndValidate();
 
         $post = $form->getAttributes();
-        $orderId = $post['orderId'] ?? 0; // xhGhsOrder.id
-        $params = $post['allParams'] ?? [];
-        $platform = $params['en_name'] ?? '';// 平台英文名,$post['platform'] 是中文
+        $orderId = $post['orderId']; // xhGhsOrder.id
+        $params = $post['allParams'];
+        $platform = $params['en_name'];// 平台英文名,$post['platform'] 是中文
         if (empty($platform)) {
             util::fail('请选择跑腿');
         }
@@ -316,7 +316,9 @@ class DeliveryController extends BaseController
         $params['ip'] = Yii::$app->request->userIP;
         $params['weight'] = $post['weight'];
         $params['remark'] = $post['remark'] ?? '';
-        $params['total_price_fen'] = $post['price'];
+        if (empty($params['total_price_fen'])) {
+            $params['total_price_fen'] = $post['price'];
+        }
         $params['pickupTime'] = $pickupTime['value'];
 
         $ghsOrder = OrderClass::getById($orderId, true);
@@ -1012,4 +1014,4 @@ class DeliveryController extends BaseController
             Yii::error('滴滴授权回调保存失败' . $e->getMessage(), 'didi');
         }
     }
-}
+}

+ 1 - 1
common/components/delivery/platform/huolala/Huolala.php

@@ -19,7 +19,7 @@ class Huolala
      */
     protected $notifyUrl;
 
-    const CITY_VEHICLE_CACHE_TTL = 259200; // 3 days
+    const CITY_VEHICLE_CACHE_TTL = 86400; // 1 day
     const CITY_VEHICLE_CACHE_KEY_PATTERN = 'huolala-city-vehicle:%d_%s';
 
     public function __construct($accessToken='')

+ 34 - 8
common/components/delivery/services/DispatchService.php

@@ -426,7 +426,7 @@ class DispatchService
             }
 
             $vehicleStd = [];
-            $vehicleStd[] = count($vehicle['vehicle_std_item']) > 0 ? $vehicle['vehicle_std_item'][0]['name'] : '';
+            $vehicleStd[] = count($vehicle['vehicle_std_item']) > 0 ? $vehicle['vehicle_std_item'][0]['name'] : "";
 
             $vehicleTypeList[$vehicle['order_vehicle_id']] = [
                 'order_vehicle_id' => $vehicle['order_vehicle_id'],
@@ -742,13 +742,13 @@ class DispatchService
                 $adapter = $this->adapters[$adapterKey];
                 
                 // 根据平台类型调用相应的 buildPriceRequest(s) 方法
-                if ($platform === 'huolala') {
-                    // 货拉拉返回多个请求(一个请求对应一个车型)
+                if ($platform === 'huolala') {// 货拉拉返回多个请求(一个请求对应一个车型)
                     $priceRequests = $adapter->buildPriceRequest($data, $orderTime);
                     foreach ($priceRequests as $requestKey => $requestInfo) {
                         $allRequests[$requestKey] = $requestInfo;
                         $platformMapping[$requestKey] = ['platform' => 'huolala', 'data' => $data];
                     }
+
                 } else {
                     // 其他平台各返回一个请求
                     $requestInfo = $adapter->buildPriceRequest($data, $orderTime);
@@ -1000,16 +1000,20 @@ class DispatchService
                             Yii::warning('货拉拉 报价数据 priceInfo 缺少 price_calculate_id:' . json_encode($priceInfo) . ' key: ' . $key);
                         }
                         //车型信息
-                        $vehicleAttr = $priceInfo['vehicle_info']['vehicle_attr'] ?? [];
-                        if(empty($vehicleAttr)){
+                        $vehicleAttr = $priceInfo['vehicle_info']['vehicle_attr'] ?? 0;
+                        if(!isset($priceInfo['vehicle_info']['vehicle_attr'])){
                             Yii::info('货拉拉 报价数据 vehicleAttr 为空:' . ' key: ' . $key); //json_encode($priceInfo) .
                         }
+                        $priceCondition = !empty($priceConditions) ? $priceConditions[0] : [];
+                        $priceInfoData = $priceCondition['price_info'] ?? [];
+                        $huolalaPrice = $priceInfoData['total_price'] ?? 0;
+                        $couponId = $this->extractHuolalaCouponId($priceConditions);
 
                         $deliveryList[] = [
                             'name' => '货拉拉' . ' (' . $vehicleType['_meta']['vehicle_type'] . ')',
                             'vehicle_type' => $vehicleType['_meta']['vehicle_type'],
                             'en_name' => 'huolala',
-                            'price' => !empty($priceConditions) ? $priceConditions[0]['price_info']['total_price'] : 0,
+                            'price' => $huolalaPrice,
                             'distance' => !empty($distanceInfo) ? $distanceInfo['distance_total'] : 0,
                             'type' => $vehicleType['_meta']['vehicle_type'],
                             'isAble' => true,
@@ -1020,11 +1024,17 @@ class DispatchService
                             // 下单需要透传的数据
                             'vehicle_std' => $vehicleType['vehicle_std'],
                             'spec_req' => $vehicleType['spec_req'],
+                            'addr_info' => $vehicleType['addr_info'],
+                            'order_time' => $vehicleType['order_time'],
+                            'total_price_fen' => $huolalaPrice,
                             'price_calculate_id' => $priceCalculateId,
-                            'price_item_encryption' => !empty($priceConditions) ? $priceConditions[0]['price_item_encryption'] : '',
+                            'price_item_encryption' => $priceCondition['price_item_encryption'] ?? '',
                             'vehicle_attr' => $vehicleAttr,
-                            'commodity_info' => !empty($priceConditions) ? $priceConditions[0]['commodity_item'] : [],
+                            'commodity_info' => $priceCondition['commodity_item'] ?? [],
                         ];
+                        if (!empty($couponId)) {
+                            $deliveryList[count($deliveryList) - 1]['coupon_id'] = $couponId;
+                        }
                     }
                     break;
                 case 'dada':
@@ -1078,4 +1088,20 @@ class DispatchService
 
         return $deliveryList;
     }
+
+    private function extractHuolalaCouponId($priceConditions)
+    {
+        if (empty($priceConditions) || !is_array($priceConditions)) {
+            return null;
+        }
+
+        foreach ($priceConditions as $condition) {
+            $couponId = $condition['coupon_info']['coupon_id'] ?? null;
+            if (!empty($couponId)) {
+                return $couponId;
+            }
+        }
+
+        return null;
+    }
 }

+ 46 - 38
common/components/delivery/services/adapter/HuolalaAdapter.php

@@ -61,44 +61,49 @@ class HuolalaAdapter extends Huolala implements Adapter
      */
     public function formatOrderData($order, $orderType, $shop, $params)
     {
-        //用车时间 -- 用车时间(unix时间戳:秒)。 取值范围:(当前时间+10分钟)<=用车时间<=(当前时间+5天)
-        $orderTime = strtotime($params['pickupTime']);
-        if($orderTime < (time()+600) ) {
-            $orderTime = time() + 600;
+        // 下单时必须优先使用估价时透传的时间,避免货拉拉核价失败。
+        $orderTime = $params['order_time'] ?? null;
+        if (empty($orderTime)) {
+            $orderTime = strtotime($params['pickupTime']);
+            if($orderTime < (time()+600) ) {
+                $orderTime = time() + 600;
+            }
         }
         $cityId = $params['city_id'];
+        // 下单时必须优先使用估价时透传的地址
+        $addrInfo = $params['addr_info'] ?? [
+             [
+                 'name' => $shop['merchantName'], //地址名称
+                 'addr' => $shop['fullAddress'],
+                 'lat_lon' => ['lat' => (float)$shop['lat'], 'lon' => (float)$shop['long']],
+                 'city_id' => $cityId,
+                 'city_name' => $shop['city'],
+                 'district_name' =>  $shop['dist'],
+                 'house_number' => $shop['floor'],
+                 'contacts_name' => $shop['mobile'],
+                 'contacts_phone_no' => $shop['mobile'],
+             ],
+             [
+                 'name' => empty($order['showAddress']) ? $order['customName'] : $order['showAddress'],
+                 'addr' => $order['fullAddress'],
+                 'lat_lon' => ['lat' => (float)$order['lat'], 'lon' => (float)$order['long']],
+                 'city_id' => $cityId,
+                 'city_name' => $order['city'],
+                 'district_name' => $order['dist'],
+                 'house_number' => $order['floor'],
+                 'contacts_name' => $order['customName'],
+                 'contacts_phone_no' => Functions::getMobile($order),
+             ]
+        ];
 
         $apiData = [
             'city_id' => $cityId,
             'city_info_revision' => $params['city_info_revision'],
             'order_vehicle_id' => $params['order_vehicle_id'],
-            'addr_info' => [
-                 [
-                     'name' => $shop['merchantName'], //地址名称
-                     'addr' => $shop['fullAddress'],
-                     'lat_lon' => ['lat' => (float)$shop['lat'], 'lon' => (float)$shop['long']],
-                     'city_id' => $cityId,
-                     'city_name' => $shop['city'],
-                     'district_name' =>  $shop['dist'],
-                     'house_number' => $shop['floor'],
-                     'contacts_name' => $shop['mobile'],
-                     'contacts_phone_no' => $shop['mobile'],
-                 ],
-                 [
-                     'name' => empty($order['showAddress']) ? $order['customName'] : $order['showAddress'],
-                     'addr' => $order['fullAddress'],
-                     'lat_lon' => ['lat' => (float)$order['lat'], 'lon' => (float)$order['long']],
-                     'city_id' => $cityId,
-                     'city_name' => $order['city'],
-                     'district_name' => $order['dist'],
-                     'house_number' => $order['floor'],
-                     'contacts_name' => $order['customName'],
-                     'contacts_phone_no' => Functions::getMobile($order),
-                 ]
-            ],
-            'vehicle_std' => $params['vehicle_std'],
+            'addr_info' => $addrInfo,
+            'vehicle_std' => $params['vehicle_std'] ?? [],
             'spec_req' => $params['spec_req'],
-            'order_time' => $orderTime,
+            'order_time' => (int)$orderTime,
             'contact_name' => $shop['merchantName'],
             'contact_phone_no' => $shop['mobile'],
             'out_user_id' => $params['mainId'], //下单人在服务商平台侧的唯一ID,如果没有,可以传0
@@ -111,6 +116,9 @@ class HuolalaAdapter extends Huolala implements Adapter
             'vehicle_attr' => $params['vehicle_attr'], //大小车属性,从计价接口返回中取值
             'commodity_info' => $params['commodity_info'] //商品信息(直接透传u-price-multiCalculate返回的commodity_item字段)
         ];
+        if (isset($params['coupon_id'])) {
+            $apiData['coupon_id'] = $params['coupon_id'];
+        }
 
         // 添加订单消息回调地址
         $apiData['notify_url'] = $this->notifyUrl;
@@ -153,24 +161,24 @@ class HuolalaAdapter extends Huolala implements Adapter
         $apiData = [
             'city_id' => (int)($order['city_id'] ?? 0),
             'city_info_revision' => (int)($order['city_info_revision'] ?? 0),
-            'order_vehicle_id' => (int)($order['order_vehicle_id'] ?? 0),
+            'order_vehicle_id' => (int)$order['order_vehicle_id'],
             'addr_info' => $addrInfo,
             'contact_name' => $order['contact_name'] ?? '',
             'contact_phone_no' => $order['contact_phone_no'] ?? '',
-            'total_price_fen' => (int)($order['total_price_fen'] ?? 0),
-            'pay_type' => (int)($order['pay_type'] ?? 0),
+            'total_price_fen' => (int)$order['total_price_fen'],
+            'pay_type' => $order['pay_type'],
             'price_calculate_id' => $order['price_calculate_id'] ?? '',
             'price_item_encryption' => $order['price_item_encryption'] ?? '',
             'vehicle_attr' => (int)($order['vehicle_attr'] ?? 0),
-            'order_time' => (int)($order['order_time'] ?? time()),
+            'order_time' => (int)$order['order_time'],
         ];
 
         // 可选参数
         if (!empty($order['vehicle_std'])) {
             $apiData['vehicle_std'] = is_array($order['vehicle_std']) ? $order['vehicle_std'] : [$order['vehicle_std']];
         }
-        if (!empty($order['spec_req'])) {
-            $apiData['spec_req'] = is_array($order['spec_req']) ? $order['spec_req'] : [$order['spec_req']];
+        if (!empty($order['spec_req']) && is_array($order['spec_req'])) {
+            $apiData['spec_req'] = array_map('intval', $order['spec_req']);
         }
         if (!empty($order['remark'])) {
             $apiData['remark'] = $order['remark'];
@@ -388,7 +396,7 @@ class HuolalaAdapter extends Huolala implements Adapter
             );
             
             // 使用车型ID作为请求的唯一标识
-            $vehicleId = (int)($vehicle['order_vehicle_id'] ?? 0);
+            $vehicleId = $vehicle['order_vehicle_id'];
             $requests["huolala_vehicle_{$vehicleId}"] = [
                 'url' => $this->baseUrl,
                 'data' => $payload,
@@ -495,4 +503,4 @@ class HuolalaAdapter extends Huolala implements Adapter
             'data' => null,
         ];
     }
-}
+}