Order.php 5.8 KB

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