shish il y a 3 ans
Parent
commit
6bb6af70c9
2 fichiers modifiés avec 104 ajouts et 34 suppressions
  1. 23 34
      app-mall/controllers/OrderController.php
  2. 81 0
      biz-mall/order/classes/OrderClass.php

+ 23 - 34
app-mall/controllers/OrderController.php

@@ -26,44 +26,33 @@ use common\components\util;
 class OrderController extends BaseController
 {
 
-    //计算运费 ssh 20221003
+    //计算运费,请搜索关键词calc_freight,二个方法要合一起 ssh 20221014
     public function actionFreight()
     {
         $get = Yii::$app->request->get();
 
-        util::success(['distance' => 1, 'showDistance' => 1, 'fee' => 1]);
-
-//        $shop = $this->shop;
-//        $shopLat = isset($shop->lat) ? $shop->lat : '';
-//        $shopLong = isset($shop->long) ? $shop->long : '';
-//
-//        if (empty($shopLat) || empty($shopLong)) {
-//            util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
-//        }
-//
-//        $ghsId = $get['ghsId'] ?? 0;
-//        $ghs = GhsClass::getById($ghsId, true);
-//        if (empty($ghs)) {
-//            util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
-//        }
-//        $ghsShopId = $ghs->shopId ?? 0;
-//        $ghsShop = ShopClass::getById($ghsShopId, true);
-//        if (empty($ghsShop)) {
-//            util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
-//        }
-//
-//        $ghsShopLat = isset($ghsShop->lat) ? $ghsShop->lat : '';
-//        $ghsShopLong = isset($ghsShop->long) ? $ghsShop->long : '';
-//        if (empty($ghsShopLat) || empty($ghsShopLong)) {
-//            util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
-//        }
-//
-//        $weight = $get['weight'] ?? 0;
-//        if (empty($weight)) {
-//            util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
-//        }
-//        $respond = PurchaseClass::getDistanceFee($this->shop, $ghsShop, $weight);
-//        util::success($respond);
+        $shop = $this->shop;
+        $shopLat = isset($shop->lat) ? $shop->lat : '';
+        $shopLong = isset($shop->long) ? $shop->long : '';
+
+        if (empty($shopLat) || empty($shopLong)) {
+            util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
+        }
+
+        $user = $this->user;
+        $userLat = $user->lat ?? '';
+        $userLong = $user->long ?? '';
+        if (empty($userLat) || empty($userLong)) {
+            util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
+        }
+
+        $weight = $get['weight'] ?? 0;
+        if (empty($weight)) {
+            util::success(['distance' => 0, 'showDistance' => 0, 'fee' => 0]);
+        }
+
+        $respond = OrderClass::getDistanceFee($user, $shop, $weight);
+        util::success($respond);
     }
 
     //购买花材 ssh 20220511

+ 81 - 0
biz-mall/order/classes/OrderClass.php

@@ -10,7 +10,9 @@ use bizMall\shop\classes\ShopClass;
 use bizMall\stat\classes\StatOrderClass;
 use bizMall\user\classes\UserClass;
 use common\components\imgUtil;
+use common\components\mapUtil;
 use common\components\miniUtil;
+use common\components\noticeUtil;
 use common\components\orderSn;
 use bizMall\base\classes\BaseClass;
 use common\components\util;
@@ -56,6 +58,85 @@ class OrderClass extends BaseClass
         20000 => [90, 100],
     ];
 
+    //计算运费,请搜索关键词calc_freight,二个方法要合一起 ssh 20221014
+    public static function getDistanceFee($user, $shop, $weight)
+    {
+        $userLat = isset($user->lat) ? $user->lat : '';
+        $userLong = isset($user->long) ? $user->long : '';
+
+        if (empty($userLat) || empty($userLong)) {
+            util::fail('请完善门店地址');
+        }
+
+        $shopLat = isset($shop->lat) ? $shop->lat : '';
+        $shopLong = isset($shop->long) ? $shop->long : '';
+        if (empty($shopLat) || empty($shopLong)) {
+            util::fail('商家地址有问题');
+        }
+
+        //基础配送费8元,0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
+        //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
+        //00:00 - 06:00 +6元,22:00-24:00 +4元
+        //恶劣天气,下单高峰期会临时调整价格
+
+        //起步价
+        $baseFee = 8;
+        $distance = mapUtil::calculateDistance($userLong, $userLat, $shopLong, $shopLat);
+
+        //0-5公里,每公里+1元,5-100公里,6元起,每公里+2元
+        $formatDistance = ceil($distance / 1000);
+        $startKiloFee = 6;
+        if ($formatDistance < 5) {
+            $addDistanceFee = bcmul($formatDistance, 1);
+        } elseif ($formatDistance == 5) {
+            $addDistanceFee = $startKiloFee;
+        } else {
+            $overKilo = bcsub($formatDistance, 5);
+            $overFee = bcmul($overKilo, 2);
+            $addDistanceFee = bcadd($startKiloFee, $overFee);
+        }
+
+        //5-10公斤,2元起,每公斤+2元,10-15公斤,+12元,超过15公斤,+20元
+        $formatWeight = ceil($weight);
+        if ($formatWeight > 15) {
+            $addWeightFee = 20;
+        } elseif ($formatWeight >= 10 && $formatWeight <= 15) {
+            $addWeightFee = 12;
+        } elseif ($formatWeight >= 5 && $formatWeight < 10) {
+            $overWeight = bcsub($formatWeight, 4);
+            $addWeightFee = bcmul($overWeight, 2);
+        } else {
+            $addWeightFee = 0;
+        }
+
+        $addFee = bcadd($addDistanceFee, $addWeightFee, 2);
+        $totalFee = bcadd($baseFee, $addFee, 2);
+
+        //特殊时段费
+        $timeFee = 0;
+        $hour = date("G");
+        //22:00 - 24:00 +4元
+        if (in_array($hour, [22, 23])) {
+            $timeFee = 4;
+        }
+        //00:00 - 06:00 +6元
+        if (in_array($hour, [0, 1, 2, 3, 4, 5])) {
+            $timeFee = 6;
+        }
+        $totalFee = bcadd($totalFee, $timeFee, 2);
+
+        //恶劣天气和下单高峰费用
+        $weatherFee = 0;
+        $totalFee = bcadd($totalFee, $weatherFee, 2);
+
+        $showDistance = sprintf("%.1f", ($distance / 1000));
+
+        noticeUtil::push("零售商城 距离:{$distance} {$showDistance} 重量:{$weight} 费用:{$totalFee}", '15280215347');
+
+        return ['distance' => $distance, 'showDistance' => $showDistance, 'fee' => $totalFee];
+    }
+
+
     //添加订单 ssh 2019.12.5
     public static function addOrder($data)
     {