Order.php 5.4 KB

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