Order.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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
  98. * @return Json
  99. * @throws BaseException
  100. */
  101. public function receipt(int $orderId): Json
  102. {
  103. $model = OrderModel::getDetail($orderId);
  104. if ($model->receipt()) {
  105. return $this->renderSuccess('确认收货成功');
  106. }
  107. return $this->renderError($model->getError());
  108. }
  109. /**
  110. * 立即支付
  111. * @param int $orderId 订单ID
  112. * @param int $payType 支付方式
  113. * @return Json
  114. * @throws \think\db\exception\DataNotFoundException
  115. * @throws \think\db\exception\DbException
  116. * @throws \think\db\exception\ModelNotFoundException
  117. * @throws BaseException
  118. */
  119. public function pay(int $orderId, int $payType = OrderPayTypeEnum::WECHAT): Json
  120. {
  121. // 获取订单详情
  122. $model = OrderModel::getUserOrderDetail($orderId);
  123. // 订单支付事件
  124. if (!$model->onPay($payType)) {
  125. return $this->renderError($model->getError() ?: '订单支付失败');
  126. }
  127. // 构建微信支付请求
  128. $payment = $model->onOrderPayment($model, $payType);
  129. // 支付状态提醒
  130. return $this->renderSuccess([
  131. 'order_id' => $model['order_id'], // 订单id
  132. 'pay_type' => $payType, // 支付方式
  133. 'payment' => $payment // 微信支付参数
  134. ]);
  135. }
  136. }