StoreOrderController.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v1\order;
  12. use app\Request;
  13. use app\services\pay\PayServices;
  14. use app\services\shipping\ExpressServices;
  15. use app\services\system\admin\SystemAdminServices;
  16. use app\services\activity\{lottery\LuckLotteryServices,
  17. combination\StorePinkServices
  18. };
  19. use app\services\activity\coupon\StoreCouponIssueServices;
  20. use app\services\order\{StoreCartServices,
  21. StoreOrderCartInfoServices,
  22. StoreOrderComputedServices,
  23. StoreOrderCreateServices,
  24. StoreOrderEconomizeServices,
  25. StoreOrderInvoiceServices,
  26. StoreOrderRefundServices,
  27. StoreOrderServices,
  28. StoreOrderStatusServices,
  29. StoreOrderSuccessServices,
  30. StoreOrderTakeServices
  31. };
  32. use app\services\pay\OrderPayServices;
  33. use app\services\pay\YuePayServices;
  34. use app\services\product\product\StoreProductReplyServices;
  35. use app\services\shipping\ShippingTemplatesServices;
  36. use crmeb\services\CacheService;
  37. use Psr\SimpleCache\InvalidArgumentException;
  38. use think\db\exception\DataNotFoundException;
  39. use think\db\exception\DbException;
  40. use think\db\exception\ModelNotFoundException;
  41. use think\facade\Log;
  42. use think\Response;
  43. /**
  44. * 订单控制器
  45. * Class StoreOrderController
  46. * @package app\api\controller\order
  47. */
  48. class StoreOrderController
  49. {
  50. /**
  51. * @var StoreOrderServices
  52. */
  53. protected $services;
  54. /**
  55. * @var int[]
  56. */
  57. protected $getChennel = [
  58. 'wechat' => 0,
  59. 'routine' => 1,
  60. 'h5' => 2,
  61. 'pc' => 3,
  62. 'app' => 4
  63. ];
  64. /**
  65. * StoreOrderController constructor.
  66. * @param StoreOrderServices $services
  67. */
  68. public function __construct(StoreOrderServices $services)
  69. {
  70. $this->services = $services;
  71. }
  72. /**
  73. * 获取确认订单页面是否展示快递配送和到店自提
  74. * @param Request $request
  75. * @return mixed
  76. * @throws InvalidArgumentException
  77. */
  78. public function checkShipping(Request $request)
  79. {
  80. [$cartId, $new] = $request->postMore(['cartId', 'new'], true);
  81. return app('json')->success($this->services->checkShipping($request->uid(), $cartId, $new));
  82. }
  83. /**
  84. * 订单确认
  85. * @param Request $request
  86. * @param ShippingTemplatesServices $services
  87. * @return mixed
  88. * @throws InvalidArgumentException
  89. * @throws DataNotFoundException
  90. * @throws DbException
  91. * @throws ModelNotFoundException
  92. */
  93. public function confirm(Request $request, ShippingTemplatesServices $services)
  94. {
  95. if (!$services->get(1, ['id'])) {
  96. return app('json')->fail(410207);
  97. }
  98. [$cartId, $new, $addressId, $shipping_type] = $request->postMore([
  99. 'cartId',
  100. 'new',
  101. ['addressId', 0],
  102. ['shipping_type', 1],
  103. ], true);
  104. if (!is_string($cartId) || !$cartId) {
  105. return app('json')->fail(410201);
  106. }
  107. $user = $request->user()->toArray();
  108. return app('json')->success($this->services->getOrderConfirmData($user, $cartId, !!$new, $addressId, (int)$shipping_type));
  109. }
  110. /**
  111. * 计算订单金额
  112. * @param Request $request
  113. * @param StoreOrderComputedServices $computedServices
  114. * @param $key
  115. * @return mixed
  116. */
  117. public function computedOrder(Request $request, StoreOrderComputedServices $computedServices, $key)
  118. {
  119. if (!$key) return app('json')->fail(100100);
  120. $uid = $request->uid();
  121. if ($this->services->be(['order_id|unique' => $key, 'uid' => $uid, 'is_del' => 0]))
  122. return app('json')->status('extend_order', 410173, ['orderId' => $key, 'key' => $key]);
  123. list($addressId, $couponId, $payType, $useIntegral, $mark, $combinationId, $pinkId, $seckill_id, $bargainId, $shipping_type) = $request->postMore([
  124. 'addressId', 'couponId', ['payType', ''], ['useIntegral', 0], 'mark', ['combinationId', 0], ['pinkId', 0], ['seckill_id', 0], ['bargainId', ''],
  125. ['shipping_type', 1],
  126. ], true);
  127. $payType = strtolower($payType);
  128. $cartGroup = $this->services->getCacheOrderInfo($uid, $key);
  129. if (!$cartGroup) return app('json')->fail(410208);
  130. $priceGroup = $computedServices->setParamData([
  131. 'combinationId' => $combinationId,
  132. 'pinkId' => $pinkId,
  133. 'seckill_id' => $seckill_id,
  134. 'bargainId' => $bargainId,
  135. ])->computedOrder($request->uid(), $request->user()->toArray(), $cartGroup, $addressId, $payType, !!$useIntegral, (int)$couponId, false, (int)$shipping_type);
  136. if ($priceGroup)
  137. return app('json')->status('NONE', 100010, $priceGroup);
  138. else
  139. return app('json')->fail(100016);
  140. }
  141. /**
  142. * 订单创建
  143. * @param Request $request
  144. * @param StoreOrderCreateServices $createServices
  145. * @param $key
  146. * @return Response
  147. * @throws DataNotFoundException
  148. * @throws DbException
  149. * @throws ModelNotFoundException
  150. */
  151. public function create(Request $request, StoreOrderCreateServices $createServices, $key)
  152. {
  153. if (!$key) return app('json')->fail(100100);
  154. $userInfo = $request->user()->toArray();
  155. if ($checkOrder = $this->services->getOne(['order_id|unique' => $key, 'uid' => $userInfo['uid'], 'is_del' => 0]))
  156. return app('json')->status('extend_order', 410209, ['orderId' => $checkOrder['order_id'], 'key' => $key]);
  157. [$addressId, $couponId, $payType, $useIntegral, $mark, $combinationId, $pinkId, $seckillId, $bargainId, $shipping_type, $real_name, $phone, $storeId, $news, $invoice_id, $advanceId, $customForm] = $request->postMore([
  158. [['addressId', 'd'], 0],
  159. [['couponId', 'd'], 0],
  160. ['payType', ''],
  161. ['useIntegral', 0],
  162. ['mark', ''],
  163. [['combinationId', 'd'], 0],
  164. [['pinkId', 'd'], 0],
  165. [['seckill_id', 'd'], 0],
  166. [['bargainId', 'd'], ''],
  167. [['shipping_type', 'd'], 1],
  168. ['real_name', ''],
  169. ['phone', ''],
  170. [['store_id', 'd'], 0],
  171. ['new', 0],
  172. [['invoice_id', 'd'], 0],
  173. [['advanceId', 'd'], 0],
  174. ['custom_form', []],
  175. ], true);
  176. $payType = strtolower($payType);
  177. $order = CacheService::lock('orderCreate' . $key, function () use ($createServices, $userInfo, $key, $addressId, $payType, $useIntegral, $couponId, $mark, $combinationId, $pinkId, $seckillId, $bargainId, $shipping_type, $real_name, $phone, $storeId, $news, $advanceId, $customForm, $invoice_id) {
  178. return $createServices->createOrder($userInfo['uid'], $key, $userInfo, $addressId, $payType, !!$useIntegral, $couponId, $mark, $combinationId, $pinkId, $seckillId, $bargainId, $shipping_type, $real_name, $phone, $storeId, !!$news, $advanceId, $customForm, $invoice_id);
  179. });
  180. $orderId = $order['order_id'];
  181. return app('json')->status('success', 410203, compact('orderId', 'key'));
  182. }
  183. /**
  184. * 订单 再次下单
  185. * @param Request $request
  186. * @param StoreCartServices $services
  187. * @return mixed
  188. */
  189. public function again(Request $request, StoreCartServices $services)
  190. {
  191. list($uni) = $request->postMore([
  192. ['uni', ''],
  193. ], true);
  194. $cateId = $this->services->againOrder($services, $uni, (int)$request->uid());
  195. return app('json')->success(['cateId' => implode(',', $cateId)]);
  196. }
  197. /**
  198. * @param Request $request
  199. * @param $orderId
  200. * @param string $type
  201. * @return Response
  202. * @throws DataNotFoundException
  203. * @throws DbException
  204. * @throws ModelNotFoundException
  205. * @author 等风来
  206. * @email 136327134@qq.com
  207. * @date 2023/2/13
  208. */
  209. public function cashier(Request $request, $orderId, $type = 'order')
  210. {
  211. if (!$orderId) {
  212. return app('json')->fail(100100);
  213. }
  214. return app('json')->success($this->services->getCashierInfo((int)$request->uid(), $orderId, $type));
  215. }
  216. /**
  217. * 订单支付
  218. * @param Request $request
  219. * @param StorePinkServices $services
  220. * @param OrderPayServices $payServices
  221. * @param YuePayServices $yuePayServices
  222. * @return mixed
  223. */
  224. public function pay(Request $request, StorePinkServices $services, OrderPayServices $payServices, YuePayServices $yuePayServices)
  225. {
  226. [$uni, $paytype, $quitUrl, $type] = $request->postMore([
  227. ['uni', ''],
  228. ['paytype', ''],
  229. ['quitUrl', ''],
  230. ['type', 0]
  231. ], true);
  232. $payLock = CacheService::get('PAY_LOCK_' . $uni);
  233. if ($payLock) return app('json')->fail('订单支付中,请勿重复支付');
  234. CacheService::set('PAY_LOCK_' . $uni, 'PAY_LOCK', 2);
  235. if (!$uni) return app('json')->fail(100100);
  236. $orderInfo = $this->services->get(['order_id' => $uni]);
  237. if ($orderInfo->is_del == 1 || $orderInfo->is_system_del == 1) return app('json')->fail('订单已经超过系统支付时间,无法支付,请重新下单');
  238. $uid = $type == 1 ? (int)$request->uid() : $orderInfo->uid;
  239. $orderInfo->is_channel = $this->getChennel[$request->getFromType()] ?? ($request->isApp() ? 0 : 1);
  240. $orderInfo->order_id = $uid != $orderInfo->pay_uid ? app()->make(StoreOrderCreateServices::class)->getNewOrderId('cp') : $uni;
  241. $orderInfo->pay_uid = $uid;
  242. $orderInfo->save();
  243. $orderInfo = $orderInfo->toArray();
  244. $order = $this->services->get(['order_id' => $orderInfo['order_id']]);
  245. if (!$order)
  246. return app('json')->fail(410173);
  247. if ($order['paid'])
  248. return app('json')->fail(410174);
  249. if ($order['pink_id'] && $services->isPinkStatus($order['pink_id'])) {
  250. return app('json')->fail(410215);
  251. }
  252. //0元支付
  253. if (bcsub((string)$orderInfo['pay_price'], '0', 2) <= 0) {
  254. //创建订单jspay支付
  255. /** @var StoreOrderSuccessServices $success */
  256. $success = app()->make(StoreOrderSuccessServices::class);
  257. $payPriceStatus = $success->zeroYuanPayment($orderInfo, $uid, $paytype);
  258. if ($payPriceStatus)//0元支付成功
  259. return app('json')->status('success', '支付成功', ['order_id' => $orderInfo['order_id'], 'key' => $orderInfo['unique']]);
  260. else
  261. return app('json')->status('pay_error', 410216);
  262. }
  263. switch ($paytype) {
  264. case PayServices::YUE_PAY:
  265. $pay = $yuePayServices->yueOrderPay($order->toArray(), $request->uid());
  266. if ($pay['status'] === true)
  267. return app('json')->status('success', 410197);
  268. else {
  269. if (is_array($pay))
  270. return app('json')->status($pay['status'], $pay['msg']);
  271. else
  272. return app('json')->status('pay_error', $pay);
  273. }
  274. case PayServices::OFFLINE_PAY:
  275. if ($this->services->setOrderTypePayOffline($order['order_id'])) {
  276. event('NoticeListener', [$order->toArray(), 'admin_pay_success_code']);
  277. return app('json')->status('success', 410203);
  278. } else {
  279. return app('json')->status('success', 410216);
  280. }
  281. default:
  282. $payInfo = $payServices->beforePay($order->toArray(), $paytype, ['quitUrl' => $quitUrl]);
  283. return app('json')->status($payInfo['status'], $payInfo['payInfo']);
  284. }
  285. }
  286. /**
  287. * 订单列表
  288. * @param Request $request
  289. * @return mixed
  290. * @throws DataNotFoundException
  291. * @throws DbException
  292. * @throws ModelNotFoundException
  293. */
  294. public function lst(Request $request)
  295. {
  296. $where = $request->getMore([
  297. ['type', '', '', 'status'],
  298. ['search', '', '', 'real_name'],
  299. ['refund_type', '', '', 'refundTypes']
  300. ]);
  301. $where['uid'] = $request->uid();
  302. $where['is_del'] = 0;
  303. $where['is_system_del'] = 0;
  304. if (in_array($where['status'], [-1, -2, -3])) {
  305. $where['not_pid'] = 1;
  306. } elseif (in_array($where['status'], [0, 1, 2, 3, 4, 9])) {
  307. $where['pid'] = 0;
  308. }
  309. $list = $this->services->getOrderApiList($where);
  310. return app('json')->success($list);
  311. }
  312. /**
  313. * 订单详情
  314. * @param Request $request
  315. * @param StoreOrderEconomizeServices $services
  316. * @param $uni
  317. * @return mixed
  318. * @throws DataNotFoundException
  319. * @throws DbException
  320. * @throws ModelNotFoundException
  321. */
  322. public function detail(Request $request, StoreOrderEconomizeServices $services, $uni)
  323. {
  324. if (!strlen(trim($uni))) return app('json')->fail(100100);
  325. $orderData = $this->services->getUserOrderByKey($services, $uni, (int)$request->uid());
  326. return app('json')->success($orderData);
  327. }
  328. /**
  329. * 代付订单详情
  330. * @param Request $request
  331. * @return mixed
  332. * @throws DataNotFoundException
  333. * @throws DbException
  334. * @throws ModelNotFoundException
  335. */
  336. public function friendDetail(Request $request)
  337. {
  338. [$orderId] = $request->getMore([
  339. ['order_id', '']
  340. ], true);
  341. $info = $this->services->getFriendDetail($orderId, $request->uid());
  342. return app('json')->success(compact('info'));
  343. }
  344. /**
  345. * TODO 弃用
  346. * 退款订单详情
  347. * @param Request $request
  348. * @param $uni
  349. * @param string $cartId
  350. * @return mixed
  351. */
  352. public function refund_detail(Request $request, $uni, $cartId = '')
  353. {
  354. if (!strlen(trim($uni))) return app('json')->fail(100100);
  355. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  356. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  357. $order = $this->services->getUserOrderDetail($uni, (int)$request->uid(), ['split', 'invoice']);
  358. if (!$order) return app('json')->fail(410173);
  359. $order = $order->toArray();
  360. $orderData = $this->services->tidyOrder($order, true, true);
  361. $splitNum = $storeOrderCartInfoServices->getSplitCartNum($order['cart_id']);
  362. foreach ($orderData['cartInfo'] ?? [] as $key => $cart) {
  363. $orderData['cartInfo'][$key]['one_postage_price'] = isset($cart['postage_price']) ? bcdiv($cart['postage_price'], $cart['cart_num'], 2) : 0;
  364. if ($cartId != '') {
  365. if ($cart['id'] != $cartId) {
  366. unset($orderData['cartInfo'][$key]);
  367. } else {
  368. if (isset($splitNum[$cart['id']])) {
  369. $orderData['total_num'] = $orderData['cartInfo'][$key]['cart_num'] = $cart['cart_num'] - $splitNum[$cart['id']];
  370. $orderData['pay_price'] = bcadd(bcmul($cart['truePrice'], $orderData['total_num'], 4), bcmul($orderData['total_num'], $orderData['cartInfo'][$key]['one_postage_price'], 4), 2);
  371. } else {
  372. $orderData['total_num'] = $orderData['cartInfo'][$key]['cart_num'];
  373. $orderData['pay_price'] = bcadd(bcmul($cart['truePrice'], $cart['cart_num'], 4), $cart['postage_price'], 2);
  374. }
  375. }
  376. } else {
  377. if (isset($splitNum[$cart['id']])) {
  378. $orderData['cartInfo'][$key]['cart_num'] = $cart['cart_num'] - $splitNum[$cart['id']];
  379. $orderData['total_num'] = $orderData['total_num'] - $splitNum[$cart['id']];
  380. if ($orderData['cartInfo'][$key]['cart_num'] == 0) unset($orderData['cartInfo'][$key]);
  381. }
  382. }
  383. }
  384. if ($cartId == '') {
  385. $orderData['pay_price'] = bcsub($orderData['pay_price'], $this->services->sum(['pid' => $orderData['id']], 'pay_price'), 2);
  386. }
  387. $orderData['cartInfo'] = array_merge($orderData['cartInfo']);
  388. return app('json')->success($orderData);
  389. }
  390. /**
  391. * 订单删除
  392. * @param Request $request
  393. * @return mixed
  394. * @throws InvalidArgumentException
  395. */
  396. public function del(Request $request)
  397. {
  398. [$uni] = $request->postMore([
  399. ['uni', ''],
  400. ], true);
  401. if (!$uni) return app('json')->fail(100100);
  402. $res = $this->services->removeOrder($uni, (int)$request->uid());
  403. if ($res) {
  404. return app('json')->success(100002);
  405. } else {
  406. return app('json')->fail(100008);
  407. }
  408. }
  409. /**
  410. * 订单收货
  411. * @param Request $request
  412. * @param StoreOrderTakeServices $services
  413. * @param StoreCouponIssueServices $issueServices
  414. * @return mixed
  415. */
  416. public function take(Request $request, StoreOrderTakeServices $services, StoreCouponIssueServices $issueServices)
  417. {
  418. list($uni) = $request->postMore([
  419. ['uni', ''],
  420. ], true);
  421. if (!$uni) return app('json')->fail(100100);
  422. $order = $services->takeOrder($uni, (int)$request->uid());
  423. if ($order) {
  424. return app('json')->success(410204);
  425. } else
  426. return app('json')->fail(410205);
  427. }
  428. /**
  429. * 订单 查看物流
  430. * @param Request $request
  431. * @param StoreOrderCartInfoServices $services
  432. * @param ExpressServices $expressServices
  433. * @param $uni
  434. * @param string $type
  435. * @return mixed
  436. * @throws DataNotFoundException
  437. * @throws DbException
  438. * @throws ModelNotFoundException
  439. */
  440. public function express(Request $request, StoreOrderCartInfoServices $services, ExpressServices $expressServices, $uni, $type = '')
  441. {
  442. if ($type == 'refund') {
  443. /** @var StoreOrderRefundServices $refundService */
  444. $refundService = app()->make(StoreOrderRefundServices::class);
  445. $order = $refundService->refundDetail($uni);
  446. $express = $order['refund_express'];
  447. $cacheName = $uni . $express;
  448. $orderInfo = [];
  449. $info = [];
  450. $cartNew = [];
  451. foreach ($order['cart_info'] as $k => $cart) {
  452. $cartNew['cart_num'] = $cart['cart_num'];
  453. $cartNew['truePrice'] = $cart['truePrice'];
  454. $cartNew['postage_price'] = $cart['postage_price'];
  455. $cartNew['productInfo']['image'] = $cart['productInfo']['image'];
  456. $cartNew['productInfo']['store_name'] = $cart['productInfo']['store_name'];
  457. $cartNew['productInfo']['unit_name'] = $cart['productInfo']['unit_name'] ?? '';
  458. array_push($info, $cartNew);
  459. unset($cart);
  460. }
  461. $orderInfo['cartInfo'] = $info;
  462. $orderInfo['delivery_id'] = $express;
  463. $orderInfo['delivery_name'] = $order['refund_express_name'];
  464. $orderInfo['delivery_code'] = '';
  465. } else {
  466. if (!$uni || !($order = $this->services->getUserOrderDetail($uni, $request->uid(), []))) return app('json')->fail(410173);
  467. if ($type != 'refund' && ($order['delivery_type'] != 'express' || !$order['delivery_id'])) return app('json')->fail(410206);
  468. $express = $type == 'refund' ? $order['refund_express'] : $order['delivery_id'];
  469. $cacheName = $uni . $express;
  470. $orderInfo = [];
  471. $cartInfo = $services->getCartColunm(['oid' => $order['id']], 'cart_info', 'unique');
  472. $info = [];
  473. $cartNew = [];
  474. foreach ($cartInfo as $cart) {
  475. $cart = json_decode($cart, true);
  476. $cartNew['cart_num'] = $cart['cart_num'];
  477. $cartNew['truePrice'] = $cart['truePrice'];
  478. $cartNew['postage_price'] = $cart['postage_price'];
  479. $cartNew['productInfo']['image'] = $cart['productInfo']['image'];
  480. $cartNew['productInfo']['store_name'] = $cart['productInfo']['store_name'];
  481. $cartNew['productInfo']['unit_name'] = $cart['productInfo']['unit_name'] ?? '';
  482. array_push($info, $cartNew);
  483. unset($cart);
  484. }
  485. $orderInfo['delivery_id'] = $express;
  486. $orderInfo['delivery_name'] = $type == 'refund' ? '用户退回' : $order['delivery_name'];;
  487. $orderInfo['delivery_code'] = $type == 'refund' ? '' : $order['delivery_code'];
  488. $orderInfo['delivery_type'] = $order['delivery_type'];
  489. $orderInfo['user_address'] = $order['user_address'];
  490. $orderInfo['user_mark'] = $order['mark'];
  491. $orderInfo['cartInfo'] = $info;
  492. }
  493. return app('json')->success([
  494. 'order' => $orderInfo,
  495. 'express' => [
  496. 'result' => ['list' => $expressServices->query($cacheName, $orderInfo['delivery_id'], $orderInfo['delivery_code'], $order['user_phone'])
  497. ]
  498. ]
  499. ]);
  500. }
  501. /**
  502. * 订单评价
  503. * @param Request $request
  504. * @param StoreOrderCartInfoServices $cartInfoServices
  505. * @param StoreProductReplyServices $replyServices
  506. * @return Response|void
  507. * @throws InvalidArgumentException
  508. */
  509. public function comment(Request $request, StoreOrderCartInfoServices $cartInfoServices, StoreProductReplyServices $replyServices)
  510. {
  511. $group = $request->postMore([
  512. ['unique', ''], ['comment', ''], ['pics', ''], ['product_score', 5], ['service_score', 5]
  513. ]);
  514. $unique = $group['unique'];
  515. unset($group['unique']);
  516. if (!$unique) return app('json')->fail(100100);
  517. $cartInfo = $cartInfoServices->getOne(['unique' => $unique]);
  518. $uid = $request->uid();
  519. $user_info = $request->user();
  520. $group['nickname'] = $user_info['nickname'];
  521. $group['avatar'] = $user_info['avatar'];
  522. if (!$cartInfo) return app('json')->fail(410294);
  523. $orderUid = $this->services->value(['id' => $cartInfo['oid']], 'uid');
  524. if ($uid != $orderUid) return app('json')->fail(410294);
  525. if ($replyServices->be(['oid' => $cartInfo['oid'], 'unique' => $unique]))
  526. return app('json')->fail(410219);
  527. $group['comment'] = htmlspecialchars(trim($group['comment']));
  528. if ($group['product_score'] < 1) return app('json')->fail(410220);
  529. else if ($group['service_score'] < 1) return app('json')->fail(410221);
  530. if ($cartInfo['cart_info']['combination_id']) $productId = $cartInfo['cart_info']['product_id'];
  531. else if ($cartInfo['cart_info']['seckill_id']) $productId = $cartInfo['cart_info']['product_id'];
  532. else if ($cartInfo['cart_info']['bargain_id']) $productId = $cartInfo['cart_info']['product_id'];
  533. else $productId = $cartInfo['product_id'];
  534. if ($group['pics']) $group['pics'] = json_encode(is_array($group['pics']) ? $group['pics'] : explode(',', $group['pics']));
  535. $group = array_merge($group, [
  536. 'uid' => $uid,
  537. 'oid' => $cartInfo['oid'],
  538. 'unique' => $unique,
  539. 'product_id' => $productId,
  540. 'add_time' => time(),
  541. 'reply_type' => 'product',
  542. 'suk' => $cartInfo['cart_info']['productInfo']['attrInfo']['suk']
  543. ]);
  544. $res = $replyServices->save($group);
  545. if (!$res) {
  546. return app('json')->fail(410222);
  547. }
  548. //自定义事件-订单评价
  549. event('CustomEventListener', ['order_comment', [
  550. 'uid' => $uid,
  551. 'oid' => $cartInfo['oid'],
  552. 'unique' => $unique,
  553. 'product_id' => $productId,
  554. 'add_time' => date('Y-m-d H:i:s'),
  555. 'suk' => $cartInfo['cart_info']['productInfo']['attrInfo']['suk']
  556. ]]);
  557. try {
  558. $this->services->checkOrderOver($replyServices, $cartInfoServices->getCartColunm(['oid' => $cartInfo['oid']], 'unique', ''), $cartInfo['oid']);
  559. } catch (\Exception $e) {
  560. return app('json')->fail(410222);
  561. }
  562. //缓存抽奖次数
  563. /** @var LuckLotteryServices $luckLotteryServices */
  564. $luckLotteryServices = app()->make(LuckLotteryServices::class);
  565. $luckLotteryServices->setCacheLotteryNum((int)$orderUid, 'comment');
  566. /** @var SystemAdminServices $systemAdmin */
  567. $systemAdmin = app()->make(SystemAdminServices::class);
  568. $systemAdmin->adminNewPush();
  569. $lottery = $luckLotteryServices->getFactorLottery(4);
  570. if (!$lottery) {
  571. return app('json')->success(['to_lottery' => false]);
  572. }
  573. $lottery = $lottery->toArray();
  574. try {
  575. $luckLotteryServices->checkoutUserAuth($uid, (int)$lottery['id'], [], $lottery);
  576. $lottery_num = $luckLotteryServices->getLotteryNum($uid, (int)$lottery['id'], [], $lottery);
  577. if ($lottery_num > 0) return app('json')->success(['to_lottery' => true]);
  578. } catch (\Exception $e) {
  579. return app('json')->success(['to_lottery' => false]);
  580. }
  581. }
  582. /**
  583. * 订单统计数据
  584. * @param Request $request
  585. * @return mixed
  586. * @throws \ReflectionException
  587. */
  588. public function data(Request $request)
  589. {
  590. return app('json')->success($this->services->getOrderData((int)$request->uid()));
  591. }
  592. /**
  593. * 订单退款理由
  594. * @return mixed
  595. */
  596. public function refund_reason()
  597. {
  598. $reason = sys_config('stor_reason') ?: [];//退款理由
  599. $reason = str_replace("\r\n", "\n", $reason);//防止不兼容
  600. $reason = explode("\n", $reason);
  601. return app('json')->success($reason);
  602. }
  603. /**
  604. * 获取可以退货的订单商品列表
  605. * @param Request $request
  606. * @param StoreOrderCartInfoServices $services
  607. * @param $id
  608. * @return mixed
  609. */
  610. public function refundCartInfo(Request $request, StoreOrderCartInfoServices $services, $id)
  611. {
  612. if (!$id) {
  613. return app('json')->fail(100100);
  614. }
  615. [$cart_ids] = $request->postMore([
  616. ['cart_ids', []]
  617. ], true);
  618. $list = $services->getRefundCartList((int)$id);
  619. if ($cart_ids) {
  620. foreach ($cart_ids as $cart) {
  621. if (!isset($cart['cart_id']) || !$cart['cart_id'] || !isset($cart['cart_num']) || !$cart['cart_num'] || $cart['cart_num'] <= 0) {
  622. return app('json')->fail(410223);
  623. }
  624. }
  625. $cart_ids = array_combine(array_column($cart_ids, 'cart_id'), $cart_ids);
  626. foreach ($list as &$item) {
  627. if (isset($cart_ids[$item['cart_id']]['cart_num'])) $item['cart_num'] = $cart_ids[$item['cart_id']]['cart_num'];
  628. }
  629. }
  630. return app('json')->success($list);
  631. }
  632. /**
  633. * 获取退货商品列表
  634. * @param Request $request
  635. * @return mixed
  636. * @throws DataNotFoundException
  637. * @throws DbException
  638. * @throws ModelNotFoundException
  639. */
  640. public function refundCartInfoList(Request $request)
  641. {
  642. [$cart_ids, $id] = $request->postMore([
  643. ['cart_ids', []],
  644. ['id', 0],
  645. ], true);
  646. if (!$id) {
  647. return app('json')->fail(100100);
  648. }
  649. return app('json')->success($this->services->refundCartInfoList((array)$cart_ids, (int)$id));
  650. }
  651. /**
  652. * 用户申请退款
  653. * @param Request $request
  654. * @param StoreOrderRefundServices $services
  655. * @param StoreOrderServices $storeOrderServices
  656. * @param $id
  657. * @return mixed
  658. * @throws InvalidArgumentException
  659. * @throws DataNotFoundException
  660. * @throws DbException
  661. * @throws ModelNotFoundException
  662. */
  663. public function applyRefund(Request $request, StoreOrderRefundServices $services, StoreOrderServices $storeOrderServices, $id)
  664. {
  665. if (!$id) {
  666. return app('json')->fail(100100);
  667. }
  668. $data = $request->postMore([
  669. ['text', ''],
  670. ['refund_reason_wap_img', ''],
  671. ['refund_reason_wap_explain', ''],
  672. ['refund_type', 1],
  673. ['refund_price', 0.00],
  674. ['cart_ids', []]
  675. ]);
  676. if ($data['text'] == '') return app('json')->fail(100100);
  677. if ($data['cart_ids']) {
  678. foreach ($data['cart_ids'] as $cart) {
  679. if (!isset($cart['cart_id']) || !$cart['cart_id'] || !isset($cart['cart_num']) || !$cart['cart_num']) {
  680. return app('json')->fail(410223);
  681. }
  682. }
  683. }
  684. $order = $storeOrderServices->get($id);
  685. $uid = (int)$request->uid();
  686. if (!$order || $uid != $order['uid']) {
  687. return app('json')->fail(410173);
  688. }
  689. if ($order['pid'] == -1) return app('json')->fail('主订单已拆单,请刷新页面');
  690. $refundData = [
  691. 'refund_reason' => $data['text'],
  692. 'refund_explain' => $data['refund_reason_wap_explain'],
  693. 'refund_img' => json_encode($data['refund_reason_wap_img'] != '' ? explode(',', $data['refund_reason_wap_img']) : []),
  694. ];
  695. $res = $services->applyRefund((int)$id, $uid, $order, $data['cart_ids'], (int)$data['refund_type'], (float)$data['refund_price'], $refundData);
  696. if ($res)
  697. return app('json')->success(100027);
  698. else
  699. return app('json')->fail(100028);
  700. }
  701. /**
  702. * 用户退货提交快递单号
  703. * @param Request $request
  704. * @param StoreOrderRefundServices $services
  705. * @return mixed
  706. */
  707. public function refund_express(Request $request, StoreOrderRefundServices $services)
  708. {
  709. [$id, $express_id] = $request->postMore([
  710. ['id', ''],
  711. ['express_id', '']
  712. ], true);
  713. if ($id == '' || $express_id == '') return app('json')->fail(100100);
  714. $res = $services->editRefundExpress($id, $express_id);
  715. if ($res)
  716. return app('json')->success(100017);
  717. else
  718. return app('json')->fail(100018);
  719. }
  720. /**
  721. * 订单取消 未支付的订单回退积分,回退优惠券,回退库存
  722. * @param Request $request
  723. * @return mixed
  724. * @throws DataNotFoundException
  725. * @throws DbException
  726. * @throws ModelNotFoundException
  727. */
  728. public function cancel(Request $request)
  729. {
  730. list($id) = $request->postMore([['id', 0]], true);
  731. if (!$id) return app('json')->fail(100100);
  732. if ($this->services->cancelOrder($id, (int)$request->uid()))
  733. return app('json')->success(100019);
  734. return app('json')->fail(100020);
  735. }
  736. /**
  737. * 订单商品信息
  738. * @param Request $request
  739. * @param StoreOrderCartInfoServices $services
  740. * @return mixed
  741. */
  742. public function product(Request $request, StoreOrderCartInfoServices $services)
  743. {
  744. list($unique) = $request->postMore([['unique', '']], true);
  745. if (!$unique || !($cartInfo = $services->getOne(['unique' => $unique]))) return app('json')->fail(410294);
  746. $cartInfo = $cartInfo->toArray();
  747. $cartProduct = [];
  748. $cartProduct['cart_num'] = $cartInfo['cart_info']['cart_num'];
  749. $cartProduct['productInfo']['image'] = get_thumb_water($cartInfo['cart_info']['productInfo']['image'] ?? '');
  750. $cartProduct['productInfo']['price'] = $cartInfo['cart_info']['productInfo']['price'] ?? 0;
  751. $cartProduct['productInfo']['store_name'] = $cartInfo['cart_info']['productInfo']['store_name'] ?? '';
  752. if (isset($cartInfo['cart_info']['productInfo']['attrInfo'])) {
  753. $cartProduct['productInfo']['attrInfo']['product_id'] = $cartInfo['cart_info']['productInfo']['attrInfo']['product_id'] ?? '';
  754. $cartProduct['productInfo']['attrInfo']['suk'] = $cartInfo['cart_info']['productInfo']['attrInfo']['suk'] ?? '';
  755. $cartProduct['productInfo']['attrInfo']['price'] = $cartInfo['cart_info']['productInfo']['attrInfo']['price'] ?? '';
  756. $cartProduct['productInfo']['attrInfo']['image'] = get_thumb_water($cartInfo['cart_info']['productInfo']['attrInfo']['image'] ?? '');
  757. }
  758. $cartProduct['product_id'] = $cartInfo['cart_info']['product_id'] ?? 0;
  759. $cartProduct['combination_id'] = $cartInfo['cart_info']['combination_id'] ?? 0;
  760. $cartProduct['seckill_id'] = $cartInfo['cart_info']['seckill_id'] ?? 0;
  761. $cartProduct['bargain_id'] = $cartInfo['cart_info']['bargain_id'] ?? 0;
  762. $cartProduct['order_id'] = $this->services->value(['id' => $cartInfo['oid']], 'order_id');
  763. return app('json')->success($cartProduct);
  764. }
  765. /**
  766. * 商家寄件回调
  767. * @param Request $request
  768. * @return Response
  769. * @author 等风来
  770. * @email 136327134@qq.com
  771. * @date 2023/6/12
  772. */
  773. public function callBack(Request $request)
  774. {
  775. $data = $request->postMore([
  776. ['type', ''],
  777. ['data', ''],
  778. ]);
  779. $data['data'] = $this->decrypt($data['data'], sys_config('sms_token'));
  780. switch ($data['type']) {
  781. case 'detection'://检测回调地址
  782. return \json($data['data']);
  783. break;
  784. case 'order_success'://下单成功
  785. $update = [
  786. 'label' => $data['data']['label'] ?? '',
  787. ];
  788. //韵达会异步推送单号
  789. if (isset($data['kuaidinum'])) {
  790. $update['delivery_id'] = $data['kuaidinum'];
  791. }
  792. if (isset($data['task_id'])) {
  793. $this->services->update(['kuaidi_task_id' => $data['task_id']], $update);
  794. }
  795. break;
  796. case 'order_take'://取件
  797. if (isset($data['data']['task_id'])) {
  798. $orderInfo = $this->services->get(['kuaidi_task_id' => $data['data']['task_id']]);
  799. if (!$orderInfo) {
  800. return app('json')->fail('订单不存在');
  801. }
  802. $this->services->transaction(function () use ($data, $orderInfo) {
  803. $this->services->update(['kuaidi_task_id' => $data['data']['task_id']], [
  804. 'status' => 1,
  805. 'is_stock_up' => 0
  806. ]);
  807. /** @var StoreOrderStatusServices $services */
  808. $services = app()->make(StoreOrderStatusServices::class);
  809. $services->save([
  810. 'oid' => $orderInfo->id,
  811. 'change_time' => time(),
  812. 'change_type' => 'delivery_goods',
  813. 'change_message' => '已发货 快递公司:' . $orderInfo->delivery_name . ' 快递单号:' . $orderInfo->delivery_id
  814. ]);
  815. });
  816. }
  817. break;
  818. case 'order_cancel'://取消寄件
  819. if (isset($data['data']['task_id'])) {
  820. $orderInfo = $this->services->get(['kuaidi_task_id' => $data['data']['task_id']]);
  821. if (!$orderInfo) {
  822. return app('json')->fail('订单不存在');
  823. }
  824. if ($orderInfo->is_stock_up && $orderInfo->status == 0) {
  825. app()->make(StoreOrderStatusServices::class)->save([
  826. 'oid' => $orderInfo->id,
  827. 'change_time' => time(),
  828. 'change_type' => 'delivery_goods_cancel',
  829. 'change_message' => '已取消发货,取消原因:用户手动取消'
  830. ]);
  831. $orderInfo->status = 0;
  832. $orderInfo->is_stock_up = 0;
  833. $orderInfo->kuaidi_task_id = '';
  834. $orderInfo->kuaidi_order_id = '';
  835. $orderInfo->express_dump = '';
  836. $orderInfo->kuaidi_label = '';
  837. $orderInfo->delivery_id = '';
  838. $orderInfo->delivery_code = '';
  839. $orderInfo->delivery_name = '';
  840. $orderInfo->delivery_type = '';
  841. $orderInfo->save();
  842. } else {
  843. Log::error('商家寄件自动回调,订单状态不正确:', [
  844. 'kuaidi_task_id' => $data['data']['task_id']
  845. ]);
  846. }
  847. }
  848. break;
  849. case 'success'://电子发票回调
  850. $oid = $this->services->value(['order_id' => $data['data']['unique']], 'id');
  851. if ($oid) {
  852. $invoiceServices = app()->make(StoreOrderInvoiceServices::class);
  853. $invoiceServices->update([
  854. 'category' => 'order',
  855. 'order_id' => $oid,
  856. ], [
  857. 'unique' => $data['data']['unique'],
  858. 'invoice_type' => $data['data']['invoice_type'],
  859. 'invoice_num' => $data['data']['invoice_num'],
  860. 'invoice_serial_number' => $data['data']['invoice_serial_number'],
  861. 'is_invoice' => 1,
  862. 'invoice_time' => time()
  863. ]);
  864. }
  865. break;
  866. }
  867. return app('json')->success();
  868. }
  869. /**
  870. * 解密商家寄件回调
  871. * @param string $encryptedData
  872. * @param string $key
  873. * @return false|string
  874. * @author: 吴汐
  875. * @email: 442384644@qq.com
  876. * @date: 2023/8/31
  877. */
  878. function decrypt(string $encryptedData, string $key)
  879. {
  880. $key = substr($key, 0, 32);
  881. $decodedData = base64_decode($encryptedData);
  882. $iv = substr($decodedData, 0, 16);
  883. $encrypted = substr($decodedData, 16);
  884. $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
  885. return json_decode($decrypted, true);
  886. }
  887. }