Order.php 3.6 KB

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