StoreOrderController.php 36 KB

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