فهرست منبع

自定义运费计算

林琦海 5 سال پیش
والد
کامیت
4daf360e7d
2فایلهای تغییر یافته به همراه83 افزوده شده و 2 حذف شده
  1. 13 2
      app-hd/controllers/PurchaseController.php
  2. 70 0
      common/components/freight.php

+ 13 - 2
app-hd/controllers/PurchaseController.php

@@ -4,6 +4,7 @@ namespace hd\controllers;
 
 use biz\admin\classes\AdminClass;
 use biz\ghs\classes\GhsClass;
+use biz\shop\classes\ShopExpressClass;
 use bizGhs\custom\classes\CustomClass;
 use bizGhs\custom\services\CustomService;
 use bizGhs\express\services\DadaExpressServices;
@@ -14,6 +15,7 @@ use bizHd\purchase\services\PurchaseService;
 use bizGhs\product\classes\ProductClass;
 use bizHd\wx\classes\WxOpenClass;
 use common\components\dict;
+use common\components\freight;
 use common\components\httpUtil;
 use Yii;
 use common\components\util;
@@ -251,9 +253,11 @@ class PurchaseController extends BaseController
     public function actionFreight()
     {
         $post = Yii::$app->request->post();
-        $ghsId = $post['ghsId'] ?? 0;
+        //$ghsId = $post['ghsId'] ?? 0;
+        $ghsId = 31;
         //供货商
         $ghsInfo = GhsClass::getById($ghsId);
+
         if (empty($ghsInfo)) {
             util::fail('没有找到供货商');
         }
@@ -311,6 +315,14 @@ class PurchaseController extends BaseController
         if (empty($customInfo)) {
             util::fail('没有找到客户');
         }
+
+        //判断商家是否开启达达,若未开启,则使用自定义运费计算
+        $shopNo = ShopExpressClass::getShopNo($ghsSjId,$ghsShopId);
+        if(!$shopNo){
+            //使用自定义运费
+            $cost = freight::getCost($ghsInfo['lat'],$ghsInfo['long'],$customInfo['lat'],$customInfo['long'],$weight);
+            util::success(['sedCost' => $cost]);
+        }
         //组装订单信息
         $info = [];
         //发送放的商家信息,即供货商的相关信息
@@ -336,7 +348,6 @@ class PurchaseController extends BaseController
         $res = DadaExpressServices::queryDeliverFee($info);
         util::success(['sedCost' => $res['fee']]);
 
-        util::success(['sedCost' => 10]);
     }
 
 }

+ 70 - 0
common/components/freight.php

@@ -0,0 +1,70 @@
+<?php
+/**
+ * User: admin
+ * Date Time: 2021/5/21 20:50
+ */
+
+namespace common\components;
+
+//运费计算
+class freight
+{
+
+    //模仿达达运费计算
+    /**
+     * 0km < d <=2    8元
+     * 2km < d <=10  每新增1km加2
+     * 10km < d <=50  每新增5km 加6
+     *
+     * 重量
+     * 0 <w <=5 kg  不加价
+     * 2kg < 2 <= 10 kg  每增1 kg 加2
+     * 10 kg < w <= 50kg  每新增 5kg 加6
+    */
+    /**
+     * @param $startLat
+     * @param $starLng
+     * @param $endLat
+     * @param $endLng
+     * @param $weight 重量kg
+     */
+    public static function getCost($startLat,$starLng,$endLat,$endLng,$weight)
+    {
+        if($weight > 50 ){
+            util::fail("重量太重了哦!");
+        }
+        //后端计算距离 m
+        $phpStatDistance = util::getDistance($startLat, $starLng, $endLat, $endLng);
+        $phpStatDistance = $phpStatDistance/1000;
+        if($phpStatDistance > 50) {
+            util::fail("距离太远了哦!");
+        }
+        $cost = 8; //起步价
+        //距离计算
+        if($phpStatDistance > 10){
+            //每新增5km 加6
+            $diff = ceil(($phpStatDistance - 10) / 5);
+            $diffCost = $diff*6;
+            $cost += $diffCost;
+        }elseif ($phpStatDistance <=10 && $phpStatDistance > 2){
+            //每新增1km加2
+            $diff = ceil(($phpStatDistance - 2));
+            $diffCost = $diff*2;
+            $cost += $diffCost;
+        }
+        //重量计算
+        if($weight > 10){
+            //每新增 5kg 加6
+            $diff = ceil(($weight - 10) / 5);
+            $diffCost = $diff*6;
+            $cost += $diffCost;
+        }elseif ($weight <=10 && $weight > 2){
+            //每增1 kg 加2
+            $diff = ceil(($weight - 2));
+            $diffCost = $diff*2;
+            $cost += $diffCost;
+        }
+        return $cost;
+    }
+
+}