StoreOrderController.php 41 KB

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