StoreOrderController.php 37 KB

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