StoreOrderController.php 40 KB

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