|
|
@@ -7,6 +7,7 @@ use bizMall\merchant\services\ExpressService;
|
|
|
use bizMall\merchant\services\MerchantExtendService;
|
|
|
use bizMall\merchant\services\MerchantService;
|
|
|
use bizMall\merchant\services\ShopService;
|
|
|
+use bizMall\order\classes\OrderClass;
|
|
|
use bizMall\order\services\OrderGoodsService;
|
|
|
use bizMall\order\services\OrderService;
|
|
|
use bizMall\promote\services\CouponService;
|
|
|
@@ -45,85 +46,98 @@ class OrderController extends BaseController
|
|
|
//运费计算方式
|
|
|
$freightType = isset($goodsInfo['freightType']) ? $goodsInfo['freightType'] : 0;
|
|
|
|
|
|
- $post['userId'] = $this->userId;
|
|
|
- $user = $this->user;
|
|
|
- $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
|
|
|
- $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
|
|
|
- $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
|
|
|
- $post['bookMobile'] = $bookMobile;
|
|
|
-
|
|
|
- $post['payWay'] = $this->isWx == true ? 0 : 1;
|
|
|
- $defaultShopId = MerchantService::getDefaultShopId($this->sj);
|
|
|
- if (empty($defaultShopId)) {
|
|
|
- util::fail('没有找到门店');
|
|
|
- }
|
|
|
- //计算运费
|
|
|
- $post['sendDistance'] = isset($post['sendDistance']) ? $post['sendDistance'] : 0;
|
|
|
- $sendCost = 0;
|
|
|
- //按距离计算运费并且客户要求送的
|
|
|
- if ($freightType == 0 && $needSend == 1) {
|
|
|
- $lat = $post['latitude'];//收货人纬度
|
|
|
- $lng = $post['longitude'];//收货人经度
|
|
|
- $respond = ExpressService::getUserDistance($lng, $lat, $defaultShopId, $this->sjExtend);
|
|
|
- $sendCost = isset($respond['fee']) ? $respond['fee'] : 0;
|
|
|
- }
|
|
|
- $post['sendCost'] = $sendCost;
|
|
|
- $post['merchantId'] = $this->sjId;
|
|
|
- $post['shopId'] = $defaultShopId;
|
|
|
-
|
|
|
- //计算总金额
|
|
|
- $unitPrice = $goodsInfo['price'];
|
|
|
- $goodsStyleList = isset($goodsInfo['goodsStyleList']) ? $goodsInfo['goodsStyleList'] : [];
|
|
|
- if (!empty($goodsStyleId)) {
|
|
|
- if (empty($goodsStyleList)) {
|
|
|
- util::fail('商品款式没有找到');
|
|
|
+ //事务处理
|
|
|
+ $connection = Yii::$app->db;
|
|
|
+ $transaction = $connection->beginTransaction();
|
|
|
+ try {
|
|
|
+
|
|
|
+ $post['userId'] = $this->userId;
|
|
|
+ $user = $this->user;
|
|
|
+ $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
|
|
|
+ $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
|
|
|
+ $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
|
|
|
+ $post['bookMobile'] = $bookMobile;
|
|
|
+
|
|
|
+ $post['payWay'] = $this->isWx == true ? 0 : 1;
|
|
|
+ $defaultShopId = MerchantService::getDefaultShopId($this->sj);
|
|
|
+ if (empty($defaultShopId)) {
|
|
|
+ util::fail('没有找到门店');
|
|
|
}
|
|
|
- $styleIdList = array_keys($goodsStyleList);
|
|
|
- if (in_array($goodsStyleId, $styleIdList) == false) {
|
|
|
- util::fail('商品款式没有找到!');
|
|
|
+ //计算运费
|
|
|
+ $post['sendDistance'] = isset($post['sendDistance']) ? $post['sendDistance'] : 0;
|
|
|
+ $sendCost = 0;
|
|
|
+ //按距离计算运费并且客户要求送的
|
|
|
+ if ($freightType == 0 && $needSend == 1) {
|
|
|
+ $lat = $post['latitude'];//收货人纬度
|
|
|
+ $lng = $post['longitude'];//收货人经度
|
|
|
+ $respond = ExpressService::getUserDistance($lng, $lat, $defaultShopId, $this->sjExtend);
|
|
|
+ $sendCost = isset($respond['fee']) ? $respond['fee'] : 0;
|
|
|
}
|
|
|
- $unitPrice = isset($goodsStyleList[$goodsStyleId]['price']) ? $goodsStyleList[$goodsStyleId]['price'] : 9999;
|
|
|
- }
|
|
|
- $goodsPrice = $unitPrice * $goodsNum;
|
|
|
- $prePrice = stringUtil::calcAdd($sendCost, $goodsPrice);
|
|
|
- //非门店订单
|
|
|
- $store = 0;
|
|
|
- $post['store'] = $store;
|
|
|
- //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
|
|
|
- $sourceType = $this->isWx ? 0 : 1;
|
|
|
- if (httpUtil::isMiniProgram()) {
|
|
|
- $sourceType = 2;
|
|
|
+ $post['sendCost'] = $sendCost;
|
|
|
+ $post['merchantId'] = $this->sjId;
|
|
|
+ $post['shopId'] = $defaultShopId;
|
|
|
+
|
|
|
+ //计算总金额
|
|
|
+ $unitPrice = $goodsInfo['price'];
|
|
|
+ $goodsStyleList = isset($goodsInfo['goodsStyleList']) ? $goodsInfo['goodsStyleList'] : [];
|
|
|
+ if (!empty($goodsStyleId)) {
|
|
|
+ if (empty($goodsStyleList)) {
|
|
|
+ util::fail('商品款式没有找到');
|
|
|
+ }
|
|
|
+ $styleIdList = array_keys($goodsStyleList);
|
|
|
+ if (in_array($goodsStyleId, $styleIdList) == false) {
|
|
|
+ util::fail('商品款式没有找到!');
|
|
|
+ }
|
|
|
+ $unitPrice = isset($goodsStyleList[$goodsStyleId]['price']) ? $goodsStyleList[$goodsStyleId]['price'] : 9999;
|
|
|
+ }
|
|
|
+ $goodsPrice = $unitPrice * $goodsNum;
|
|
|
+ $prePrice = stringUtil::calcAdd($sendCost, $goodsPrice);
|
|
|
+ //非门店订单
|
|
|
+ $store = 0;
|
|
|
+ $post['store'] = $store;
|
|
|
+ //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
|
|
|
+ $sourceType = $this->isWx ? 0 : 1;
|
|
|
+ if (httpUtil::isMiniProgram()) {
|
|
|
+ $sourceType = 2;
|
|
|
+ }
|
|
|
+ $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0;
|
|
|
+ $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]);
|
|
|
+ $actPrice = $discountData['price'];
|
|
|
+ $post['discountType'] = $discountData['discountType'];
|
|
|
+ $post['discountAmount'] = $discountData['discountAmount'];
|
|
|
+
|
|
|
+ $now = time();
|
|
|
+ $expireTime = $now + 1800;//订单30分钟后过期
|
|
|
+ $post['deadline'] = $expireTime;
|
|
|
+ $post['fromType'] = 1;//商城
|
|
|
+ $post['prePrice'] = $prePrice;
|
|
|
+ $post['actPrice'] = $actPrice;
|
|
|
+ $order = OrderClass::addOrder($post);
|
|
|
+ $orderId = $order['id'];
|
|
|
+ $orderSn = $order['orderSn'];
|
|
|
+ $data = [
|
|
|
+ 'orderId' => $orderId,
|
|
|
+ 'goodsId' => $goodsId,
|
|
|
+ 'userId' => $this->userId,
|
|
|
+ 'merchantId' => $this->sjId,
|
|
|
+ 'title' => $goodsInfo['goodsName'],
|
|
|
+ 'cover' => isset($goodsInfo['shortImgList']) && !empty($goodsInfo['shortImgList']) ? current($goodsInfo['shortImgList']) : '',
|
|
|
+ 'unitPrice' => $unitPrice,
|
|
|
+ 'num' => $goodsNum,
|
|
|
+ 'goodsStyleName' => isset($goodsInfo['goodsStyleList'][$goodsStyleId]['title']) ? $goodsInfo['goodsStyleList'][$goodsStyleId]['title'] : '',
|
|
|
+ 'goodsStyleId' => $goodsStyleId,
|
|
|
+ 'createTime' => date("Y-m-d H:i:s"),
|
|
|
+ ];
|
|
|
+ OrderGoodsService::add($data);//创建订单商品列表
|
|
|
+
|
|
|
+ $transaction->commit();
|
|
|
+ util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => $couponId]);
|
|
|
+ } catch (Exception $e) {
|
|
|
+ $transaction->rollBack();
|
|
|
+ Yii::info("下单失败原因:" . $e->getMessage());
|
|
|
+ util::fail('下单失败');
|
|
|
}
|
|
|
- $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0;
|
|
|
- $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]);
|
|
|
- $actPrice = $discountData['price'];
|
|
|
- $post['discountType'] = $discountData['discountType'];
|
|
|
- $post['discountAmount'] = $discountData['discountAmount'];
|
|
|
|
|
|
- $now = time();
|
|
|
- $expireTime = $now + 1800;//订单30分钟后过期
|
|
|
- $post['deadline'] = $expireTime;
|
|
|
- $post['fromType'] = 1;//商城
|
|
|
- $post['prePrice'] = $prePrice;
|
|
|
- $post['actPrice'] = $actPrice;
|
|
|
- $order = OrderService::addOrder($post);
|
|
|
- $orderId = $order['id'];
|
|
|
- $orderSn = $order['orderSn'];
|
|
|
- $data = [
|
|
|
- 'orderId' => $orderId,
|
|
|
- 'goodsId' => $goodsId,
|
|
|
- 'userId' => $this->userId,
|
|
|
- 'merchantId' => $this->sjId,
|
|
|
- 'title' => $goodsInfo['goodsName'],
|
|
|
- 'cover' => isset($goodsInfo['shortImgList']) && !empty($goodsInfo['shortImgList']) ? current($goodsInfo['shortImgList']) : '',
|
|
|
- 'unitPrice' => $unitPrice,
|
|
|
- 'num' => $goodsNum,
|
|
|
- 'goodsStyleName' => isset($goodsInfo['goodsStyleList'][$goodsStyleId]['title']) ? $goodsInfo['goodsStyleList'][$goodsStyleId]['title'] : '',
|
|
|
- 'goodsStyleId' => $goodsStyleId,
|
|
|
- 'createTime' => date("Y-m-d H:i:s"),
|
|
|
- ];
|
|
|
- OrderGoodsService::add($data);//创建订单商品列表
|
|
|
- util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => $couponId]);
|
|
|
}
|
|
|
|
|
|
//下单要用到的相关信息 shish 2019.12.6
|
|
|
@@ -262,11 +276,7 @@ class OrderController extends BaseController
|
|
|
$store = isset($post['store']) ? $post['store'] : 1;
|
|
|
$post['store'] = $store;
|
|
|
//来源 0微信 1支付宝 2小程序 3朋友圈 4美团
|
|
|
- $sourceType = $this->isWx ? 0 : 1;
|
|
|
- if (httpUtil::isMiniProgram()) {
|
|
|
- $sourceType = 2;
|
|
|
- }
|
|
|
- $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 0;
|
|
|
+ $sourceType = isset($post['sourceType']) ? $post['sourceType'] : 2;
|
|
|
//兼容旧系统sourceType=3表示朋友圈来源
|
|
|
$fromType = $sourceType == 3 ? 2 : 0;
|
|
|
$fromType = isset($post['fromType']) && !empty($post['fromType']) ? $post['fromType'] : $fromType;
|
|
|
@@ -297,7 +307,7 @@ class OrderController extends BaseController
|
|
|
$post['sourceType'] = $sourceType;
|
|
|
$post['goodsNum'] = 1;
|
|
|
$post['fromType'] = $fromType;
|
|
|
- $order = OrderService::addOrder($post);
|
|
|
+ $order = OrderClass::addOrder($post);
|
|
|
$orderId = $order['id'];
|
|
|
$orderSn = $order['orderSn'];
|
|
|
$sjId = $this->sjId;
|
|
|
@@ -306,13 +316,10 @@ class OrderController extends BaseController
|
|
|
$name = '购买商品';
|
|
|
$totalFee = $actPrice;
|
|
|
$user = UserService::getById($this->userId);
|
|
|
- $openId = isset($user['openId']) ? $user['openId'] : 0;
|
|
|
- if (httpUtil::isMiniProgram()) {
|
|
|
- //小程序使用miniOpenId
|
|
|
- $openId = $user['miniOpenId'];
|
|
|
- }
|
|
|
+ //小程序使用miniOpenId
|
|
|
+ $openId = $user['miniOpenId'];
|
|
|
if (empty($openId)) {
|
|
|
- util::fail('没有找到客户的openId');
|
|
|
+ util::fail('没有找您的微信ID');
|
|
|
}
|
|
|
$typeList = dict::getConfig('capitalType');
|
|
|
$capitalType = $typeList['xhOrder']['id'];
|
|
|
@@ -336,13 +343,10 @@ class OrderController extends BaseController
|
|
|
$input->SetNotify_url(Yii::$app->params['mallHost'] . '/notice/wx-callback/');
|
|
|
$input->SetTrade_type("JSAPI");
|
|
|
|
|
|
- //花卉宝代为申请的微信支付
|
|
|
$sjExtend = $this->sjExtend;
|
|
|
- if ($sjExtend['wxPayApply'] == 1) {
|
|
|
- $input->SetSub_openid($openId);
|
|
|
- } else {
|
|
|
- $input->SetOpenid($openId);
|
|
|
- }
|
|
|
+ //花卉宝代为申请的微信支付
|
|
|
+ //$input->SetSub_openid($openId);
|
|
|
+ $input->SetOpenid($openId);
|
|
|
//小程序使用miniAppId
|
|
|
if (httpUtil::isMiniProgram()) {
|
|
|
$sjExtend['wxAppId'] = $sjExtend['miniAppId'];
|