Order.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\controller;
  13. use app\api\model\Order as OrderModel;
  14. use app\api\model\Setting as SettingModel;
  15. use app\store\model\Express as ExpressModel;
  16. use app\common\enum\order\PayType as OrderPayTypeEnum;
  17. use app\common\exception\BaseException;
  18. /**
  19. * 我的订单控制器
  20. * Class Order
  21. * @package app\api\controller
  22. */
  23. class Order extends Controller
  24. {
  25. /**
  26. * 获取当前用户待处理的订单数量
  27. * @return array|\think\response\Json
  28. * @throws BaseException
  29. */
  30. public function todoCounts()
  31. {
  32. $model = new OrderModel;
  33. $counts = $model->getTodoCounts();
  34. return $this->renderSuccess(compact('counts'));
  35. }
  36. /**
  37. * 我的订单列表
  38. * @param string $dataType 订单类型 (all全部 payment待付款 received待发货 deliver待收货 comment待评价)
  39. * @return array|\think\response\Json
  40. * @throws BaseException
  41. * @throws \think\db\exception\DbException
  42. */
  43. public function list(string $dataType)
  44. {
  45. $model = new OrderModel;
  46. $list = $model->getList($dataType);
  47. return $this->renderSuccess(compact('list'));
  48. }
  49. /**
  50. * 订单详情信息
  51. * @param int $orderId 订单ID
  52. * @return array|\think\response\Json
  53. * @throws BaseException
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function detail(int $orderId)
  59. {
  60. // 订单详情
  61. $model = OrderModel::getUserOrderDetail($orderId);
  62. return $this->renderSuccess([
  63. 'order' => $model, // 订单详情
  64. 'setting' => [
  65. // 积分名称
  66. 'points_name' => SettingModel::getPointsName(),
  67. ],
  68. ]);
  69. }
  70. /**
  71. * 获取物流信息
  72. * @param int $orderId 订单ID
  73. * @return array|\think\response\Json
  74. * @throws BaseException
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function express(int $orderId)
  80. {
  81. // 订单信息
  82. $order = OrderModel::getDetail($orderId);
  83. if (!$order['express_no']) {
  84. return $this->renderError('没有物流信息');
  85. }
  86. // 获取物流信息
  87. $model = ExpressModel::detail($order['express_id']);
  88. $express = $model->dynamic($model['express_name'], $model['kuaidi100_code'], $order['express_no']);
  89. if ($express === false) {
  90. return $this->renderError($model->getError());
  91. }
  92. return $this->renderSuccess(compact('express'));
  93. }
  94. /**
  95. * 取消订单
  96. * @param int $orderId
  97. * @return array|\think\response\Json
  98. * @throws BaseException
  99. */
  100. public function cancel(int $orderId)
  101. {
  102. $model = OrderModel::getDetail($orderId);
  103. if ($model->cancel()) {
  104. return $this->renderSuccess('订单取消成功');
  105. }
  106. return $this->renderError($model->getError() ?: '订单取消失败');
  107. }
  108. /**
  109. * 确认收货
  110. * @param int $orderId
  111. * @return array|\think\response\Json
  112. * @throws BaseException
  113. */
  114. public function receipt(int $orderId)
  115. {
  116. $model = OrderModel::getDetail($orderId);
  117. if ($model->receipt()) {
  118. return $this->renderSuccess('确认收货成功');
  119. }
  120. return $this->renderError($model->getError());
  121. }
  122. /**
  123. * 立即支付
  124. * @param int $orderId 订单ID
  125. * @param int $payType 支付方式
  126. * @return array|\think\response\Json
  127. * @throws BaseException
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\DbException
  130. * @throws \think\db\exception\ModelNotFoundException
  131. */
  132. public function pay(int $orderId, int $payType = OrderPayTypeEnum::WECHAT)
  133. {
  134. // 获取订单详情
  135. $model = OrderModel::getUserOrderDetail($orderId);
  136. // 订单支付事件
  137. if (!$model->onPay($payType)) {
  138. return $this->renderError($model->getError() ?: '订单支付失败');
  139. }
  140. // 构建微信支付请求
  141. $payment = $model->onOrderPayment($model, $payType);
  142. // 支付状态提醒
  143. return $this->renderSuccess([
  144. 'order_id' => $model['order_id'], // 订单id
  145. 'pay_type' => $payType, // 支付方式
  146. 'payment' => $payment // 微信支付参数
  147. ]);
  148. }
  149. }