Kaynağa Gözat

运费 快递费的计算

shish 6 yıl önce
ebeveyn
işleme
771a2e42a9

+ 29 - 0
app/mall/controllers/ExpressController.php

@@ -0,0 +1,29 @@
+<?php
+
+namespace mall\controllers;
+
+use biz\merchant\services\ExpressService;
+use biz\merchant\services\ShopService;
+use Yii;
+use yii\web\Controller;
+use common\components\util;
+
+class ExpressController extends BaseController
+{
+
+	//计算客户距离和运费 shish 2020.5.14
+	public function actionGetUserDistance()
+	{
+		//经度
+		$lnt = Yii::$app->request->get('lnt', '');
+		//纬度
+		$lat = Yii::$app->request->get('lat', '');
+		if (empty($lnt) || empty($lat)) {
+			util::fail('经度或纬度不能为空');
+		}
+		$shopId = ShopService::getDefaultShopId($this->merchant);
+		$respond = ExpressService::getUserDistance($lnt, $lat, $shopId, $this->merchantExtend);
+		util::success($respond);
+	}
+
+}

+ 15 - 0
biz/merchant/classes/ExpressClass.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace biz\merchant\classes;
+
+use common\components\util;
+use common\components\wxUtil;
+use Yii;
+use biz\base\classes\BaseClass;
+
+class ExpressClass extends BaseClass
+{
+
+	public static $baseFile = '\biz\merchant\models\Express';
+
+}

+ 13 - 0
biz/merchant/models/Express.php

@@ -0,0 +1,13 @@
+<?php
+namespace biz\merchant\models;
+use biz\base\models\Base;
+
+class Express extends Base
+{
+
+	public static function tableName()
+	{
+		return 'xhExpress';
+	}
+
+}

+ 38 - 0
biz/merchant/services/ExpressService.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace biz\merchant\services;
+
+use biz\base\services\BaseService;
+use biz\merchant\classes\ShopClass;
+use common\components\mapUtil;
+use common\components\stringUtil;
+use common\components\util;
+use Yii;
+
+class ExpressService extends BaseService
+{
+	
+	public static $baseFile = '\biz\merchant\classes\ExpressClass';
+	
+	//计算客户的距离和运费 shish 2020.5.14
+	public static function getUserDistance($lnt, $lat, $shopId, $extend)
+	{
+		$shop = ShopClass::getById($shopId);
+		$shopLat = isset($shop['lat']) ? $shop['lat'] : '';
+		$shopLong = isset($shop['long']) ? $shop['long'] : '';
+		$distance = mapUtil::calculateDistance($shopLong, $shopLat, $lnt, $lat);
+		$perKiloFee = isset($extend['freight']) ? $extend['freight'] : 10;
+		$freeDistance = isset($extend['freeDistance']) ? $extend['freeDistance'] : 5;
+		$fee = 0;
+		if ($distance > $freeDistance * 1000) {
+			$fee = ceil(($distance - $freeDistance * 1000) / 1000) * $perKiloFee;
+		}
+		if ($distance > 1000) {
+			$showDistance = sprintf("%.1f", ($distance / 1000));
+		} else {
+			$showDistance = $distance . '米';
+		}
+		return ['distance' => $distance, 'showDistance' => $showDistance, 'fee' => $fee];
+	}
+
+}

+ 0 - 15
common/components/util.php

@@ -21,27 +21,12 @@ class util
     public static function getDistance($lat1, $lng1, $lat2, $lng2)
     {
         $earthRadius = 6367000; //approximate radius of earth in meters
-
-        /*
-         Convert these degrees to radians
-        to work with the formula
-        */
-
         $lat1 = ($lat1 * pi()) / 180;
         $lng1 = ($lng1 * pi()) / 180;
 
         $lat2 = ($lat2 * pi()) / 180;
         $lng2 = ($lng2 * pi()) / 180;
 
-        /*
-         Using the
-        Haversine formula
-
-        http://en.wikipedia.org/wiki/Haversine_formula
-
-        calculate the distance
-        */
-
         $calcLongitude = $lng2 - $lng1;
         $calcLatitude = $lat2 - $lat1;
         $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);

+ 8 - 0
console/controllers/UpgradeController.php

@@ -1434,6 +1434,14 @@ ALTER TABLE `xhAdmin` CHANGE `nickname` `nickName` VARCHAR(50) NOT NULL DEFAULT
 		$sql = "ALTER TABLE `xhUser` MODIFY `sourceType` TINYINT NOT NULL DEFAULT 0 COMMENT '来源 0微信 1支付宝 2小程序 3朋友圈 4美团 5系统,订单和客户来源id尽量统一';
 ALTER TABLE `xhOrder` MODIFY `sourceType` TINYINT NOT NULL DEFAULT 0 COMMENT '来源 0微信 1支付宝 2小程序 3朋友圈 4美团 5系统,订单和客户来源id尽量统一';";
 		Yii::$app->db->createCommand($sql)->execute($sql);
+		
+		$sql = "CREATE TABLE `xhExpress`(
+id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
+merchantId INT NOT NULL DEFAULT 0 COMMENT '商家id',
+`addTime` INT NOT NULL DEFAULT 0 COMMENT '添加时间'
+)COMMENT='商家的物流与配送信息';";
+		Yii::$app->db->createCommand($sql)->execute($sql);
+
 	}
 	
 	//数据迁移 shish 2020.1.12