Order.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. $order['express_no'] = 'AT354585357CN';
  114. // 获取物流信息
  115. $model = new ExpressModel();
  116. $express = $model->dynamicUsps($order['express_no']);
  117. if ($express === false) {
  118. return $this->renderError($model->getError());
  119. }
  120. return $this->renderSuccess(compact('express'));
  121. }
  122. /**
  123. * 取消订单
  124. * @return Json
  125. * @throws BaseException
  126. */
  127. public function cancel(): Json
  128. {
  129. $userId = Session::get('user_id');
  130. if (empty($userId)) {
  131. return $this->renderJson(config('status.not_logged'), 'Log in please!');
  132. }
  133. $orderId = $this->request->param('orderId');
  134. if (empty($orderId)){
  135. return $this->renderError('Something error!');
  136. }
  137. $model = OrderModel::getDetail(intval($orderId));
  138. if ($model->cancel()) {
  139. return $this->renderSuccess('订单取消成功');
  140. }
  141. return $this->renderError($model->getError() ?: '订单取消失败');
  142. }
  143. /**
  144. * 确认收货
  145. * @param int $orderId
  146. * @return Json
  147. * @throws BaseException
  148. */
  149. public function receipt(int $orderId): Json
  150. {
  151. $model = OrderModel::getDetail($orderId);
  152. if ($model->receipt()) {
  153. return $this->renderSuccess('Confirm receipt of goods successfully');
  154. }
  155. return $this->renderError($model->getError());
  156. }
  157. /**
  158. * 立即支付
  159. * @param int $orderId 订单ID
  160. * @param int $payType 支付方式
  161. * @return Json
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\DbException
  164. * @throws \think\db\exception\ModelNotFoundException
  165. * @throws BaseException
  166. */
  167. public function pay(int $orderId, int $payType = OrderPayTypeEnum::WECHAT): Json
  168. {
  169. // 获取订单详情
  170. $model = OrderModel::getUserOrderDetail($orderId);
  171. // 订单支付事件
  172. if (!$model->onPay($payType)) {
  173. return $this->renderError($model->getError() ?: 'Order creation failed, please try again later');
  174. }
  175. // 构建微信支付请求
  176. $payment = $model->onOrderPayment($model, $payType);
  177. if ($payType == OrderPayTypeEnum::POINTS) {
  178. if (!$payment['flag']) {
  179. return $this->renderError($payment['message'] ?? 'Redemption failed, please try again later');
  180. }
  181. }
  182. // 支付状态提醒
  183. return $this->renderSuccess([
  184. 'order_id' => $model['order_id'], // 订单id
  185. 'pay_type' => $payType, // 支付方式
  186. 'payment' => $payment // 微信支付参数
  187. ]);
  188. }
  189. }