StoreOrderController.php 36 KB

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