Explorar o código

订单相关处理

shish %!s(int64=6) %!d(string=hai) anos
pai
achega
f6f1fc0590

+ 41 - 79
app/client/controllers/OrderController.php

@@ -4,6 +4,7 @@ namespace client\controllers;
 
 use biz\goods\services\CategoryService;
 use biz\goods\services\GoodsCategoryService;
+use biz\goods\services\GoodsService;
 use biz\merchant\services\MerchantService;
 use biz\merchant\services\ShopService;
 use biz\order\services\OrderService;
@@ -18,101 +19,62 @@ class OrderController extends BaseController
 	//商城下单操作 shish 2019.12.3
 	public function actionCreateOrder()
 	{
+		//给出省市区的接口 todo
+		//给出商家门店地址接口 todo
+		//给出腾讯的接口 todo
+
 		$post = Yii::$app->request->post();
-		$needSend = isset($post['needSend']) ? $post['needSend'] : 0;
+		$goodsId = isset($post['goodsId']) ? $post['goodsId'] : 0;
+		if (empty($goodsId)) {
+			util::fail('请选择商品');
+		}
+		$goodsInfo = GoodsService::getById($goodsId);
+		if (empty($goodsInfo)) {
+			util::fail('没有找到商品');
+		}
+		//确认是否要配送的商品
+		$needSend = isset($goodsInfo['needSend']) && $goodsInfo['needSend'] == 1 ? 1 : 0;
+		
 		$userId = $this->userId;
 		$post['userId'] = $userId;
 		$post['payWay'] = $this->isWeixin == true ? 0 : 1;
-		$lat = $post['receiveLat'];//收货人纬度
-		$lng = $post['receiveLong'];//收货人经度
 		$defaultShopId = MerchantService::getDefaultShopId($this->merchant);
 		if (empty($defaultShopId)) {
 			util::fail('没有找到门店');
 		}
 		$shopInfo = ShopService::getById($defaultShopId);
 		
-		$calcDistance = ShopService::calcDistance($lat, $lng, $shopInfo);
-		
-		$couponAmount = 0;
-		$couponId = isset($post['couponId']) ? $post['couponId'] : 0;//代金劵
-		if (!empty($couponId)) {
-			$coupon = xhCouponService::getById($couponId);
-			$couponUserId = $coupon['userId'];
-			if ($couponUserId != $userId) {
-				util::fail('不是你的代金劵');
-			}
-			if ($coupon['useStatus'] == 1 || $coupon['deadline'] < time()) {
-				util::fail('代金劵已经失效了');
-			}
-			$couponAmount = $coupon['amount'];
+		//计算运费
+		$post['sendDistance'] = isset($post['sendDistance']) ? $post['sendDistance'] : 0;
+		$post['sendCost'] = 0;
+		if ($needSend == 1) {
+			$lat = $post['receiveLat'];//收货人纬度
+			$lng = $post['receiveLong'];//收货人经度
+			$jsStatDistance = $post['sendDistance'];
+			$sendCost = ShopService::getSendCost($lat, $lng, $jsStatDistance, $shopInfo);
+			$post['sendCost'] = $sendCost;
 		}
-		//不要将代金劵保存到订单表,付款成功后再保存进去!!!
+		//付款成功才保存优惠券id
 		unset($post['couponId']);
 		$post['merchantId'] = $this->merchantId;
 		$post['shopId'] = $defaultShopId;
 		$order = OrderService::add($post);
-
 		$orderId = $order['id'];
-		$oData = [];
-		$goodsInfo = $post['goodsInfo'];
-		$totalFee = 0;//计算总价格
-		$goodsIdList = [];
-		$multiPriceIdList = [];
-		$orderName = '';
-		$goodsNum = 0;
-		foreach ($goodsInfo as $key => $val) {
-			$multiPriceId = 0;//多种价格表Id
-			$goodsId = $val['goodsId'];
-			$num = $val['num'];
-			$goodsNum += $num;
-			$goods = xhGoodsService::getById($goodsId);
-			if (empty($orderName)) {
-				$orderName = $goods['goodsName'];
-			}
-			$price = $goods['price'];
-			if ($val['multiPriceId'] != 0 && $goods['multiPrice'] != '') {
-				$pos = strpos($goods['multiPrice'], $val['multiPriceId']);
-				if ($pos === false) {
-					util::fail('订单的商品不存在');
-				}
-				$goodPrice = xhGoodsPriceService::getById($val['multiPriceId']);
-				//统一对商品进行价格等方面设置
-				$price = xhGoodsSettingService::changePrice($goods, $goodPrice['price']);
-				//多种价格规格 替换 默认(单价格)
-				$goods['goodsName'] = $goodPrice['title'];
-				$multiPriceId = $val['multiPriceId'];
-			}
-			$totalFee += $price * $num;
-			$goodsIdList[] = $goodsId;
-			$multiPriceIdList[] = $multiPriceId;
-			$data = [
-				'orderId' => $orderId,
-				'goodsId' => $goodsId,
-				'userId' => $userId,
-				'merchantId' => $this->merchantId,
-				'title' => $goods['goodsName'],
-				'cover' => $goods['cover'],
-				'unitPrice' => $price,
-				'num' => $num,
-				'multiPriceId' => $multiPriceId,
-				'createTime' => date("Y-m-d H:i:s"),
-			];
-			xhOrderGoodsService::add($data);//创建订单商品列表
-		}
-		$sendCost = $post['sendCost'];//运费
-		$oData['goodsNum'] = $goodsNum;//订单的商品数量类型,单个还是多个的
-		$oData['prePrice'] = $totalFee + $sendCost;
-		$oData['orderName'] = $goodsNum > 1 ? $orderName . '等' . $goodsNum . '件商品' : $orderName;
-		$userAsset = xhUserAssetService::getByUserId($userId);
-		$level = $userAsset['memberLevel'];
-		if ($level > 0) {
-			$gradeList = xhMerchantExtendService::getGradeList($this->merchantExtend);
-			$discount = $gradeList[$level]['discount'];
-			$discount = strlen($discount) == 1 ? $discount * 10 : $discount;
-			$totalFee = number_format($discount * $totalFee / 100, 2);
-		}
-		$oData['actPrice'] = $totalFee + $sendCost - $couponAmount;
-		xhOrderService::updateById($orderId, $oData);
+
+		$data = [
+			'orderId' => $orderId,
+			'goodsId' => $goodsId,
+			'userId' => $userId,
+			'merchantId' => $this->merchantId,
+			'title' => $goods['goodsName'],
+			'cover' => $goods['cover'],
+			'unitPrice' => $price,
+			'num' => $num,
+			'multiPriceId' => $multiPriceId,
+			'createTime' => date("Y-m-d H:i:s"),
+		];
+		xhOrderGoodsService::add($data);//创建订单商品列表
+
 		util::success(['orderId' => $orderId, 'couponId' => $couponId]);
 	}
 	

+ 14 - 0
biz/merchant/services/MerchantExtendService.php

@@ -5,6 +5,7 @@ namespace biz\merchant\services;
 use biz\base\services\BaseService;
 use biz\merchant\classes\MerchantExtendClass;
 use common\components\arrayUtil;
+use common\components\util;
 use Yii;
 
 class MerchantExtendService extends BaseService
@@ -37,5 +38,18 @@ class MerchantExtendService extends BaseService
 		$data = arrayUtil::sort($data, 'level', $order);
 		return $data;
 	}
