StoreOrderController.php 40 KB

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