Order.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\bargain;
  10. use app\api\controller\Controller;
  11. use app\api\model\bargain\Task as TaskModel;
  12. use app\api\model\bargain\Setting as SettingModel;
  13. use app\api\service\order\Checkout as CheckoutModel;
  14. use app\common\enum\order\OrderSource as OrderSourceEnum;
  15. class Order extends Controller
  16. {
  17. /* @var \app\api\model\User $user */
  18. private $user;
  19. /**
  20. * 构造方法
  21. * @throws \app\common\exception\BaseException
  22. * @throws \think\exception\DbException
  23. */
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. // 用户信息
  28. $this->user = $this->getUser();
  29. }
  30. /**
  31. * 砍价订单结算
  32. * @return array
  33. * @throws \app\common\exception\BaseException
  34. * @throws \think\Exception
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\ModelNotFoundException
  37. * @throws \think\exception\DbException
  38. * @throws \Exception
  39. */
  40. public function checkout()
  41. {
  42. // 实例化结算台服务
  43. $Checkout = new CheckoutModel;
  44. // 订单结算api参数
  45. $params = $Checkout->setParam($this->getParam([
  46. 'task_id' => 0
  47. ]));
  48. // 获取砍价任务详情
  49. $task = TaskModel::detail($params['task_id']);
  50. // 获取砍价商品信息
  51. $goodsList = $task->getTaskGoods($params['task_id']);
  52. if ($goodsList === false) {
  53. return $this->renderError($task->getError());
  54. }
  55. // 设置订单来源
  56. $Checkout->setOrderSource([
  57. 'source' => OrderSourceEnum::BARGAIN,
  58. 'source_id' => $params['task_id'],
  59. ]);
  60. // 砍价商品不参与 等级折扣和优惠券折扣
  61. $Checkout->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. $task->setTaskEnd();
  82. // 构建微信支付请求
  83. $payment = $Checkout->onOrderPayment();
  84. // 支付状态提醒
  85. $message = ['success' => '支付成功', 'error' => '订单未支付'];
  86. return $this->renderSuccess([
  87. 'order_id' => $Checkout->model['order_id'], // 订单id
  88. 'pay_type' => $params['pay_type'], // 支付方式
  89. 'payment' => $payment // 微信支付参数
  90. ], $message);
  91. }
  92. /**
  93. * 订单结算提交的参数
  94. * @param array $define
  95. * @return array
  96. */
  97. private function getParam($define = [])
  98. {
  99. return array_merge($define, $this->request->param());
  100. }
  101. }