* Date: 2019/7/15 * Time: 14:27 */ namespace mini\controllers; use biz\order\services\OrderService; use common\components\configDict; use common\components\util; use common\services\xhCartService; use common\services\xhCouponService; use common\services\xhGoodsPriceService; use common\services\xhGoodsService; use common\services\xhGoodsSettingService; use common\services\xhMerchantExtendService; use common\services\xhOrderGoodsService; use common\services\xhOrderService; use common\services\xhUserAssetService; use Yii; use yii\helpers\Json; class OrderController extends BaseController { public $enableCsrfValidation = false; //创建订单 public function actionGenerateOrder() { $post = Yii::$app->request->post(); $userId = $this->userId; $post['userId'] = $userId; $post['payWay'] = 0; $regionArr = explode(',',$post['region']); /* $lat2 = $post['receiveLat'];//收货人纬度 $lng2 = $post['receiveLong'];//收货人经度 $lat1 = $this->merchant['shopLat'];//花店纬度 $lng1 = $this->merchant['shopLong'];//花店经度 $calcDistance = util::getDistance($lat1, $lng1, $lat2, $lng2);//防止别人改动,PHP计算的距离会比腾讯js算的小,但能保证有一定准确性 $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; } */ $post['receiveProvince'] = $regionArr[0]; $post['receiveCity'] = $regionArr[1]; $post['receiveDist'] = $regionArr[2]; $post['receiveFullAddress'] = $regionArr[0].$regionArr[1].$regionArr[2].$post['receiveAddress'].$post['receiveFloor']; $extend = xhMerchantExtendService::getByMerchantId($this->merchantId); if (empty($extend['payment'])) { util::sendFail('支付功能未开通,暂时无法购买哦~'); } $couponAmount = 0; $couponId = isset($post['couponId']) ? $post['couponId'] : 0;//代金劵 if (!empty($couponId)) { $coupon = xhCouponService::getById($couponId); $couponUserId = $coupon['userId']; if ($couponUserId != $userId) { util::failInfo('不是你的代金劵'); } if ($coupon['useStatus'] == 1 || $coupon['deadline'] < time()) { util::failInfo('代金劵已经失效了'); } $couponAmount = $coupon['amount']; } //不要将代金劵保存到订单表,付款成功后再保存进去!!! unset($post['couponId']); $post['merchantId'] = $this->merchantId; //店铺id $post['shopId'] = $this->merchant['defaultShopId']; $order = xhOrderService::add($post);//创建订单 if ($order == false) { util::failInfo('创建订单失败'); } $orderId = $order['id']; $oData = []; $goodsInfo = json_decode($post['goodsInfo'], true); $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::failInfo('订单的商品不存在'); } $goodPrice = xhGoodsPriceService::getById($val['multiPriceId']); //$price = $goodPrice['price']; $price = xhGoodsSettingService::changePrice($goods, $goodPrice['price']);//统一对商品进行价格等方面设置 //多种价格规格 替换 默认(单价格) $goods['goodsName'] = $goodPrice['title']; //$goods['cover'] = $goodPrice['picture'];//后台图片上传功能未开发,暂时使用默认商品图片 $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; if (isset($post['sourceType']) && $post['sourceType'] == 'cart') { if (!empty($goodsIdList)) { foreach ($goodsIdList as $key => $goodsId) { xhCartService::delByIds($userId, $goodsId, $multiPriceIdList[$key]);//删除购物车($userId, $goodsId, $goodsPriceId, $cartId) } } } $userAsset = xhUserAssetService::getByUserId($userId); $level = $userAsset['memberLevel']; if ($level > 0) { $merchantExtend = xhMerchantExtendService::getByMerchantId($this->merchantId); $gradeList = xhMerchantExtendService::getGradeList($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); util::sendJson(['orderId' => $orderId, 'couponId' => $couponId, 'price' => $oData['actPrice']]); } //取出所有订单 public function actionGetList() { $list = OrderService::getAllListInfo($this->userId); util::sendJson(['list' => $list]); } //订单详情 public function actionGetDetail() { $id = Yii::$app->request->get('id'); $info = OrderService::getDetail($id); if (isset($info) && $info['userId'] != $this->userId) { util::sendFail('不是你的订单'); } util::sendJson(['info' => $info]); } public function actionOrder() { $order = xhOrderService::getAllByCondition(['userId' => $this->userId]); $payStatusName = configDict::getConfig('payStatusName'); return $this->render('order', ['order' => $order, 'payStatusName' => $payStatusName]); } public function actionOrderDetail() { $get = Yii::$app->request->get(); $id = isset($get['id']) ? $get['id'] : 0; $order = xhOrderService::getById($id); if ($order['userId'] != $this->userId) { util::stop('非法访问'); } if ($order['sourceType'] == 0) {//0商城订单 $goodsList = xhOrderGoods::getAllByCondition(['orderId' => $id]); foreach ($goodsList as $key => $good) { $goodsId = $good['goodsId']; $goodsData = xhGoodsService::getById($goodsId); if ($goodsData['goodsName'] != $good['title']) { $subTitle = $good['title']; $goodsList[$key]['title'] = $goodsData['goodsName']; $goodsList[$key]['subTitle'] = $subTitle; } } } else if ($order['sourceType'] == 1) {//1付款订单 $goodsList = []; } else { util::stop('未知订单类型'); } } }