Order.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\api\controller\Controller;
  4. use app\api\model\Order as OrderModel;
  5. use app\api\model\Setting as SettingModel;
  6. use app\common\enum\OrderType as OrderTypeEnum;
  7. use app\common\enum\order\PayType as PayTypeEnum;
  8. use app\common\service\qrcode\Extract as ExtractQRcode;
  9. /**
  10. * 用户订单管理
  11. * Class Order
  12. * @package app\api\controller\user
  13. */
  14. class Order extends Controller
  15. {
  16. /* @var \app\api\model\User $user */
  17. private $user;
  18. /**
  19. * 构造方法
  20. * @throws \app\common\exception\BaseException
  21. * @throws \think\exception\DbException
  22. */
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->user = $this->getUser(); // 用户信息
  27. }
  28. /**
  29. * 我的订单列表
  30. * @param $dataType
  31. * @return array
  32. * @throws \think\exception\DbException
  33. */
  34. public function lists($dataType)
  35. {
  36. $model = new OrderModel;
  37. $list = $model->getList($this->user['user_id'], $dataType);
  38. return $this->renderSuccess(compact('list'));
  39. }
  40. /**
  41. * 订单详情信息
  42. * @param $order_id
  43. * @return array
  44. * @throws \app\common\exception\BaseException
  45. * @throws \think\exception\DbException
  46. */
  47. public function detail($order_id)
  48. {
  49. // 订单详情
  50. $model = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
  51. // 该订单是否允许申请售后
  52. $model['isAllowRefund'] = $model->isAllowRefund();
  53. return $this->renderSuccess([
  54. 'order' => $model, // 订单详情
  55. 'setting' => [
  56. // 积分名称
  57. 'points_name' => SettingModel::getPointsName(),
  58. ],
  59. ]);
  60. }
  61. /**
  62. * 获取物流信息
  63. * @param $order_id
  64. * @return array
  65. * @throws \app\common\exception\BaseException
  66. * @throws \think\exception\DbException
  67. */
  68. public function express($order_id)
  69. {
  70. // 订单信息
  71. $order = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
  72. if (!$order['express_no']) {
  73. return $this->renderError('没有物流信息');
  74. }
  75. // 获取物流信息
  76. /* @var \app\store\model\Express $model */
  77. $model = $order['express'];
  78. $express = $model->dynamic($model['express_name'], $model['express_code'], $order['express_no']);
  79. if ($express === false) {
  80. return $this->renderError($model->getError());
  81. }
  82. return $this->renderSuccess(compact('express'));
  83. }
  84. /**
  85. * 取消订单
  86. * @param $order_id
  87. * @return array
  88. * @throws \Exception
  89. * @throws \app\common\exception\BaseException
  90. * @throws \think\exception\DbException
  91. */
  92. public function cancel($order_id)
  93. {
  94. $model = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
  95. if ($model->cancel($this->user)) {
  96. return $this->renderSuccess($model->getError() ?: '订单取消成功');
  97. }
  98. return $this->renderError($model->getError() ?: '订单取消失败');
  99. }
  100. /**
  101. * 确认收货
  102. * @param $order_id
  103. * @return array
  104. * @throws \app\common\exception\BaseException
  105. * @throws \think\exception\DbException
  106. */
  107. public function receipt($order_id)
  108. {
  109. $model = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
  110. if ($model->receipt()) {
  111. return $this->renderSuccess();
  112. }
  113. return $this->renderError($model->getError());
  114. }
  115. /**
  116. * 立即支付
  117. * @param int $order_id 订单id
  118. * @param int $payType 支付方式
  119. * @return array
  120. * @throws \app\common\exception\BaseException
  121. * @throws \think\Exception
  122. * @throws \think\exception\DbException
  123. */
  124. public function pay($order_id, $payType = PayTypeEnum::WECHAT)
  125. {
  126. // 获取订单详情
  127. $model = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
  128. // 订单支付事件
  129. if (!$model->onPay($payType)) {
  130. return $this->renderError($model->getError() ?: '订单支付失败');
  131. }
  132. // 构建微信支付请求
  133. $payment = $model->onOrderPayment($this->user, $model, $payType);
  134. // 支付状态提醒
  135. return $this->renderSuccess([
  136. 'order_id' => $model['order_id'], // 订单id
  137. 'pay_type' => $payType, // 支付方式
  138. 'payment' => $payment // 微信支付参数
  139. ], ['success' => '支付成功', 'error' => '订单未支付']);
  140. }
  141. /**
  142. * 获取订单核销二维码
  143. * @param $order_id
  144. * @return array
  145. * @throws \app\common\exception\BaseException
  146. * @throws \think\exception\DbException
  147. */
  148. public function extractQrcode($order_id)
  149. {
  150. // 订单详情
  151. $order = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
  152. // 判断是否为待核销订单
  153. if (!$order->checkExtractOrder($order)) {
  154. return $this->renderError($order->getError());
  155. }
  156. $Qrcode = new ExtractQRcode(
  157. $this->wxapp_id,
  158. $this->user,
  159. $order_id,
  160. OrderTypeEnum::MASTER
  161. );
  162. return $this->renderSuccess([
  163. 'qrcode' => $Qrcode->getImage(),
  164. ]);
  165. }
  166. }