Order.php 7.5 KB

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