Order.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. 'is_upgrade' => 0,
  46. 'goods_num' => 0,
  47. 'goods_sku_id' => '',
  48. ]));
  49. // 表单验证
  50. if (!$this->validate->scene('buyNow')->check($params)) {
  51. return $this->renderError($this->validate->getError());
  52. }
  53. // 立即购买:获取订单商品列表
  54. $model = new OrderModel;
  55. $goodsList = $model->getOrderGoodsListByNow(
  56. $params['goods_id'],
  57. $params['goods_sku_id'],
  58. $params['goods_num']
  59. );
  60. // 获取订单确认信息
  61. $orderInfo = $Checkout->onCheckout($this->user, $goodsList);
  62. if ($this->request->isGet()) {
  63. return $this->renderSuccess($orderInfo);
  64. }
  65. // 订单结算提交
  66. if ($Checkout->hasError()) {
  67. return $this->renderError($Checkout->getError());
  68. }
  69. // 创建订单
  70. if (!$Checkout->createOrder($orderInfo)) {
  71. return $this->renderError($Checkout->getError() ?: '订单创建失败');
  72. }
  73. // 构建微信支付请求
  74. $payment = $model->onOrderPayment($this->user, $Checkout->model, $params['pay_type']);
  75. // 返回结算信息
  76. return $this->renderSuccess([
  77. 'order_id' => $Checkout->model['order_id'], // 订单id
  78. 'pay_type' => $params['pay_type'], // 支付方式
  79. 'payment' => $payment // 微信支付参数
  80. ], ['success' => '支付成功', 'error' => '订单未支付']);
  81. }
  82. /**
  83. * 订单确认-购物车结算
  84. * @return array
  85. * @throws \app\common\exception\BaseException
  86. * @throws \think\Exception
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. * @throws \think\exception\DbException
  90. * @throws \Exception
  91. */
  92. public function cart()
  93. {
  94. // 实例化结算台服务
  95. $Checkout = new CheckoutModel;
  96. // 订单结算api参数
  97. $params = $Checkout->setParam($this->getParam([
  98. 'cart_ids' => '',
  99. ]));
  100. // 商品结算信息
  101. $CartModel = new CartModel($this->user);
  102. // 购物车商品列表
  103. $goodsList = $CartModel->getList($params['cart_ids']);
  104. // 获取订单结算信息
  105. $orderInfo = $Checkout->onCheckout($this->user, $goodsList);
  106. if ($this->request->isGet()) {
  107. return $this->renderSuccess($orderInfo);
  108. }
  109. // 创建订单
  110. if (!$Checkout->createOrder($orderInfo)) {
  111. return $this->renderError($Checkout->getError() ?: '订单创建失败');
  112. }
  113. // 移出购物车中已下单的商品
  114. $CartModel->clearAll($params['cart_ids']);
  115. // 构建微信支付请求
  116. $payment = $Checkout->onOrderPayment();
  117. // 返回状态
  118. return $this->renderSuccess([
  119. 'order_id' => $Checkout->model['order_id'], // 订单id
  120. 'pay_type' => $params['pay_type'], // 支付方式
  121. 'payment' => $payment // 微信支付参数
  122. ], ['success' => '支付成功', 'error' => '订单未支付']);
  123. }
  124. /**
  125. * 订单结算提交的参数
  126. * @param array $define
  127. * @return array
  128. */
  129. private function getParam($define = [])
  130. {
  131. return array_merge($define, $this->request->param());
  132. }
  133. }