Order.php 4.5 KB

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