OrderController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * User: shish <shish@zhhinc.com>
  4. * Date: 2019/7/15
  5. * Time: 14:27
  6. */
  7. namespace mini\controllers;
  8. use biz\order\services\OrderService;
  9. use common\components\configDict;
  10. use common\components\util;
  11. use common\services\xhCartService;
  12. use common\services\xhCouponService;
  13. use common\services\xhGoodsPriceService;
  14. use common\services\xhGoodsService;
  15. use common\services\xhGoodsSettingService;
  16. use common\services\xhMerchantExtendService;
  17. use common\services\xhOrderGoodsService;
  18. use common\services\xhOrderService;
  19. use common\services\xhUserAssetService;
  20. use Yii;
  21. use yii\helpers\Json;
  22. class OrderController extends BaseController
  23. {
  24. public $enableCsrfValidation = false;
  25. //创建订单
  26. public function actionGenerateOrder()
  27. {
  28. $post = Yii::$app->request->post();
  29. $userId = $this->userId;
  30. $post['userId'] = $userId;
  31. $post['payWay'] = 0;
  32. $regionArr = explode(',',$post['region']);
  33. /*
  34. $lat2 = $post['receiveLat'];//收货人纬度
  35. $lng2 = $post['receiveLong'];//收货人经度
  36. $lat1 = $this->merchant['shopLat'];//花店纬度
  37. $lng1 = $this->merchant['shopLong'];//花店经度
  38. $calcDistance = util::getDistance($lat1, $lng1, $lat2, $lng2);//防止别人改动,PHP计算的距离会比腾讯js算的小,但能保证有一定准确性
  39. $post['sendDistance'] = $post['sendDistance'] >= $calcDistance ? $post['sendDistance'] : $calcDistance;//二地距离
  40. $sendDistance = $post['sendDistance'];
  41. $freight = configDict::getConfig('freight');
  42. $firstDistance = $freight['firstDistance'];
  43. $firstPrice = $freight['firstPrice'];
  44. $nextDistance = $freight['nextDistance'];
  45. $nextPrice = $freight['nextPrice'];
  46. if ($sendDistance <= $firstDistance && $post['isCharge'] == 1) {
  47. $post['sendCost'] = $firstPrice;
  48. } else if ($post['isCharge'] == 1) {
  49. $subDistance = $sendDistance - $firstDistance;
  50. $addPrice = intval($subDistance / $nextDistance) * $nextPrice;
  51. $post['sendCost'] = $firstPrice + $addPrice;
  52. }
  53. */
  54. $post['receiveProvince'] = $regionArr[0];
  55. $post['receiveCity'] = $regionArr[1];
  56. $post['receiveDist'] = $regionArr[2];
  57. $post['receiveFullAddress'] = $regionArr[0].$regionArr[1].$regionArr[2].$post['receiveAddress'].$post['receiveFloor'];
  58. $extend = xhMerchantExtendService::getByMerchantId($this->merchantId);
  59. if (empty($extend['payment'])) {
  60. util::sendFail('支付功能未开通,暂时无法购买哦~');
  61. }
  62. $couponAmount = 0;
  63. $couponId = isset($post['couponId']) ? $post['couponId'] : 0;//代金劵
  64. if (!empty($couponId)) {
  65. $coupon = xhCouponService::getById($couponId);
  66. $couponUserId = $coupon['userId'];
  67. if ($couponUserId != $userId) {
  68. util::failInfo('不是你的代金劵');
  69. }
  70. if ($coupon['useStatus'] == 1 || $coupon['deadline'] < time()) {
  71. util::failInfo('代金劵已经失效了');
  72. }
  73. $couponAmount = $coupon['amount'];
  74. }
  75. //不要将代金劵保存到订单表,付款成功后再保存进去!!!
  76. unset($post['couponId']);
  77. $post['merchantId'] = $this->merchantId;
  78. //店铺id
  79. $post['shopId'] = $this->merchant['defaultShopId'];
  80. $order = xhOrderService::add($post);//创建订单
  81. if ($order == false) {
  82. util::failInfo('创建订单失败');
  83. }
  84. $orderId = $order['id'];
  85. $oData = [];
  86. $goodsInfo = json_decode($post['goodsInfo'], true);
  87. $totalFee = 0;//计算总价格
  88. $goodsIdList = [];
  89. $multiPriceIdList = [];
  90. $orderName = '';
  91. $goodsNum = 0;
  92. foreach ($goodsInfo as $key => $val) {
  93. $multiPriceId = 0;//多种价格表Id
  94. $goodsId = $val['goodsId'];
  95. $num = $val['num'];
  96. $goodsNum += $num;
  97. $goods = xhGoodsService::getById($goodsId);
  98. if (empty($orderName)) {
  99. $orderName = $goods['goodsName'];
  100. }
  101. $price = $goods['price'];
  102. if ($val['multiPriceId'] != 0 && $goods['multiPrice'] != '') {
  103. $pos = strpos($goods['multiPrice'], $val['multiPriceId']);
  104. if ($pos === false) {
  105. util::failInfo('订单的商品不存在');
  106. }
  107. $goodPrice = xhGoodsPriceService::getById($val['multiPriceId']);
  108. //$price = $goodPrice['price'];
  109. $price = xhGoodsSettingService::changePrice($goods, $goodPrice['price']);//统一对商品进行价格等方面设置
  110. //多种价格规格 替换 默认(单价格)
  111. $goods['goodsName'] = $goodPrice['title'];
  112. //$goods['cover'] = $goodPrice['picture'];//后台图片上传功能未开发,暂时使用默认商品图片
  113. $multiPriceId = $val['multiPriceId'];
  114. }
  115. $totalFee += $price * $num;
  116. $goodsIdList[] = $goodsId;
  117. $multiPriceIdList[] = $multiPriceId;
  118. $data = [
  119. 'orderId' => $orderId,
  120. 'goodsId' => $goodsId,
  121. 'userId' => $userId,
  122. 'merchantId' => $this->merchantId,
  123. 'title' => $goods['goodsName'],
  124. 'cover' => $goods['cover'],
  125. 'unitPrice' => $price,
  126. 'num' => $num,
  127. 'multiPriceId' => $multiPriceId,
  128. 'createTime' => date("Y-m-d H:i:s"),
  129. ];
  130. xhOrderGoodsService::add($data);//创建订单商品列表
  131. }
  132. $sendCost = $post['sendCost'];//运费
  133. $oData['goodsNum'] = $goodsNum;//订单的商品数量类型,单个还是多个的
  134. $oData['prePrice'] = $totalFee + $sendCost;
  135. $oData['orderName'] = $goodsNum > 1 ? $orderName . '等' . $goodsNum . '件商品' : $orderName;
  136. if (isset($post['sourceType']) && $post['sourceType'] == 'cart') {
  137. if (!empty($goodsIdList)) {
  138. foreach ($goodsIdList as $key => $goodsId) {
  139. xhCartService::delByIds($userId, $goodsId, $multiPriceIdList[$key]);//删除购物车($userId, $goodsId, $goodsPriceId, $cartId)
  140. }
  141. }
  142. }
  143. $userAsset = xhUserAssetService::getByUserId($userId);
  144. $level = $userAsset['memberLevel'];
  145. if ($level > 0) {
  146. $merchantExtend = xhMerchantExtendService::getByMerchantId($this->merchantId);
  147. $gradeList = xhMerchantExtendService::getGradeList($merchantExtend);
  148. $discount = $gradeList[$level]['discount'];
  149. $discount = strlen($discount) == 1 ? $discount * 10 : $discount;
  150. $totalFee = number_format($discount * $totalFee / 100, 2);
  151. }
  152. $oData['actPrice'] = $totalFee + $sendCost - $couponAmount;
  153. xhOrderService::updateById($orderId, $oData);
  154. util::sendJson(['orderId' => $orderId, 'couponId' => $couponId, 'price' => $oData['actPrice']]);
  155. }
  156. //取出所有订单
  157. public function actionGetList()
  158. {
  159. $list = OrderService::getAllListInfo($this->userId);
  160. util::sendJson(['list' => $list]);
  161. }
  162. //订单详情
  163. public function actionGetDetail()
  164. {
  165. $id = Yii::$app->request->get('id');
  166. $info = OrderService::getDetail($id);
  167. if (isset($info) && $info['userId'] != $this->userId) {
  168. util::sendFail('不是你的订单');
  169. }
  170. util::sendJson(['info' => $info]);
  171. }
  172. public function actionOrder()
  173. {
  174. $order = xhOrderService::getAllByCondition(['userId' => $this->userId]);
  175. $payStatusName = configDict::getConfig('payStatusName');
  176. return $this->render('order', ['order' => $order, 'payStatusName' => $payStatusName]);
  177. }
  178. public function actionOrderDetail()
  179. {
  180. $get = Yii::$app->request->get();
  181. $id = isset($get['id']) ? $get['id'] : 0;
  182. $order = xhOrderService::getById($id);
  183. if ($order['userId'] != $this->userId) {
  184. util::stop('非法访问');
  185. }
  186. if ($order['sourceType'] == 0) {//0商城订单
  187. $goodsList = xhOrderGoods::getAllByCondition(['orderId' => $id]);
  188. foreach ($goodsList as $key => $good) {
  189. $goodsId = $good['goodsId'];
  190. $goodsData = xhGoodsService::getById($goodsId);
  191. if ($goodsData['goodsName'] != $good['title']) {
  192. $subTitle = $good['title'];
  193. $goodsList[$key]['title'] = $goodsData['goodsName'];
  194. $goodsList[$key]['subTitle'] = $subTitle;
  195. }
  196. }
  197. } else if ($order['sourceType'] == 1) {//1付款订单
  198. $goodsList = [];
  199. } else {
  200. util::stop('未知订单类型');
  201. }
  202. }
  203. }