StoreOrderController.php 36 KB

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