StoreOrderController.php 40 KB

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