|
|
@@ -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]);
|
|
|
}
|
|
|
|