Order.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\index\controller;
  13. use app\index\model\Order as OrderModel;
  14. use app\index\model\Setting as SettingModel;
  15. use app\store\model\Express as ExpressModel;
  16. use app\common\enum\order\PayType as OrderPayTypeEnum;
  17. use cores\exception\BaseException;
  18. use think\facade\Session;
  19. use think\response\Json;
  20. /**
  21. * 我的订单控制器
  22. * Class Order
  23. * @package app\api\controller
  24. */
  25. class Order extends Controller
  26. {
  27. /**
  28. * 获取当前用户待处理的订单数量
  29. * @return Json
  30. * @throws BaseException
  31. */
  32. public function todoCounts(): Json
  33. {
  34. $model = new OrderModel;
  35. $counts = $model->getTodoCounts();
  36. return $this->renderSuccess(compact('counts'));
  37. }
  38. /**
  39. * 我的订单列表
  40. * @param string $dataType 订单类型 (all全部 payment待付款 received待发货 deliver待收货 comment待评价)
  41. * @return Json
  42. * @throws \think\db\exception\DbException
  43. * @throws BaseException
  44. */
  45. public function list(string $dataType): Json
  46. {
  47. $model = new OrderModel;
  48. $list = $model->getList($dataType);
  49. return $this->renderSuccess(compact('list'));
  50. }
  51. /**
  52. * 订单详情信息
  53. * @param int $orderId 订单ID
  54. * @return Json
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. * @throws BaseException
  59. */
  60. public function detail(int $orderId): Json
  61. {
  62. // 订单详情
  63. $model = OrderModel::getUserOrderDetail($orderId);
  64. return $this->renderSuccess([
  65. 'order' => $model, // 订单详情
  66. 'setting' => [
  67. // 积分名称
  68. 'points_name' => SettingModel::getPointsName(),
  69. ],
  70. ]);
  71. }
  72. /**
  73. * 获取物流信息
  74. * @param int $orderId 订单ID
  75. * @return Json
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. * @throws BaseException
  80. */
  81. public function express(int $orderId): Json
  82. {
  83. // 订单信息
  84. $order = OrderModel::getDetail($orderId);
  85. if (!$order['express_no']) {
  86. return $this->renderError('没有物流信息');
  87. }
  88. // 获取物流信息
  89. $model = ExpressModel::detail($order['express_id']);
  90. $express = $model->dynamic($model['express_name'], $model['kuaidi100_code'], $order['express_no']);
  91. if ($express === false) {
  92. return $this->renderError($model->getError());
  93. }
  94. return $this->renderSuccess(compact('express'));
  95. }
  96. /**
  97. * 获取物流信息
  98. * @param int $orderId 订单ID
  99. * @return Json
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. * @throws BaseException
  104. */
  105. public function expressUsps(int $orderId): Json
  106. {
  107. // 订单信息
  108. $order = OrderModel::getDetail($orderId);
  109. if (!$order['express_no']) {
  110. return $this->renderError('No logistics information');
  111. }
  112. $expressUserId = config('usps.exp_user_id');
  113. // 获取物流信息
  114. $model = new ExpressModel();
  115. $express = $model->dynamicUsps($expressUserId, $order['express_no']);
  116. if ($express === false) {
  117. return $this->renderError($model->getError());
  118. }
  119. return $this->renderSuccess(compact('express'));
  120. }
  121. /**
  122. * 取消订单
  123. * @return Json
  124. * @throws BaseException
  125. */
  126. public function cancel(): Json
  127. {
  128. $userId = Session::get('user_id');
  129. if (empty($userId)) {
  130. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  131. }
  132. $orderId = $this->request->param('orderId');
  133. if (empty($orderId)){
  134. return $this->renderError('Something error!');
  135. }
  136. $model = OrderModel::getDetail(intval($orderId));
  137. if ($model->cancel()) {
  138. return $this->renderSuccess('订单取消成功');
  139. }
  140. return $this->renderError($model->getError() ?: '订单取消失败');
  141. }
  142. /**
  143. * 确认收货
  144. * @param int $orderId
  145. * @return Json
  146. * @throws BaseException
  147. */
  148. public function receipt(int $orderId): Json
  149. {
  150. $model = OrderModel::getDetail($orderId);
  151. if ($model->receipt()) {
  152. return $this->renderSuccess('Confirm receipt of goods successfully');
  153. }
  154. return $this->renderError($model->getError());
  155. }
  156. /**
  157. * 立即支付
  158. * @param int $orderId 订单ID
  159. * @param int $payType 支付方式
  160. * @return Json
  161. * @throws \think\db\exception\DataNotFoundException
  162. * @throws \think\db\exception\DbException
  163. * @throws \think\db\exception\ModelNotFoundException
  164. * @throws BaseException
  165. */
  166. public function pay(int $orderId, int $payType = OrderPayTypeEnum::WECHAT): Json
  167. {
  168. // 获取订单详情
  169. $model = OrderModel::getUserOrderDetail($orderId);
  170. // 订单支付事件
  171. if (!$model->onPay($payType)) {
  172. return $this->renderError($model->getError() ?: 'Order creation failed, please try again later');
  173. }
  174. // 构建微信支付请求
  175. $payment = $model->onOrderPayment($model, $payType);
  176. if ($payType == OrderPayTypeEnum::POINTS) {
  177. if (!$payment['flag']) {
  178. return $this->renderError($payment['message'] ?? 'Redemption failed, please try again later');
  179. }
  180. }
  181. // 支付状态提醒
  182. return $this->renderSuccess([
  183. 'order_id' => $model['order_id'], // 订单id
  184. 'pay_type' => $payType, // 支付方式
  185. 'payment' => $payment // 微信支付参数
  186. ]);
  187. }
  188. }