Order.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace app\api\controller\sharp;
  3. use app\api\controller\Controller;
  4. use app\api\model\sharp\Setting as SettingModel;
  5. use app\api\service\order\Checkout as CheckoutModel;
  6. use app\api\service\sharp\Active as ActiveService;
  7. use app\common\enum\order\OrderSource as OrderSourceEnum;
  8. class Order extends Controller
  9. {
  10. /* @var \app\api\model\User $user */
  11. private $user;
  12. /**
  13. * 构造方法
  14. * @throws \app\common\exception\BaseException
  15. * @throws \think\exception\DbException
  16. */
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. // 用户信息
  21. $this->user = $this->getUser();
  22. }
  23. /**
  24. * 秒杀订单结算
  25. * @return array
  26. * @throws \app\common\exception\BaseException
  27. * @throws \think\Exception
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. * @throws \think\exception\DbException
  31. * @throws \Exception
  32. */
  33. public function checkout()
  34. {
  35. // 实例化结算台服务
  36. $Checkout = new CheckoutModel;
  37. // 订单结算api参数
  38. $params = $Checkout->setParam($this->getParam([
  39. 'active_time_id' => 0,
  40. 'sharp_goods_id' => 0,
  41. 'goods_sku_id' => '',
  42. 'goods_num' => 0,
  43. ]));
  44. // 获取秒杀商品信息
  45. $service = new ActiveService;
  46. $goodsList = $service->getCheckoutGoodsList(
  47. $params['active_time_id'],
  48. $params['sharp_goods_id'],
  49. $params['goods_sku_id'],
  50. $params['goods_num']
  51. );
  52. if ($goodsList === false) {
  53. return $this->renderError($service->getError());
  54. }
  55. // 设置订单来源
  56. $Checkout->setOrderSource([
  57. 'source' => OrderSourceEnum::SHARP,
  58. 'source_id' => $params['active_time_id'],
  59. ])
  60. // 秒杀商品不参与 等级折扣和优惠券折扣
  61. ->setCheckoutRule([
  62. 'is_user_grade' => false,
  63. 'is_coupon' => false,
  64. 'is_use_points' => false,
  65. 'is_dealer' => SettingModel::getIsDealer(),
  66. ]);
  67. // 获取订单结算信息
  68. $orderInfo = $Checkout->onCheckout($this->user, $goodsList);
  69. if ($this->request->isGet()) {
  70. return $this->renderSuccess($orderInfo);
  71. }
  72. // submit:订单结算提交
  73. if ($Checkout->hasError()) {
  74. return $this->renderError($Checkout->getError());
  75. }
  76. // 创建订单
  77. if (!$Checkout->createOrder($orderInfo)) {
  78. return $this->renderError($Checkout->getError() ?: '订单创建失败');
  79. }
  80. // 构建微信支付请求
  81. $payment = $Checkout->onOrderPayment();
  82. // 支付状态提醒
  83. $message = ['success' => '支付成功', 'error' => '订单未支付'];
  84. return $this->renderSuccess([
  85. 'order_id' => $Checkout->model['order_id'], // 订单id
  86. 'pay_type' => $params['pay_type'], // 支付方式
  87. 'payment' => $payment // 微信支付参数
  88. ], $message);
  89. }
  90. /**
  91. * 订单结算提交的参数
  92. * @param array $define
  93. * @return array
  94. */
  95. private function getParam($define = [])
  96. {
  97. return array_merge($define, $this->request->param());
  98. }
  99. }