Order.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\api\controller;
  10. use app\api\model\Cart as CartModel;
  11. use app\api\model\Order as OrderModel;
  12. use app\api\service\order\Checkout as CheckoutModel;
  13. use app\api\validate\order\Checkout as CheckoutValidate;
  14. /**
  15. * 订单控制器
  16. * Class Order
  17. * @package app\api\controller
  18. */
  19. class Order extends Controller
  20. {
  21. /* @var \app\api\model\User $user */
  22. private $user;
  23. /* @var CheckoutValidate $validate */
  24. private $validate;
  25. /**
  26. * 构造方法
  27. * @throws \app\common\exception\BaseException
  28. * @throws \think\exception\DbException
  29. */
  30. public function _initialize()
  31. {
  32. parent::_initialize();
  33. // 用户信息
  34. $this->user = $this->getUser();
  35. // 验证类
  36. $this->validate = new CheckoutValidate;
  37. }
  38. /**
  39. * 订单确认-立即购买
  40. * @return array
  41. * @throws \app\common\exception\BaseException
  42. * @throws \think\exception\DbException
  43. * @throws \Exception
  44. */
  45. public function buyNow()
  46. {
  47. // 实例化结算台服务
  48. $Checkout = new CheckoutModel;
  49. // 订单结算api参数
  50. $params = $Checkout->setParam($this->getParam([
  51. 'goods_id' => 0,
  52. 'goods_num' => 0,
  53. 'goods_sku_id' => '',
  54. ]));
  55. // 表单验证
  56. if (!$this->validate->scene('buyNow')->check($params)) {
  57. return $this->renderError($this->validate->getError());
  58. }
  59. // 立即购买:获取订单商品列表
  60. $model = new OrderModel;
  61. $goodsList = $model->getOrderGoodsListByNow(
  62. $params['goods_id'],
  63. $params['goods_sku_id'],
  64. $params['goods_num']
  65. );
  66. // 获取订单确认信息
  67. $orderInfo = $Checkout->onCheckout($this->user, $goodsList);
  68. if ($this->request->isGet()) {
  69. return $this->renderSuccess($orderInfo);
  70. }
  71. // 订单结算提交
  72. if ($Checkout->hasError()) {
  73. return $this->renderError($Checkout->getError());
  74. }
  75. // 创建订单
  76. if (!$Checkout->createOrder($orderInfo)) {
  77. return $this->renderError($Checkout->getError() ?: '订单创建失败');
  78. }
  79. // 构建微信支付请求
  80. $payment = $model->onOrderPayment($this->user, $Checkout->model, $params['pay_type']);
  81. // 返回结算信息
  82. return $this->renderSuccess([
  83. 'order_id' => $Checkout->model['order_id'], // 订单id
  84. 'pay_type' => $params['pay_type'], // 支付方式
  85. 'payment' => $payment // 微信支付参数
  86. ], ['success' => '支付成功', 'error' => '订单未支付']);
  87. }
  88. /**
  89. * 订单确认-购物车结算
  90. * @return array
  91. * @throws \app\common\exception\BaseException
  92. * @throws \think\Exception
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. * @throws \think\exception\DbException
  96. * @throws \Exception
  97. */
  98. public function cart()
  99. {
  100. // 实例化结算台服务
  101. $Checkout = new CheckoutModel;
  102. // 订单结算api参数
  103. $params = $Checkout->setParam($this->getParam([
  104. 'cart_ids' => '',
  105. ]));
  106. // 商品结算信息
  107. $CartModel = new CartModel($this->user);
  108. // 购物车商品列表
  109. $goodsList = $CartModel->getList($params['cart_ids']);
  110. // 获取订单结算信息
  111. $orderInfo = $Checkout->onCheckout($this->user, $goodsList);
  112. if ($this->request->isGet()) {
  113. return $this->renderSuccess($orderInfo);
  114. }
  115. // 创建订单
  116. if (!$Checkout->createOrder($orderInfo)) {
  117. return $this->renderError($Checkout->getError() ?: '订单创建失败');
  118. }
  119. // 移出购物车中已下单的商品
  120. $CartModel->clearAll($params['cart_ids']);
  121. // 构建微信支付请求
  122. $payment = $Checkout->onOrderPayment();
  123. // 返回状态
  124. return $this->renderSuccess([
  125. 'order_id' => $Checkout->model['order_id'], // 订单id
  126. 'pay_type' => $params['pay_type'], // 支付方式
  127. 'payment' => $payment // 微信支付参数
  128. ], ['success' => '支付成功', 'error' => '订单未支付']);
  129. }
  130. /**
  131. * 订单结算提交的参数
  132. * @param array $define
  133. * @return array
  134. */
  135. private function getParam($define = [])
  136. {
  137. return array_merge($define, $this->request->param());
  138. }
  139. }