StoreOrder.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\controller\admin\user\OilLevel;
  13. use think\App;
  14. use crmeb\basic\BaseController;
  15. use crmeb\services\LockService;
  16. use think\exception\ValidateException;
  17. use app\validate\api\UserReceiptValidate;
  18. use app\common\repositories\store\order\StoreCartRepository;
  19. use app\common\repositories\delivery\DeliveryOrderRepository;
  20. use app\common\repositories\store\order\StoreOrderRepository;
  21. use app\common\repositories\store\order\StoreGroupOrderRepository;
  22. use app\common\repositories\store\order\StoreOrderCreateRepository;
  23. use app\common\repositories\store\order\StoreOrderReceiptRepository;
  24. /**
  25. * Class StoreOrder
  26. * @package app\controller\api\store\order
  27. * @author xaboy
  28. * @day 2020/6/10
  29. */
  30. class StoreOrder extends BaseController
  31. {
  32. /**
  33. * @var StoreOrderRepository
  34. */
  35. protected $repository;
  36. /**
  37. * StoreOrder constructor.
  38. * @param App $app
  39. * @param StoreOrderRepository $repository
  40. */
  41. public function __construct(App $app, StoreOrderRepository $repository)
  42. {
  43. parent::__construct($app);
  44. $this->repository = $repository;
  45. }
  46. public function v2CheckOrder(StoreCartRepository $cartRepository, StoreOrderCreateRepository $orderCreateRepository)
  47. {
  48. $cartId = (array)$this->request->param('cart_id', []);
  49. $addressId = (int)$this->request->param('address_id');
  50. $couponIds = (array)$this->request->param('use_coupon', []);
  51. $takes = (array)$this->request->param('takes', []);
  52. $useIntegral = (bool)$this->request->param('use_integral', false);
  53. $user = $this->request->userInfo();
  54. $uid = $user->uid;
  55. $spread_uid = $user->spread_uid;
  56. if ($spread_uid<=0){
  57. return app('json')->fail('没有推荐人无法下单');
  58. }
  59. if (!($count = count($cartId)) || $count != count($cartRepository->validIntersection($cartId, $uid)))
  60. return app('json')->fail('数据无效');
  61. $orderInfo = $orderCreateRepository->v2CartIdByOrderInfo($user, $cartId, $takes, $couponIds, $useIntegral, $addressId);
  62. return app('json')->success($orderInfo);
  63. }
  64. public function v2CreateOrder(StoreCartRepository $cartRepository, StoreOrderCreateRepository $orderCreateRepository)
  65. {
  66. $cartId = (array)$this->request->param('cart_id', []);
  67. $addressId = (int)$this->request->param('address_id');
  68. $couponIds = (array)$this->request->param('use_coupon', []);
  69. $takes = (array)$this->request->param('takes', []);
  70. $useIntegral = (bool)$this->request->param('use_integral', false);
  71. $receipt_data = (array)$this->request->param('receipt_data', []);
  72. $extend = (array)$this->request->param('extend', []);
  73. $mark = (array)$this->request->param('mark', []);
  74. $payType = $this->request->param('pay_type');
  75. $key = (string)$this->request->param('key');
  76. $post = (array)$this->request->param('post');
  77. if(!$key){
  78. return app('json')->fail('订单操作超时,请刷新页面');
  79. }
  80. $payType = ($payType === 'pc') ? 'balance' : $payType;
  81. if (!in_array($payType, StoreOrderRepository::PAY_TYPE, true))
  82. return app('json')->fail('请选择正确的支付方式');
  83. $validate = app()->make(UserReceiptValidate::class);
  84. foreach ($receipt_data as $receipt) {
  85. if (!is_array($receipt)) throw new ValidateException('发票信息有误');
  86. $validate->check($receipt);
  87. }
  88. $uid = $this->request->uid();
  89. $user = $this->request->userInfo();
  90. $spread_uid = $user->spread_uid;
  91. if ($spread_uid<=0){
  92. return app('json')->fail('没有推荐人无法下单');
  93. }
  94. if (!($count = count($cartId)) || $count != count($cartRepository->validIntersection($cartId, $uid)))
  95. return app('json')->fail('已生成订单,请勿重复提交~');
  96. $groupOrder = app()->make(LockService::class)->exec('order.create', function () use ($key, $orderCreateRepository, $receipt_data, $mark, $extend, $cartId, $payType, $takes, $couponIds, $useIntegral, $addressId, $post) {
  97. return $orderCreateRepository->v2CreateOrder($key, array_search($payType, StoreOrderRepository::PAY_TYPE), $this->request->userInfo(), $cartId, $extend, $mark, $receipt_data, $takes, $couponIds, $useIntegral, $addressId, $post);
  98. });
  99. //全部改成创建订单,下一步调用支付
  100. try{
  101. $orderList = $this->repository->getSearch([])->where('group_order_id',$groupOrder->group_order_id)->select();
  102. foreach ($orderList as $item ) {
  103. $this->repository->autoPrinter($item->order_id, $item->mer_id, 2);
  104. }
  105. }catch (\Exception $e) {
  106. }
  107. return app('json')->success(['order_id' => $groupOrder->group_order_id]);
  108. /**
  109. * 以下是立即支付,返回支付或者跳转信息
  110. */
  111. //try {
  112. // return $this->repository->pay($payType, $this->request->userInfo(), $groupOrder, $this->request->param('return_url'), $this->request->isApp());
  113. //} catch (\Exception $e) {
  114. // return app('json')->status('error', $e->getMessage(), ['order_id' => $groupOrder->group_order_id]);
  115. //}
  116. }
  117. /**
  118. * @return mixed
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. * @author xaboy
  123. * @day 2020/6/10
  124. */
  125. public function lst()
  126. {
  127. [$page, $limit] = $this->getPage();
  128. $where['status'] = $this->request->param('status');
  129. $where['search'] = $this->request->param('store_name');
  130. $where['uid'] = $this->request->uid();
  131. $where['paid'] = 1;
  132. // $where['is_user'] = 1;
  133. $data = $this->repository->getList($where, $page, $limit);
  134. return app('json')->success($data);
  135. }
  136. /**
  137. * @param $id
  138. * @return mixed
  139. * @author xaboy
  140. * @day 2020/6/10
  141. */
  142. public function detail($id)
  143. {
  144. $order = $this->repository->getDetail((int)$id, $this->request->uid());
  145. if (!$order)
  146. return app('json')->fail('订单不存在');
  147. if ($order->order_type == 1) {
  148. $order->append(['take', 'refund_status', 'open_receipt']);
  149. }
  150. return app('json')->success($order->toArray());
  151. }
  152. /**
  153. * @return mixed
  154. * @author xaboy
  155. * @day 2020/6/10
  156. */
  157. public function number()
  158. {
  159. return app('json')->success(['orderPrice' => $this->request->userInfo()->pay_price] + $this->repository->userOrderNumber($this->request->uid()));
  160. }
  161. /**
  162. * @param StoreGroupOrderRepository $groupOrderRepository
  163. * @return mixed
  164. * @author xaboy
  165. * @day 2020/6/10
  166. */
  167. public function groupOrderList(StoreGroupOrderRepository $groupOrderRepository)
  168. {
  169. [$page, $limit] = $this->getPage();
  170. $list = $groupOrderRepository->getList(['uid' => $this->request->uid(), 'paid' => 0], $page, $limit);
  171. return app('json')->success($list);
  172. }
  173. /**
  174. * @param $id
  175. * @param StoreGroupOrderRepository $groupOrderRepository
  176. * @return mixed
  177. * @author xaboy
  178. * @day 2020/6/10
  179. */
  180. public function groupOrderDetail($id, StoreGroupOrderRepository $groupOrderRepository)
  181. {
  182. $groupOrder = $groupOrderRepository->detail($this->request->uid(), (int)$id);
  183. if (!$groupOrder)
  184. return app('json')->fail('订单不存在');
  185. else
  186. return app('json')->success($groupOrder->append(['cancel_time', 'cancel_unix'])->toArray());
  187. }
  188. /**
  189. * 订单状态查询
  190. * @param $id
  191. * @param StoreGroupOrderRepository $groupOrderRepository
  192. * @return \think\response\Json
  193. * @author Qinii
  194. */
  195. public function groupOrderStatus($id, StoreGroupOrderRepository $groupOrderRepository)
  196. {
  197. $groupOrder = $groupOrderRepository->status($this->request->uid(), intval($id));
  198. if (!$groupOrder)
  199. return app('json')->fail('订单不存在');
  200. if ($groupOrder->paid) $groupOrder->append(['give_coupon']);
  201. $activity_type = 0;
  202. $activity_id = 0;
  203. foreach ($groupOrder->orderList as $order) {
  204. $activity_type = max($order->activity_type, $activity_type);
  205. if ($order->activity_type == 4 && $groupOrder->paid) {
  206. $order->append(['orderProduct']);
  207. $activity_id = $order->orderProduct[0]['activity_id'];
  208. }
  209. }
  210. $groupOrder->activity_type = $activity_type;
  211. $groupOrder->activity_id = $activity_id;
  212. return app('json')->success($groupOrder->toArray());
  213. }
  214. /**
  215. * @param $id
  216. * @param StoreGroupOrderRepository $groupOrderRepository
  217. * @return mixed
  218. * @author xaboy
  219. * @day 2020/6/10
  220. */
  221. public function cancelGroupOrder($id, StoreGroupOrderRepository $groupOrderRepository)
  222. {
  223. $groupOrderRepository->cancel((int)$id, $this->request->uid());
  224. return app('json')->success('取消成功');
  225. }
  226. /**
  227. * 订单付款操作
  228. * @param $id
  229. * @param StoreGroupOrderRepository $groupOrderRepository
  230. * @return mixed|\think\response\Json
  231. * @author Qinii
  232. */
  233. public function groupOrderPay($id, StoreGroupOrderRepository $groupOrderRepository)
  234. {
  235. //TODO 佣金结算,佣金退回,物流查询
  236. $type = $this->request->param('type');
  237. $is_points = $this->request->param('is_points',0);
  238. if (!in_array($type, StoreOrderRepository::PAY_TYPE))
  239. return app('json')->fail('请选择正确的支付方式');
  240. $groupOrder = $groupOrderRepository->detail($this->request->uid(), (int)$id, false);
  241. if (!$groupOrder)
  242. return app('json')->fail('订单不存在或已支付');
  243. $this->repository->changePayType($groupOrder, array_search($type, StoreOrderRepository::PAY_TYPE));
  244. if ($groupOrder['pay_price'] == 0) {
  245. $this->repository->paySuccess($groupOrder);
  246. return app('json')->status('success', '支付成功', ['order_id' => $groupOrder['group_order_id']]);
  247. }
  248. if ($type == 'offline') {
  249. if (count($groupOrder['orderList']) > 1) {
  250. return app('json')->fail('线下支付仅支持同店铺商品');
  251. }
  252. if (!systemConfig('offline_switch')) {
  253. return app('json')->fail('未开启线下支付功能');
  254. }
  255. if (!(($groupOrder['orderList'][0]->merchant['offline_switch']) ?? '')) {
  256. return app('json')->fail('该店铺未开启线下支付');
  257. }
  258. return app('json')->status('success', '线下支付,请告知收银员', ['order_id' => $groupOrder['group_order_id']]);
  259. }
  260. @file_put_contents('quanju.txt',"-123\r\n",8);
  261. try {
  262. return $this->repository->pay($type, $this->request->userInfo(), $groupOrder, $this->request->param('return_url'), $this->request->isApp());
  263. } catch (\Exception $e) {
  264. return app('json')->status('error', $e->getMessage(), ['order_id' => $groupOrder->group_order_id]);
  265. }
  266. }
  267. public function take($id)
  268. {
  269. $this->repository->takeOrder($id, $this->request->userInfo());
  270. return app('json')->success('确认收货成功');
  271. }
  272. public function express($id)
  273. {
  274. $order = $this->repository->getWhere(['order_id' => $id, 'is_del' => 0]);
  275. if (!$order)
  276. return app('json')->fail('订单不存在');
  277. if (!$order->delivery_type || !$order->delivery_id)
  278. return app('json')->fail('订单未发货');
  279. $express = $this->repository->express($id,null);
  280. $order->append(['orderProduct']);
  281. return app('json')->success(compact('express', 'order'));
  282. }
  283. public function verifyCode($id)
  284. {
  285. $order = $this->repository->getWhere(['order_id' => $id, 'uid' => $this->request->uid(), 'is_del' => 0]);
  286. if (!$order)return app('json')->fail('订单状态有误');
  287. return app('json')->success(['qrcode' => $this->repository->wxQrcode($id, $order)]);
  288. }
  289. public function del($id)
  290. {
  291. $this->repository->userDel($id, $this->request->uid());
  292. return app('json')->success('删除成功');
  293. }
  294. public function createReceipt($id)
  295. {
  296. $data = $this->request->params(['receipt_type' , 'receipt_title' , 'duty_paragraph', 'receipt_title_type', 'bank_name', 'bank_code', 'address','tel', 'email']);
  297. $order = $this->repository->getWhere(['order_id' => $id, 'uid' => $this->request->uid(), 'is_del' => 0]);
  298. if (!$order) return app('json')->fail('订单不属于您或不存在');
  299. app()->make(StoreOrderReceiptRepository::class)->add($data, $order);
  300. return app('json')->success('操作成功');
  301. }
  302. public function getOrderDelivery($id, DeliveryOrderRepository $orderRepository)
  303. {
  304. $res = $orderRepository->show($id, $this->request->uid());
  305. return app('json')->success($res);
  306. }
  307. public function getCashierOrder($id)
  308. {
  309. $data = $this->repository->payConfig($id, $this->request->uid());
  310. return app('json')->success($data);
  311. }
  312. public function payConfig()
  313. {
  314. $id = $this->request->param('id',0);
  315. $type = $this->request->param('type',0);
  316. if ($type) {
  317. $data = $this->repository->payConfigPresell($id, $this->request->uid());
  318. } else {
  319. $data = $this->repository->payConfig($id, $this->request->uid());
  320. }
  321. return app('json')->success($data);
  322. }
  323. public function cancelOrder($id)
  324. {
  325. $order = $this->repository->getSearch(['uid' => $this->request->uid()])->where('order_id',$id)->find();
  326. if (!$order) return app('json')->fail('订单状态有误');
  327. // if (!$order->refund_status) return app('json')->fail('订单已过退款/退货期限');
  328. if ($order->status < 0) return app('json')->fail('订单已退款');
  329. if ($order->status == 10 || !$order->is_cancel) return app('json')->fail('订单不支持退款');
  330. if ($order->is_virtual !== 4) return app('json')->fail('订单不支持退款');
  331. $this->repository->cancelOrder($order);
  332. return app('json')->success('订单已取消');
  333. }
  334. /**
  335. * 核销
  336. * @day 2020/8/15
  337. */
  338. // public function validateVerify()
  339. // {
  340. // $data = $this->request->params(['data']);
  341. // $order_id = $this->request->params(['order_id']);
  342. // $user = $this->request->userInfo();
  343. //// $num = $this->request->params(['num']);
  344. //// $merId = $this->request->merId();
  345. // // 根据订单ID、商家ID、验证码和订单类型查询订单,并连带查询订单产品信息
  346. // $order = $this->repository->getWhere(['order_id' => $order_id, 'order_type' => 1], '*', ['orderProduct']);
  347. // // 如果订单不存在,则抛出验证异常
  348. // if (!$order) return app('json')->fail('订单不存在');
  349. // // 如果订单未支付,则抛出验证异常
  350. // if (!$order->paid) return app('json')->fail('订单未支付');
  351. // // 如果订单已全部核销,则抛出验证异常
  352. // if ($order['status']) return app('json')->fail('订单已全部核销,请勿重复操作');
  353. // if ($user['oil_level']>0){
  354. // $name = \app\common\model\user\OilLevel::where('id',$user['oil_level'])->value('name');
  355. //
  356. // if ($name!=='服务中心'){
  357. // return app('json')->fail('不是服务中心不能进行核销');
  358. // }
  359. // }else{
  360. //
  361. // return app('json')->fail('不是服务中心不能进行核销');
  362. // }
  363. // $this->repository->verifyPartOrder($order, $data,$user['uid']);
  364. // return app('json')->success('订单核销成功');
  365. // }
  366. /**
  367. * 验证核销并返回商品内容
  368. * @day 2020/8/15
  369. */
  370. public function validateVerify()
  371. {
  372. $data = $this->request->params(['data']);
  373. $order_id = $this->request->params(['order_id']);
  374. $user = $this->request->userInfo();
  375. var_dump($data);die();
  376. // $num = $this->request->params(['num']);
  377. // $merId = $this->request->merId();
  378. // 根据订单ID、商家ID、验证码和订单类型查询订单,并连带查询订单产品信息
  379. $order = $this->repository->getWhere(['order_id' => $order_id, 'order_type' => 1], '*', ['orderProduct']);
  380. // 如果订单不存在,则抛出验证异常
  381. if (!$order) return app('json')->fail('订单不存在');
  382. // 如果订单未支付,则抛出验证异常
  383. if (!$order->paid) return app('json')->fail('订单未支付');
  384. // 如果订单已全部核销,则抛出验证异常
  385. if ($order['status']) return app('json')->fail('订单已全部核销,请勿重复操作');
  386. if ($user['oil_level']>0){
  387. $name = \app\common\model\user\OilLevel::where('id',$user['oil_level'])->value('name');
  388. if ($name!=='服务中心'){
  389. return app('json')->fail('不是服务中心不能进行核销');
  390. }
  391. }else{
  392. return app('json')->fail('不是服务中心不能进行核销');
  393. }
  394. $this->repository->verifyPartOrder($order, $data,$user['uid']);
  395. return app('json')->success('订单核销成功');
  396. // $order_id = $this->request->params(['order_sn']);
  397. //
  398. //// $num = $this->request->params(['num']);
  399. //// $merId = $this->request->merId();
  400. // // 根据订单ID、商家ID、验证码和订单类型查询订单,并连带查询订单产品信息
  401. // $order = $this->repository->getWhere(['order_sn' => $order_id], '*', ['orderProduct']);
  402. // // 如果订单不存在,则抛出验证异常
  403. // if (!$order) return app('json')->fail('订单不存在');
  404. // // 如果订单未支付,则抛出验证异常
  405. // if (!$order->paid) return app('json')->fail('订单未支付');
  406. // // 如果订单已全部核销,则抛出验证异常
  407. // if ($order['status']) return app('json')->fail('订单已全部核销,请勿重复操作');
  408. // $user = $this->request->userInfo();
  409. // if ($user['oil_level']>0){
  410. // $name = \app\common\model\user\OilLevel::where('id',$user['oil_level'])->value('name');
  411. //
  412. // if ($name!=='服务中心'){
  413. // return app('json')->fail('不是服务中心不能进行核销');
  414. // }
  415. // }else{
  416. // var_dump($user['oil_level']);
  417. // return app('json')->fail('不是服务中心不能进行核销');
  418. // }
  419. // foreach ($order['orderProduct'] as $k => $v){
  420. // $v['verify_num'] = \app\common\model\store\order\StoreCart::where('product_id',$v['product_id'])->where('cart_id',$v['cart_id'])->value('verify_num');
  421. // }
  422. //// $this->repository->verifyPartOrder($order, $data);
  423. // return app('json')->success($order);
  424. }
  425. }