+
+	//运费计算方式
+	public static function getFreight($ext)
+	{
+		$freight = isset($ext['freight']) ? $ext['freight'] : '5_2';
+		$arr = explode('_', $freight);
+		return [
+			//首公里
+			'first' => $arr[0],
+			//通过首公里每增加一公里增加运费
+			'add' => $arr[1]
+		];
+	}
 	
 }

+ 22 - 25
biz/merchant/services/ShopService.php

@@ -3,38 +3,35 @@
 namespace biz\merchant\services;
 
 use biz\base\services\BaseService;
+use common\components\stringUtil;
 use common\components\util;
 use Yii;
 
 class ShopService extends BaseService
 {
-
+	
 	public static $baseFile = '\biz\merchant\classes\ShopClass';
-
-	//后端计算订单距离 shish 2019.12.4
-	public static function calcDistance($lat, $lng, $shopInfo)
+	
+	//计算运费 shish 2019.12.4
+	public static function getSendCost($lat, $lng, $jsStatDistance, $shopInfo)
 	{
-		
-		$post['sendDistance'] = $post['sendDistance'] >= $calcDistance ? $post['sendDistance'] : $calcDistance;
-		$sendDistance = $post['sendDistance'];
-		$freight = configDict::getConfig('freight');
-		$firstDistance = $freight['firstDistance'];
-		$firstPrice = $freight['firstPrice'];
-		$nextDistance = $freight['nextDistance'];
-		$nextPrice = $freight['nextPrice'];
-		if ($sendDistance <= $firstDistance && $post['isCharge'] == 1) {
-			$post['sendCost'] = $firstPrice;
-		} else if ($post['isCharge'] == 1) {
-			$subDistance = $sendDistance - $firstDistance;
-			$addPrice = intval($subDistance / $nextDistance) * $nextPrice;
-			$post['sendCost'] = $firstPrice + $addPrice;
+		//门店纬度
+		$shopLat = $shopInfo['shopLat'];
+		//门店经度
+		$shopLong = $shopInfo['shopLong'];
+		//后端计算距离再确认
+		$phpStatDistance = util::getDistance($shopLat, $shopLong, $lat, $lng);
+		if ($phpStatDistance > $jsStatDistance) {
+			util::fail('前端计算距离有误');
 		}
-		
-		
-		$shopLat = $shopInfo['shopLat'];//门店纬度
-		$shopLong = $shopInfo['shopLong'];//门店经度
-		$calcDistance = util::getDistance($shopLat, $shopLong, $lat, $lng);
-		return $calcDistance;
+		$merchantId = Yii::$app->params['merchantId'];
+		$ext = MerchantExtendService::getByMerchantId($merchantId);
+		$freight = MerchantExtendService::getFreight($ext);
+		$firstDistance = $freight[0];
+		$addPrice = $freight[1];
+		$remainDistance = $jsStatDistance > $firstDistance ? stringUtil::calcSub($jsStatDistance, $firstDistance) : 0;
+		$sendCost = ceil($remainDistance * 1000 / 1000 / 1000) * $addPrice;
+		return $sendCost;
 	}
-
+	
 }

+ 1 - 0
sql.txt

@@ -18,6 +18,7 @@ ALTER TABLE xhOrder CHANGE `needSend` `sendType` TINYINT NOT NULL DEFAULT 0 COMM
 ALTER TABLE xhOrder MODIFY reachPeriod TINYINT NOT NULL DEFAULT 0 COMMENT '0上午1下午2晚上';
 ALTER TABLE xhOrder DROP COLUMN `blessing`;
 ALTER TABLE xhOrder ADD cardInfo VARCHAR(500) NOT NULL DEFAULT '' COMMENT '贺卡内容' AFTER `needCard`;
+ALTER TABLE xhOrder MODIFY `payWay` TINYINT NOT NULL DEFAULT 0 COMMENT '支付方式 0微信 1支付宝 2余额';
 
 2019.12.4 shish
 !!执行下面的sql,全部的会员折扣要纠正