StoreOrder.php 21 KB

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