Checkout.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\controller;
  13. use think\response\Json;
  14. use app\api\model\Order as OrderModel;
  15. use app\api\service\Cart as CartService;
  16. use app\api\service\order\Checkout as CheckoutService;
  17. use app\api\validate\order\Checkout as CheckoutValidate;
  18. /**
  19. * 订单结算控制器
  20. * Class Checkout
  21. * @package app\api\controller
  22. */
  23. class Checkout extends Controller
  24. {
  25. // 结算台验证器
  26. private ?CheckoutValidate $validate = null;
  27. /**
  28. * 结算台订单信息
  29. * @param string $mode
  30. * @return Json
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. * @throws \cores\exception\BaseException
  35. */
  36. public function order(string $mode = 'buyNow'): Json
  37. {
  38. if ($mode === 'buyNow') {
  39. return $this->buyNow();
  40. } elseif ($mode === 'cart') {
  41. return $this->cart();
  42. }
  43. return $this->renderError('结算模式不合法');
  44. }
  45. /**
  46. * 订单提交
  47. * @param string $mode
  48. * @return Json
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. * @throws \cores\exception\BaseException
  53. */
  54. public function submit(string $mode = 'buyNow'): Json
  55. {
  56. return $this->order($mode);
  57. }
  58. /**
  59. * 订单确认-立即购买
  60. * @return Json
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. * @throws \cores\exception\BaseException
  65. */
  66. private function buyNow(): Json
  67. {
  68. // 实例化结算台服务
  69. $Checkout = new CheckoutService;
  70. // 订单结算api参数
  71. $params = $Checkout->setParam($this->getParam([
  72. 'goodsId' => 0,
  73. 'goodsSkuId' => '',
  74. 'goodsNum' => 0,
  75. ]));
  76. // 表单验证
  77. if (!$this->getValidate()->scene('buyNow')->check($params)) {
  78. return $this->renderError($this->getValidate()->getError(), ['isCreated' => false]);
  79. }
  80. // 立即购买:获取订单商品列表
  81. $model = new OrderModel;
  82. $goodsList = $model->getOrderGoodsListByNow(
  83. (int)$params['goodsId'],
  84. (string)$params['goodsSkuId'],
  85. (int)$params['goodsNum']
  86. );
  87. // 获取订单确认信息
  88. $orderInfo = $Checkout->onCheckout($goodsList);
  89. if ($this->request->isGet()) {
  90. return $this->renderSuccess([
  91. 'order' => $orderInfo,
  92. 'personal' => $Checkout->getPersonal(),
  93. 'setting' => $Checkout->getSetting(),
  94. ]);
  95. }
  96. // 验证订单是否存在错误
  97. if ($Checkout->hasError()) {
  98. return $this->renderError($Checkout->getError(), ['isCreated' => false]);
  99. }
  100. // 创建订单
  101. if (!$Checkout->createOrder($orderInfo)) {
  102. return $this->renderError($Checkout->getError() ?: '订单创建失败', ['isCreated' => false]);
  103. }
  104. // 返回状态
  105. return $this->renderSuccess(['orderId' => $Checkout->model['order_id']], '订单创建成功');
  106. }
  107. /**
  108. * 订单确认-购物车结算
  109. * @return Json
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\DbException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. * @throws \cores\exception\BaseException
  114. */
  115. private function cart(): Json
  116. {
  117. // 实例化结算台服务
  118. $Checkout = new CheckoutService;
  119. // 订单结算api参数
  120. $params = $Checkout->setParam($this->getParam());
  121. // 购物车ID集
  122. $cartIds = $this->getCartIds();
  123. // 商品结算信息
  124. $CartModel = new CartService;
  125. // 购物车商品列表
  126. $goodsList = $CartModel->getOrderGoodsList($cartIds);
  127. // 获取订单结算信息
  128. $orderInfo = $Checkout->onCheckout($goodsList);
  129. if ($this->request->isGet()) {
  130. return $this->renderSuccess([
  131. 'order' => $orderInfo,
  132. 'personal' => $Checkout->getPersonal(),
  133. 'setting' => $Checkout->getSetting(),
  134. ]);
  135. }
  136. // 验证订单是否存在错误
  137. if ($Checkout->hasError()) {
  138. return $this->renderError($Checkout->getError(), ['isCreated' => false]);
  139. }
  140. // 创建订单
  141. if (!$Checkout->createOrder($orderInfo)) {
  142. return $this->renderError($Checkout->getError() ?: '订单创建失败');
  143. }
  144. // 移出购物车中已下单的商品
  145. $CartModel->clear($cartIds);
  146. // 返回状态
  147. return $this->renderSuccess(['orderId' => $Checkout->model['order_id']], '订单创建成功');
  148. }
  149. /**
  150. * 获取结算台验证器
  151. * @return CheckoutValidate
  152. */
  153. private function getValidate(): CheckoutValidate
  154. {
  155. if (is_null($this->validate)) {
  156. $this->validate = new CheckoutValidate;
  157. }
  158. return $this->validate;
  159. }
  160. /**
  161. * 获取购物车ID集
  162. * @return false|string[]
  163. */
  164. private function getCartIds()
  165. {
  166. $cartIds = $this->request->param('cartIds');
  167. return explode(',', $cartIds);
  168. }
  169. /**
  170. * 订单结算提交的参数
  171. * @param array $define
  172. * @return array
  173. */
  174. private function getParam(array $define = []): array
  175. {
  176. return array_merge($define, $this->request->param());
  177. }
  178. }