Checkout.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace addons\shop\controller;
  3. use addons\shop\model\Address;
  4. use addons\shop\model\Carts;
  5. use addons\shop\model\Order;
  6. use addons\shop\model\UserCoupon;
  7. use think\Config;
  8. /**
  9. * 结算控制器
  10. * Class Checkout
  11. * @package addons\shop\controller
  12. */
  13. class Checkout extends Base
  14. {
  15. protected $noNeedLogin = [];
  16. protected $noNeedRight = '*';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. }
  21. /**
  22. * 结算页
  23. */
  24. public function index()
  25. {
  26. $cartIds = $this->request->post('ids/a');
  27. $addressId = $this->request->post('address_id/d'); //地址id
  28. $user_coupon_id = $this->request->post('user_coupon_id/d'); //优惠券
  29. if (empty($cartIds)) {
  30. $this->error('请选择需要结算的商品');
  31. }
  32. $addressInfo = Address::get($addressId);
  33. if (!$addressInfo) {
  34. $addressInfo = Address::where('isdefault', 1)->where('user_id', $this->auth->id)->where('status', 'normal')->find();
  35. }
  36. $orderItem = [];
  37. $orderInfo = [
  38. 'order_sn' => '',
  39. 'goodsprice' => 0, //商品总金额
  40. 'amount' => 0, //总金额
  41. 'shippingfee' => 0, //运费
  42. 'discount' => 0, //优惠金额
  43. ];
  44. $goodsList = [];
  45. $areaId = !empty($addressInfo) ? $addressInfo->area_id : 0;
  46. try {
  47. list($orderItem, $goodsList) = Order::computeCarts($orderInfo, $cartIds, $this->auth->id, $areaId, $user_coupon_id);
  48. if (empty($goodsList)) {
  49. throw new \Exception("未找到商品");
  50. }
  51. } catch (\Exception $e) {
  52. $this->error($e->getMessage());
  53. }
  54. foreach ($goodsList as $item) {
  55. $item->category_id = $item->goods->category_id;
  56. $item->brand_id = $item->goods->brand_id;
  57. }
  58. $goods_ids = array_column($goodsList, 'goods_id');
  59. $category_ids = array_column($goodsList, 'category_id');
  60. $brand_ids = array_column($goodsList, 'brand_id');
  61. $addressList = Address::getAddressList($this->auth->id);
  62. $couponList = UserCoupon::myGoodsCoupon($this->auth->id, $goods_ids, $category_ids, $brand_ids);
  63. $this->view->assign([
  64. 'cartIds' => $cartIds,
  65. 'addressList' => $addressList,
  66. 'goodsList' => $goodsList,
  67. 'orderItem' => $orderItem,
  68. 'addressInfo' => $addressInfo,
  69. 'orderInfo' => $orderInfo,
  70. 'couponList' => $couponList
  71. ]);
  72. Config::set('shop.title', "订单结算");
  73. return $this->view->fetch('/checkout');
  74. }
  75. /**
  76. * 提交订单
  77. */
  78. public function submit()
  79. {
  80. if ($this->request->isPost()) {
  81. $cartIds = $this->request->post('ids');
  82. $addressId = $this->request->post('address_id/d'); //地址id
  83. $user_coupon_id = $this->request->post('user_coupon_id/d'); //优惠券
  84. $memo = $this->request->post('memo');
  85. if (empty($cartIds)) {
  86. $this->error('商品信息已变更,请返回刷新重试');
  87. }
  88. if (empty($addressId)) {
  89. $this->error('请选择收货地址');
  90. }
  91. //为购物车id
  92. //校验购物车id 合法
  93. $row = (new Carts)->where('id', 'IN', $cartIds)->where('user_id', '<>', $this->auth->id)->find();
  94. if ($row) {
  95. $this->error('存在不合法购物车数据');
  96. }
  97. $order = null;
  98. try {
  99. $order = Order::createOrder($addressId, $this->auth->id, $cartIds, $user_coupon_id, $memo);
  100. } catch (\Exception $e) {
  101. $this->error($e->getMessage());
  102. }
  103. $this->redirect(addon_url('shop/payment/index') . '?orderid=' . $order['order_sn']);
  104. }
  105. return;
  106. }
  107. /**
  108. * 重新统计
  109. */
  110. public function recount()
  111. {
  112. if ($this->request->isPost()) {
  113. $cartIds = $this->request->post('ids');
  114. $cartIds = explode(',', $cartIds);
  115. $addressId = $this->request->post('address_id/d'); //地址id
  116. if (empty($cartIds)) {
  117. $this->error('请选择需要结算的商品');
  118. }
  119. $user_coupon_id = $this->request->post('user_coupon_id/d'); //优惠券
  120. $addressInfo = Address::get($addressId);
  121. if (!$addressInfo) {
  122. $addressInfo = Address::where('isdefault', 1)->where('user_id', $this->auth->id)->where('status', 'normal')->find();
  123. }
  124. $orderInfo = [
  125. 'order_sn' => '',
  126. 'goodsprice' => 0, //商品总金额
  127. 'amount' => 0, //总金额
  128. 'shippingfee' => 0, //运费
  129. 'discount' => 0, //优惠金额
  130. ];
  131. $areaId = !empty($addressInfo) ? $addressInfo->area_id : 0;
  132. try {
  133. list($orderItem, $goodsList) = Order::computeCarts($orderInfo, $cartIds, $this->auth->id, $areaId, $user_coupon_id);
  134. if (empty($goodsList)) {
  135. throw new \Exception("未找到商品");
  136. }
  137. } catch (\Exception $e) {
  138. $this->error($e->getMessage());
  139. }
  140. $this->success('', '', ['orderInfo' => $orderInfo]);
  141. }
  142. return;
  143. }
  144. }