Order.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace app\api\controller\bargain;
  3. use app\api\controller\Controller;
  4. use app\api\model\bargain\Task as TaskModel;
  5. use app\api\model\bargain\Setting as SettingModel;
  6. use app\api\service\order\Checkout as CheckoutModel;
  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. 'task_id' => 0
  40. ]));
  41. // 获取砍价任务详情
  42. $task = TaskModel::detail($params['task_id']);
  43. // 获取砍价商品信息
  44. $goodsList = $task->getTaskGoods($params['task_id']);
  45. if ($goodsList === false) {
  46. return $this->renderError($task->getError());
  47. }
  48. // 设置订单来源
  49. $Checkout->setOrderSource([
  50. 'source' => OrderSourceEnum::BARGAIN,
  51. 'source_id' => $params['task_id'],
  52. ]);
  53. // 砍价商品不参与 等级折扣和优惠券折扣
  54. $Checkout->setCheckoutRule([
  55. 'is_user_grade' => false,
  56. 'is_coupon' => false,
  57. 'is_use_points' => false,
  58. 'is_dealer' => SettingModel::getIsDealer(),
  59. ]);
  60. // 获取订单结算信息
  61. $orderInfo = $Checkout->onCheckout($this->user, $goodsList);
  62. if ($this->request->isGet()) {
  63. return $this->renderSuccess($orderInfo);
  64. }
  65. // submit:订单结算提交
  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. $task->setTaskEnd();
  75. // 构建微信支付请求
  76. $payment = $Checkout->onOrderPayment();
  77. // 支付状态提醒
  78. $message = ['success' => '支付成功', 'error' => '订单未支付'];
  79. return $this->renderSuccess([
  80. 'order_id' => $Checkout->model['order_id'], // 订单id
  81. 'pay_type' => $params['pay_type'], // 支付方式
  82. 'payment' => $payment // 微信支付参数
  83. ], $message);
  84. }
  85. /**
  86. * 订单结算提交的参数
  87. * @param array $define
  88. * @return array
  89. */
  90. private function getParam($define = [])
  91. {
  92. return array_merge($define, $this->request->param());
  93. }
  94. }