Order.php 3.9 KB

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