Order.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\api\controller\sharing;
  10. use app\api\controller\Controller;
  11. use app\api\model\Setting as SettingModel;
  12. use app\api\model\sharing\Order as OrderModel;
  13. use app\api\service\sharing\order\Checkout as CheckoutModel;
  14. use app\api\validate\sharing\order\Checkout as CheckoutValidate;
  15. use app\common\enum\OrderType as OrderTypeEnum;
  16. use app\common\enum\order\PayType as PayTypeEnum;
  17. use app\common\service\qrcode\Extract as ExtractQRcode;
  18. /**
  19. * 拼团订单控制器
  20. * Class Order
  21. * @package app\api\controller
  22. */
  23. class Order extends Controller
  24. {
  25. /* @var \app\api\model\User $user */
  26. private $user;
  27. /* @var CheckoutValidate $validate */
  28. private $validate;
  29. /**
  30. * 构造方法
  31. * @throws \app\common\exception\BaseException
  32. * @throws \think\exception\DbException
  33. */
  34. public function _initialize()
  35. {
  36. parent::_initialize();
  37. // 用户信息
  38. $this->user = $this->getUser();
  39. // 验证类
  40. $this->validate = new CheckoutValidate;
  41. }
  42. /**
  43. * 订单结算台
  44. * @return array
  45. * @throws \app\common\exception\BaseException
  46. * @throws \think\Exception
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. * @throws \think\exception\DbException
  50. * @throws \Exception
  51. */
  52. public function checkout()
  53. {
  54. // 实例化结算台服务
  55. $Checkout = new CheckoutModel;
  56. // 订单结算api参数
  57. $params = $Checkout->setParam($this->getParam([
  58. 'goods_id' => 0,
  59. 'goods_num' => 0,
  60. 'goods_sku_id' => '',
  61. ]));
  62. // 表单验证
  63. if (!$this->validate->scene('buyNow')->check($params)) {
  64. return $this->renderError($this->validate->getError());
  65. }
  66. // 立即购买:获取订单商品列表
  67. $model = new OrderModel;
  68. $goodsList = $model->getOrderGoodsListByNow($params);
  69. // 获取订单确认信息
  70. $orderInfo = $Checkout->onCheckout($this->user, $goodsList);
  71. if ($this->request->isGet()) {
  72. return $this->renderSuccess($orderInfo);
  73. }
  74. // 订单结算提交
  75. if ($Checkout->hasError()) {
  76. return $this->renderError($Checkout->getError());
  77. }
  78. // 创建订单
  79. if (!$Checkout->createOrder($orderInfo)) {
  80. return $this->renderError($Checkout->getError() ?: '订单创建失败');
  81. }
  82. // 构建微信支付请求
  83. $payment = $model->onOrderPayment($this->user, $Checkout->model, $params['pay_type']);
  84. // 返回结算信息
  85. return $this->renderSuccess([
  86. 'order_id' => $Checkout->model['order_id'], // 订单id
  87. 'pay_type' => $params['pay_type'], // 支付方式
  88. 'payment' => $payment // 微信支付参数
  89. ], ['success' => '支付成功', 'error' => '订单未支付']);
  90. }
  91. /**
  92. * 订单结算提交的参数
  93. * @param array $define
  94. * @return array
  95. */
  96. private function getParam($define = [])
  97. {
  98. return array_merge($define, $this->request->param());
  99. }
  100. /**
  101. * 我的拼团订单列表
  102. * @param $dataType
  103. * @return array
  104. * @throws \think\exception\DbException
  105. */
  106. public function lists($dataType)
  107. {
  108. $model = new OrderModel;
  109. $list = $model->getList($this->user['user_id'], $dataType);
  110. return $this->renderSuccess(compact('list'));
  111. }
  112. /**
  113. * 拼团订单详情信息
  114. * @param $order_id
  115. * @return array
  116. * @throws \app\common\exception\BaseException
  117. */
  118. public function detail($order_id)
  119. {
  120. // 订单详情
  121. $model = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
  122. // 该订单是否允许申请售后
  123. $model['isAllowRefund'] = $model->isAllowRefund();
  124. return $this->renderSuccess([
  125. 'order' => $model, // 订单详情
  126. 'setting' => [
  127. // 积分名称
  128. 'points_name' => SettingModel::getPointsName(),
  129. ],
  130. ]);
  131. }
  132. /**
  133. * 获取物流信息
  134. * @param $order_id
  135. * @return array
  136. * @throws \app\common\exception\BaseException
  137. */
  138. public function express($order_id)
  139. {
  140. // 订单信息
  141. $order = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
  142. if (!$order['express_no']) {
  143. return $this->renderError('没有物流信息');
  144. }
  145. // 获取物流信息
  146. /* @var \app\store\model\Express $model */
  147. $model = $order['express'];
  148. $express = $model->dynamic($model['express_name'], $model['express_code'], $order['express_no']);
  149. if ($express === false) {
  150. return $this->renderError($model->getError());
  151. }
  152. return $this->renderSuccess(compact('express'));
  153. }
  154. /**
  155. * 取消订单
  156. * @param $order_id
  157. * @return array
  158. * @throws \app\common\exception\BaseException
  159. */
  160. public function cancel($order_id)
  161. {
  162. $model = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
  163. if ($model->cancel($this->user)) {
  164. return $this->renderSuccess($model->getError() ?: '订单取消成功');
  165. }
  166. return $this->renderError($model->getError() ?: '订单取消失败');
  167. }
  168. /**
  169. * 确认收货
  170. * @param $order_id
  171. * @return array
  172. * @throws \app\common\exception\BaseException
  173. */
  174. public function receipt($order_id)
  175. {
  176. $model = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
  177. if ($model->receipt()) {
  178. return $this->renderSuccess();
  179. }
  180. return $this->renderError($model->getError());
  181. }
  182. /**
  183. * 立即支付
  184. * @param int $order_id 订单id
  185. * @param int $payType 支付方式
  186. * @return array
  187. * @throws \app\common\exception\BaseException
  188. * @throws \think\Exception
  189. * @throws \think\exception\DbException
  190. */
  191. public function pay($order_id, $payType = PayTypeEnum::WECHAT)
  192. {
  193. // 订单详情
  194. $model = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
  195. // 订单支付事件
  196. if (!$model->onPay($payType)) {
  197. return $this->renderError($model->getError() ?: '订单支付失败');
  198. }
  199. // 构建微信支付请求
  200. $payment = $model->onOrderPayment($this->user, $model, $payType);
  201. // 支付状态提醒
  202. return $this->renderSuccess([
  203. 'order_id' => $model['order_id'], // 订单id
  204. 'pay_type' => $payType, // 支付方式
  205. 'payment' => $payment // 微信支付参数
  206. ], ['success' => '支付成功', 'error' => '订单未支付']);
  207. }
  208. /**
  209. * 获取订单核销二维码
  210. * @param $order_id
  211. * @return array
  212. * @throws \app\common\exception\BaseException
  213. * @throws \think\exception\DbException
  214. */
  215. public function extractQrcode($order_id)
  216. {
  217. // 订单详情
  218. $order = OrderModel::getUserOrderDetail($order_id, $this->user['user_id']);
  219. // 判断是否为待核销订单
  220. if (!$order->checkExtractOrder($order)) {
  221. return $this->renderError($order->getError());
  222. }
  223. $Qrcode = new ExtractQRcode(
  224. $this->wxapp_id,
  225. $this->user,
  226. $order_id,
  227. OrderTypeEnum::SHARING
  228. );
  229. return $this->renderSuccess([
  230. 'qrcode' => $Qrcode->getImage(),
  231. ]);
  232. }
  233. }