|
|
@@ -37,116 +37,129 @@ class OrderController extends BaseController
|
|
|
{
|
|
|
$post = Yii::$app->request->post();
|
|
|
$needSend = isset($post['needSend']) && is_numeric($post['needSend']) ? $post['needSend'] : 1;
|
|
|
+ $orderType = $post['orderType'] ?? 1;
|
|
|
|
|
|
- $goodsInfo = $post['goodsInfo'] ?? '';
|
|
|
- $goodsInfoArr = json_decode($goodsInfo, true);
|
|
|
- if (empty($goodsInfoArr)) {
|
|
|
- util::fail('请选择商品');
|
|
|
- }
|
|
|
- //总的商品金额
|
|
|
- $totalGoodsPrice = 0;
|
|
|
- $orderGoods = [];
|
|
|
- foreach ($goodsInfoArr as $key => $currentGoods) {
|
|
|
- $currentGoodsId = $currentGoods['goodsId'];
|
|
|
- $currentGoodsInfo = GoodsService::getGoodsInfo($currentGoodsId);
|
|
|
- if (empty($currentGoodsInfo)) {
|
|
|
- util::fail('没有找到商品');
|
|
|
- }
|
|
|
- $goodsStyleId = $currentGoods['goodsStyleId'];
|
|
|
- $goodsNum = $currentGoods['goodsNum'];
|
|
|
- //计算总金额
|
|
|
- $unitPrice = $currentGoodsInfo['price'];
|
|
|
- $goodsStyleList = isset($currentGoodsInfo['goodsStyleList']) ? $currentGoodsInfo['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;
|
|
|
- $totalGoodsPrice = stringUtil::calcAdd($totalGoodsPrice, $goodsPrice);
|
|
|
-
|
|
|
- $orderGoods[] = [
|
|
|
- 'goodsId' => $currentGoodsId,
|
|
|
- 'userId' => $this->adminId,
|
|
|
- 'merchantId' => $this->sjId,
|
|
|
- 'title' => $currentGoodsInfo['goodsName'],
|
|
|
- 'cover' => isset($goodsInfo['shortImgList']) && !empty($currentGoodsInfo['shortImgList']) ? current($currentGoodsInfo['shortImgList']) : '',
|
|
|
- 'unitPrice' => $unitPrice,
|
|
|
- 'num' => $goodsNum,
|
|
|
- 'goodsStyleName' => isset($currentGoodsInfo['goodsStyleList'][$goodsStyleId]['title']) ? $currentGoodsInfo['goodsStyleList'][$goodsStyleId]['title'] : '',
|
|
|
- 'goodsStyleId' => $goodsStyleId,
|
|
|
- 'createTime' => date("Y-m-d H:i:s"),
|
|
|
- ];
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- $customId = $post['customId'] ?? 0;
|
|
|
- if (!empty($customId)) {
|
|
|
- $customId = $this->customId;
|
|
|
- }
|
|
|
- $post['customId'] = $customId;
|
|
|
- $user = $this->custom;
|
|
|
- $post['bookName'] = isset($user['name']) ? $user['name'] : '';
|
|
|
- $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;
|
|
|
- //按距离计算运费并且客户要求送的
|
|
|
- //运费计算方式
|
|
|
- $freightType = 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;
|
|
|
-
|
|
|
- $prePrice = stringUtil::calcAdd($sendCost, $totalGoodsPrice);
|
|
|
-
|
|
|
+ $post['payWay'] = $this->isWx == true ? 0 : 1;
|
|
|
+ $now = time();
|
|
|
+ $expireTime = $now + 1800;//订单30分钟后过期
|
|
|
+ $post['deadline'] = $expireTime;
|
|
|
+ $post['fromType'] = 1;//商城
|
|
|
+ $customId = $post['customId'] ?? 0;
|
|
|
+ if (!empty($customId)) {
|
|
|
+ $customId = $this->customId;
|
|
|
+ }
|
|
|
+ $post['customId'] = $customId;
|
|
|
//非门店订单
|
|
|
$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->adminId]);
|
|
|
- $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'];
|
|
|
+ if ($orderType == 1) {
|
|
|
|
|
|
- foreach ($orderGoods as $key => $val) {
|
|
|
- $val['orderId'] = $orderId;
|
|
|
- $val['orderSn'] = $orderSn;
|
|
|
- OrderGoodsService::add($val);//创建订单商品列表
|
|
|
+ $goodsInfo = $post['goodsInfo'] ?? '';
|
|
|
+ $goodsInfoArr = json_decode($goodsInfo, true);
|
|
|
+ if (empty($goodsInfoArr)) {
|
|
|
+ util::fail('请选择商品');
|
|
|
+ }
|
|
|
+ //总的商品金额
|
|
|
+ $totalGoodsPrice = 0;
|
|
|
+ $orderGoods = [];
|
|
|
+ foreach ($goodsInfoArr as $key => $currentGoods) {
|
|
|
+ $currentGoodsId = $currentGoods['goodsId'];
|
|
|
+ $currentGoodsInfo = GoodsService::getGoodsInfo($currentGoodsId);
|
|
|
+ if (empty($currentGoodsInfo)) {
|
|
|
+ util::fail('没有找到商品');
|
|
|
+ }
|
|
|
+ $goodsStyleId = $currentGoods['goodsStyleId'];
|
|
|
+ $goodsNum = $currentGoods['goodsNum'];
|
|
|
+ //计算总金额
|
|
|
+ $unitPrice = $currentGoodsInfo['price'];
|
|
|
+ $goodsStyleList = isset($currentGoodsInfo['goodsStyleList']) ? $currentGoodsInfo['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;
|
|
|
+ $totalGoodsPrice = stringUtil::calcAdd($totalGoodsPrice, $goodsPrice);
|
|
|
+
|
|
|
+ $orderGoods[] = [
|
|
|
+ 'goodsId' => $currentGoodsId,
|
|
|
+ 'userId' => $this->adminId,
|
|
|
+ 'merchantId' => $this->sjId,
|
|
|
+ 'title' => $currentGoodsInfo['goodsName'],
|
|
|
+ 'cover' => isset($goodsInfo['shortImgList']) && !empty($currentGoodsInfo['shortImgList']) ? current($currentGoodsInfo['shortImgList']) : '',
|
|
|
+ 'unitPrice' => $unitPrice,
|
|
|
+ 'num' => $goodsNum,
|
|
|
+ 'goodsStyleName' => isset($currentGoodsInfo['goodsStyleList'][$goodsStyleId]['title']) ? $currentGoodsInfo['goodsStyleList'][$goodsStyleId]['title'] : '',
|
|
|
+ 'goodsStyleId' => $goodsStyleId,
|
|
|
+ 'createTime' => date("Y-m-d H:i:s"),
|
|
|
+ ];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ $user = $this->custom;
|
|
|
+ $post['bookName'] = isset($user['name']) ? $user['name'] : '';
|
|
|
+ $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['sendDistance'] = isset($post['sendDistance']) ? $post['sendDistance'] : 0;
|
|
|
+ $sendCost = 0;
|
|
|
+ //按距离计算运费并且客户要求送的
|
|
|
+ //运费计算方式
|
|
|
+ $freightType = 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;
|
|
|
+
|
|
|
+ $prePrice = stringUtil::calcAdd($sendCost, $totalGoodsPrice);
|
|
|
+
|
|
|
+ //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
|
|
|
+ $sourceType = $this->isWx ? 0 : 1;
|
|
|
+ if (httpUtil::isMiniProgram()) {
|
|
|
+ $sourceType = 2;
|
|
|
+ }
|
|
|
+ $discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->adminId]);
|
|
|
+ $actPrice = $discountData['price'];
|
|
|
+ $post['discountType'] = $discountData['discountType'];
|
|
|
+ $post['discountAmount'] = $discountData['discountAmount'];
|
|
|
+ $post['prePrice'] = $prePrice;
|
|
|
+ $post['actPrice'] = $actPrice;
|
|
|
+ $order = OrderClass::addOrder($post);
|
|
|
+ $orderId = $order['id'];
|
|
|
+ $orderSn = $order['orderSn'];
|
|
|
+
|
|
|
+ foreach ($orderGoods as $key => $val) {
|
|
|
+ $val['orderId'] = $orderId;
|
|
|
+ $val['orderSn'] = $orderSn;
|
|
|
+ OrderGoodsService::add($val);//创建订单商品列表
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $productData = $post['product'] ?? '';
|
|
|
+ $product = json_decode($productData, true);
|
|
|
+
|
|
|
+
|
|
|
+ $order = OrderClass::addOrder($post);
|
|
|
+ $orderId = $order['id'];
|
|
|
+ $orderSn = $order['orderSn'];
|
|
|
}
|
|
|
|
|
|
util::success(['orderSn' => $orderSn, 'totalPrice' => $actPrice, 'couponId' => $couponId, 'orderId' => $orderId]);
|