StoreOrder.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\store\order;
  12. use app\validate\api\UserReceiptValidate;
  13. use crmeb\basic\BaseController;
  14. use app\common\repositories\store\order\StoreCartRepository;
  15. use app\common\repositories\store\order\StoreGroupOrderRepository;
  16. use app\common\repositories\store\order\StoreOrderRepository;
  17. use crmeb\services\ExpressService;
  18. use think\App;
  19. use think\exception\ValidateException;
  20. /**
  21. * Class StoreOrder
  22. * @package app\controller\api\store\order
  23. * @author xaboy
  24. * @day 2020/6/10
  25. */
  26. class StoreOrder extends BaseController
  27. {
  28. /**
  29. * @var StoreOrderRepository
  30. */
  31. protected $repository;
  32. /**
  33. * StoreOrder constructor.
  34. * @param App $app
  35. * @param StoreOrderRepository $repository
  36. */
  37. public function __construct(App $app, StoreOrderRepository $repository)
  38. {
  39. parent::__construct($app);
  40. $this->repository = $repository;
  41. }
  42. /**
  43. * @param StoreCartRepository $cartRepository
  44. * @return mixed
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. * @author xaboy
  49. * @day 2020/6/10
  50. */
  51. public function checkOrder(StoreCartRepository $cartRepository)
  52. {
  53. $cartId = (array)$this->request->param('cart_id', []);
  54. $addressId = (int)$this->request->param('address_id');
  55. $uid = $this->request->uid();
  56. if (!($count = count($cartId)) || $count != count($cartRepository->validIntersection($cartId, $uid)))
  57. return app('json')->fail('数据无效');
  58. $orderInfo = $this->repository->cartIdByOrderInfo($uid, [], $cartId, $addressId, true);
  59. return app('json')->success($orderInfo);
  60. }
  61. /**
  62. * @param StoreCartRepository $cartRepository
  63. * @return mixed
  64. * @author xaboy
  65. * @day 2020/6/10
  66. */
  67. public function createOrder(StoreCartRepository $cartRepository)
  68. {
  69. $cartId = (array)$this->request->param('cart_id', []);
  70. $addressId = (int)$this->request->param('address_id');
  71. $order_type = (int)$this->request->param('order_type', 0);
  72. $receipt_data = (array)$this->request->param('receipt_data', []);
  73. $coupon = (array)$this->request->param('coupon', []);
  74. $take = (array)$this->request->param('take', []);
  75. $mark = (array)$this->request->param('mark', []);
  76. $payType = $this->request->param('pay_type');
  77. $isPc = $payType === 'pc';
  78. if ($isPc) {
  79. $payType = 'balance';
  80. }
  81. if (!in_array($payType, StoreOrderRepository::PAY_TYPE))
  82. return app('json')->fail('请选择正确的支付方式');
  83. if (!in_array($order_type, [0, 1, 2, 3, 4]))
  84. return app('json')->fail('订单类型错误');
  85. $validate = app()->make(UserReceiptValidate::class);
  86. foreach ($receipt_data as $receipt) {
  87. if (!is_array($receipt)) throw new ValidateException('发票信息有误');
  88. $validate->check($receipt);
  89. }
  90. $uid = $this->request->uid();
  91. if (!($count = count($cartId)) || $count != count($cartRepository->validIntersection($cartId, $uid)))
  92. return app('json')->fail('数据无效');
  93. if (!$addressId)
  94. return app('json')->fail('请选择地址');
  95. makeLock()->lock();
  96. try {
  97. if ($order_type == 2) {
  98. $groupOrder = $this->repository->createPresellOrder($this->request->userInfo(), array_search($payType, StoreOrderRepository::PAY_TYPE), $cartId, $addressId, $coupon, $take, $mark, $receipt_data);
  99. } else {
  100. $groupOrder = $this->repository->createOrder($this->request->userInfo(), array_search($payType, StoreOrderRepository::PAY_TYPE), $cartId, $addressId, $coupon, $take, $mark, $receipt_data);
  101. }
  102. } catch (\Throwable $e) {
  103. makeLock()->unlock();
  104. throw $e;
  105. }
  106. makeLock()->unlock();
  107. if ($groupOrder['pay_price'] == 0) {
  108. $this->repository->paySuccess($groupOrder);
  109. return app('json')->status('success', '支付成功', ['order_id' => $groupOrder['group_order_id']]);
  110. }
  111. if ($isPc) {
  112. return app('json')->success(['order_id' => $groupOrder->group_order_id]);
  113. }
  114. try {
  115. return $this->repository->pay($payType, $this->request->userInfo(), $groupOrder, $this->request->param('return_url'));
  116. } catch (\Exception $e) {
  117. return app('json')->status('error', $e->getMessage(), ['order_id' => $groupOrder->group_order_id]);
  118. }
  119. }
  120. /**
  121. * @return mixed
  122. * @throws \think\db\exception\DataNotFoundException
  123. * @throws \think\db\exception\DbException
  124. * @throws \think\db\exception\ModelNotFoundException
  125. * @author xaboy
  126. * @day 2020/6/10
  127. */
  128. public function lst()
  129. {
  130. [$page, $limit] = $this->getPage();
  131. return app('json')->success($this->repository->getList([
  132. 'uid' => $this->request->uid(),
  133. 'paid' => 1,
  134. 'status' => (int)$this->request->get('status', 0)
  135. ], $page, $limit));
  136. }
  137. /**
  138. * @param $id
  139. * @return mixed
  140. * @author xaboy
  141. * @day 2020/6/10
  142. */
  143. public function detail($id)
  144. {
  145. $order = $this->repository->getDetail((int)$id, $this->request->uid());
  146. if (!$order)
  147. return app('json')->fail('订单不存在');
  148. if ($order->order_type == 1) {
  149. $order->append(['take']);
  150. }
  151. return app('json')->success($order->toArray());
  152. }
  153. /**
  154. * @return mixed
  155. * @author xaboy
  156. * @day 2020/6/10
  157. */
  158. public function number()
  159. {
  160. return app('json')->success(['orderPrice' => $this->request->userInfo()->pay_price] + $this->repository->userOrderNumber($this->request->uid()));
  161. }
  162. /**
  163. * @param StoreGroupOrderRepository $groupOrderRepository
  164. * @return mixed
  165. * @author xaboy
  166. * @day 2020/6/10
  167. */
  168. public function groupOrderList(StoreGroupOrderRepository $groupOrderRepository)
  169. {
  170. [$page, $limit] = $this->getPage();
  171. $list = $groupOrderRepository->getList(['uid' => $this->request->uid(), 'paid' => 0], $page, $limit);
  172. return app('json')->success($list);
  173. }
  174. /**
  175. * @param $id
  176. * @param StoreGroupOrderRepository $groupOrderRepository
  177. * @return mixed
  178. * @author xaboy
  179. * @day 2020/6/10
  180. */
  181. public function groupOrderDetail($id, StoreGroupOrderRepository $groupOrderRepository)
  182. {
  183. $groupOrder = $groupOrderRepository->detail($this->request->uid(), (int)$id);
  184. if (!$groupOrder)
  185. return app('json')->fail('订单不存在');
  186. else
  187. return app('json')->success($groupOrder->append(['cancel_time', 'cancel_unix'])->toArray());
  188. }
  189. public function groupOrderStatus($id, StoreGroupOrderRepository $groupOrderRepository)
  190. {
  191. $groupOrder = $groupOrderRepository->status($this->request->uid(), intval($id));
  192. if (!$groupOrder)
  193. return app('json')->fail('订单不存在');
  194. if ($groupOrder->paid) $groupOrder->append(['give_coupon']);
  195. $activity_type = 0;
  196. $activity_id = 0;
  197. foreach ($groupOrder->orderList as $order) {
  198. $activity_type = max($order->activity_type, $activity_type);
  199. if ($order->activity_type == 4 && $groupOrder->paid) {
  200. $order->append(['orderProduct']);
  201. $activity_id = $order->orderProduct[0]['activity_id'];
  202. }
  203. }
  204. $groupOrder->activity_type = $activity_type;
  205. $groupOrder->activity_id = $activity_id;
  206. return app('json')->success($groupOrder->toArray());
  207. }
  208. /**
  209. * @param $id
  210. * @param StoreGroupOrderRepository $groupOrderRepository
  211. * @return mixed
  212. * @author xaboy
  213. * @day 2020/6/10
  214. */
  215. public function cancelGroupOrder($id, StoreGroupOrderRepository $groupOrderRepository)
  216. {
  217. $groupOrderRepository->cancel((int)$id, $this->request->uid());
  218. return app('json')->success('取消成功');
  219. }
  220. public function groupOrderPay($id, StoreGroupOrderRepository $groupOrderRepository)
  221. {
  222. //TODO 佣金结算,佣金退回,物流查询
  223. $type = $this->request->param('type');
  224. if (!in_array($type, StoreOrderRepository::PAY_TYPE))
  225. return app('json')->fail('请选择正确的支付方式');
  226. $groupOrder = $groupOrderRepository->detail($this->request->uid(), (int)$id, false);
  227. if (!$groupOrder)
  228. return app('json')->fail('订单不存在或已支付');
  229. $this->repository->changePayType($groupOrder, array_search($type, StoreOrderRepository::PAY_TYPE));
  230. if ($groupOrder['pay_price'] == 0) {
  231. $this->repository->paySuccess($groupOrder);
  232. return app('json')->status('success', '支付成功', ['order_id' => $groupOrder['group_order_id']]);
  233. }
  234. try {
  235. return $this->repository->pay($type, $this->request->userInfo(), $groupOrder, $this->request->param('return_url'));
  236. } catch (\Exception $e) {
  237. return app('json')->status('error', $e->getMessage(), ['order_id' => $groupOrder->group_order_id]);
  238. }
  239. }
  240. public function take($id)
  241. {
  242. $this->repository->takeOrder($id, $this->request->userInfo());
  243. return app('json')->success('确认收货成功');
  244. }
  245. public function express($id)
  246. {
  247. $order = $this->repository->getWhere(['order_id' => $id, 'uid' => $this->request->uid(), 'is_del' => 0]);
  248. if (!$order)
  249. return app('json')->fail('订单不存在');
  250. if (!$order->delivery_type || !$order->delivery_id)
  251. return app('json')->fail('订单未发货');
  252. $express = ExpressService::express($order->delivery_id);
  253. $order->append(['orderProduct']);
  254. return app('json')->success(compact('express', 'order'));
  255. }
  256. public function verifyCode($id)
  257. {
  258. $order = $this->repository->getWhere(['order_id' => $id, 'uid' => $this->request->uid(), 'is_del' => 0, 'order_type' => 1]);
  259. if (!$order)
  260. return app('json')->fail('订单状态有误');
  261. // $type = $this->request->param('type');
  262. return app('json')->success(['qrcode' => $this->repository->wxQrcode($id, $order->verify_code)]);
  263. // return app('json')->success(['qrcode' => $type == 'routine' ? $this->repository->routineQrcode($id, $order->verify_code) : $this->repository->wxQrcode($id, $order->verify_code)]);
  264. }
  265. public function del($id)
  266. {
  267. $this->repository->userDel($id, $this->request->uid());
  268. return app('json')->success('删除成功');
  269. }
  270. }