StoreOrderController.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. <?php
  2. namespace app\api\controller\order;
  3. use app\admin\model\system\{
  4. SystemAttachment, ShippingTemplates
  5. };
  6. use app\admin\model\user\User;
  7. use app\models\routine\RoutineFormId;
  8. use crmeb\repositories\OrderRepository;
  9. use app\models\store\{
  10. StoreBargainUser,
  11. StoreCart,
  12. StoreCoupon,
  13. StoreCouponIssue,
  14. StoreCouponUser,
  15. StoreOrder,
  16. StoreOrderCartInfo,
  17. StoreOrderStatus,
  18. StorePink,
  19. StoreProductReply
  20. };
  21. use app\models\system\SystemStore;
  22. use app\models\user\UserAddress;
  23. use app\models\user\UserLevel;
  24. use app\Request;
  25. use crmeb\services\{
  26. CacheService,
  27. ExpressService,
  28. SystemConfigService,
  29. UtilService
  30. };
  31. /**
  32. * 订单类
  33. * Class StoreOrderController
  34. * @package app\api\controller\order
  35. */
  36. class StoreOrderController
  37. {
  38. /**
  39. * 订单确认
  40. * @param Request $request
  41. * @return mixed
  42. */
  43. public function confirm(Request $request)
  44. {
  45. $temp = ShippingTemplates::get(1);
  46. if (!$temp) return app('json')->fail('默认模板未配置,无法下单');
  47. list($cartId) = UtilService::postMore(['cartId'], $request, true);
  48. if (!is_string($cartId) || !$cartId) return app('json')->fail('请提交购买的商品');
  49. $uid = $request->uid();
  50. $cartGroup = StoreCart::getUserProductCartList($uid, $cartId, 1);
  51. if (count($cartGroup['invalid'])) return app('json')->fail($cartGroup['invalid'][0]['productInfo']['store_name'] . '已失效!');
  52. if (!$cartGroup['valid']) return app('json')->fail('请提交购买的商品');
  53. $cartInfo = $cartGroup['valid'];
  54. $addr = UserAddress::where('uid', $uid)->where('is_default', 1)->find();
  55. $priceGroup = StoreOrder::getOrderPriceGroup($cartInfo, $addr);
  56. if ($priceGroup === false) {
  57. return app('json')->fail(StoreOrder::getErrorInfo('运费模板不存在'));
  58. }
  59. $other = [
  60. 'offlinePostage' => sys_config('offline_postage'),
  61. 'integralRatio' => sys_config('integral_ratio')
  62. ];
  63. $usableCoupons = StoreCouponUser::getUsableCouponList($uid, $cartGroup, $priceGroup['totalPrice']);
  64. $usableCoupon = isset($usableCoupons[0]) ? $usableCoupons[0] : null;
  65. $cartIdA = explode(',', $cartId);
  66. $seckill_id = 0;
  67. $combination_id = 0;
  68. $bargain_id = 0;
  69. if (count($cartIdA) == 1) {
  70. $seckill_id = StoreCart::where('id', $cartId)->value('seckill_id');
  71. $combination_id = StoreCart::where('id', $cartId)->value('combination_id');
  72. $bargain_id = StoreCart::where('id', $cartId)->value('bargain_id');
  73. }
  74. $data['deduction'] = $seckill_id || $combination_id || $bargain_id;
  75. $data['usableCoupon'] = $usableCoupon;
  76. $data['addressInfo'] = UserAddress::getUserDefaultAddress($uid);
  77. $data['seckill_id'] = $seckill_id;
  78. $data['combination_id'] = $combination_id;
  79. $data['bargain_id'] = $bargain_id;
  80. $data['cartInfo'] = $cartInfo;
  81. $data['priceGroup'] = $priceGroup;
  82. $data['orderKey'] = StoreOrder::cacheOrderInfo($uid, $cartInfo, $priceGroup, $other);
  83. $data['offlinePostage'] = $other['offlinePostage'];
  84. $vipId = UserLevel::getUserLevel($uid);
  85. $user = $request->user();
  86. if (isset($user['pwd'])) unset($user['pwd']);
  87. $user['vip'] = $vipId !== false ? true : false;
  88. if ($user['vip']) {
  89. $user['vip_id'] = $vipId;
  90. $user['discount'] = UserLevel::getUserLevelInfo($vipId, 'discount');
  91. }
  92. $data['userInfo'] = $user;
  93. $data['integralRatio'] = $other['integralRatio'];
  94. $data['offline_pay_status'] = (int)sys_config('offline_pay_status') ?? (int)2;
  95. $data['store_self_mention'] = (int)sys_config('store_self_mention') ?? 0;//门店自提是否开启
  96. $data['system_store'] = ($res = SystemStore::getStoreDispose()) ? $res : [];//门店信息
  97. return app('json')->successful($data);
  98. }
  99. /**
  100. * 计算订单金额
  101. * @param Request $request
  102. * @param $key
  103. * @return mixed
  104. * @throws \think\Exception
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. * @throws \think\exception\DbException
  108. */
  109. public function computedOrder(Request $request, $key)
  110. {
  111. // $priceGroup = StoreOrder::getOrderPriceGroup($cartInfo);
  112. if (!$key) return app('json')->fail('参数错误!');
  113. $uid = $request->uid();
  114. if (StoreOrder::be(['order_id|unique' => $key, 'uid' => $uid, 'is_del' => 0]))
  115. return app('json')->status('extend_order', '订单已生成', ['orderId' => $key, 'key' => $key]);
  116. list($addressId, $couponId, $payType, $useIntegral, $mark, $combinationId, $pinkId, $seckill_id, $formId, $bargainId, $shipping_type) = UtilService::postMore([
  117. 'addressId', 'couponId', ['payType', 'yue'], ['useIntegral', 0], 'mark', ['combinationId', 0], ['pinkId', 0], ['seckill_id', 0], ['formId', ''], ['bargainId', ''],
  118. ['shipping_type', 1],
  119. ], $request, true);
  120. $payType = strtolower($payType);
  121. if ($bargainId) {
  122. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $uid);//TODO 获取用户参与砍价表编号
  123. if (!$bargainUserTableId)
  124. return app('json')->fail('砍价失败');
  125. $status = StoreBargainUser::getBargainUserStatusEnd($bargainUserTableId);
  126. if ($status == 3)
  127. return app('json')->fail('砍价已支付');
  128. StoreBargainUser::setBargainUserStatus($bargainId, $uid); //修改砍价状态
  129. }
  130. if ($pinkId) {
  131. if (StorePink::getIsPinkUid($pinkId, $request->uid()))
  132. return app('json')->status('ORDER_EXIST', '订单生成失败,你已经在该团内不能再参加了', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $request->uid())]);
  133. if (StoreOrder::getIsOrderPink($pinkId, $request->uid()))
  134. return app('json')->status('ORDER_EXIST', '订单生成失败,你已经参加该团了,请先支付订单', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $request->uid())]);
  135. }
  136. $priceGroup = StoreOrder::cacheKeyCreateOrder($request->uid(), $key, $addressId, $payType, (int)$useIntegral, $couponId, $mark, $combinationId, $pinkId, $seckill_id, $bargainId, true, 0, $shipping_type);
  137. if ($priceGroup)
  138. return app('json')->status('NONE', 'ok', $priceGroup);
  139. else
  140. return app('json')->fail(StoreOrder::getErrorInfo('计算失败'));
  141. }
  142. /**
  143. * 订单创建
  144. * @param Request $request
  145. * @param $key
  146. * @return mixed
  147. * @throws \think\Exception
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\ModelNotFoundException
  150. * @throws \think\exception\DbException
  151. */
  152. public function create(Request $request, $key)
  153. {
  154. if (!$key) return app('json')->fail('参数错误!');
  155. $uid = $request->uid();
  156. if (StoreOrder::be(['order_id|unique' => $key, 'uid' => $uid, 'is_del' => 0]))
  157. return app('json')->status('extend_order', '订单已生成', ['orderId' => $key, 'key' => $key]);
  158. list($addressId, $couponId, $payType, $useIntegral, $mark, $combinationId, $pinkId, $seckill_id, $formId, $bargainId, $from, $shipping_type, $real_name, $phone, $storeId) = UtilService::postMore([
  159. 'addressId', 'couponId', 'payType', ['useIntegral', 0], 'mark', ['combinationId', 0], ['pinkId', 0], ['seckill_id', 0], ['formId', ''], ['bargainId', ''], ['from', 'weixin'],
  160. ['shipping_type', 1], ['real_name', ''], ['phone', ''], ['store_id', 0]
  161. ], $request, true);
  162. $payType = strtolower($payType);
  163. if ($bargainId) {
  164. $bargainUserTableId = StoreBargainUser::getBargainUserTableId($bargainId, $uid);//TODO 获取用户参与砍价表编号
  165. if (!$bargainUserTableId)
  166. return app('json')->fail('砍价失败');
  167. $status = StoreBargainUser::getBargainUserStatusEnd($bargainUserTableId);
  168. if ($status == 3)
  169. return app('json')->fail('砍价已支付');
  170. StoreBargainUser::setBargainUserStatus($bargainId, $uid); //修改砍价状态
  171. }
  172. if ($pinkId) {
  173. if (StorePink::getIsPinkUid($pinkId, $request->uid()))
  174. return app('json')->status('ORDER_EXIST', '订单生成失败,你已经在该团内不能再参加了', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $request->uid())]);
  175. if (StoreOrder::getIsOrderPink($pinkId, $request->uid()))
  176. return app('json')->status('ORDER_EXIST', '订单生成失败,你已经参加该团了,请先支付订单', ['orderId' => StoreOrder::getStoreIdPink($pinkId, $request->uid())]);
  177. }
  178. $isChannel = 1;
  179. if ($from == 'weixin')
  180. $isChannel = 0;
  181. elseif ($from == 'weixinh5')
  182. $isChannel = 2;
  183. $order = StoreOrder::cacheKeyCreateOrder($request->uid(), $key, $addressId, $payType, (int)$useIntegral, $couponId, $mark, $combinationId, $pinkId, $seckill_id, $bargainId, false, $isChannel, $shipping_type, $real_name, $phone, $storeId);
  184. if ($order === false) return app('json')->fail(StoreOrder::getErrorInfo('订单生成失败'));
  185. $orderId = $order['order_id'];
  186. $info = compact('orderId', 'key');
  187. if ($orderId) {
  188. event('OrderCreated', [$order]); //订单创建成功事件
  189. event('ShortMssageSend', [$orderId, 'AdminPlaceAnOrder']);//发送管理员通知
  190. switch ($payType) {
  191. case "weixin":
  192. $orderInfo = StoreOrder::where('order_id', $orderId)->find();
  193. if (!$orderInfo || !isset($orderInfo['paid'])) return app('json')->fail('支付订单不存在!');
  194. $orderInfo = $orderInfo->toArray();
  195. if ($orderInfo['paid']) return app('json')->fail('支付已支付!');
  196. //支付金额为0
  197. if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
  198. //创建订单jspay支付
  199. $payPriceStatus = StoreOrder::jsPayPrice($orderId, $uid, $formId);
  200. if ($payPriceStatus)//0元支付成功
  201. return app('json')->status('success', '微信支付成功', $info);
  202. else
  203. return app('json')->status('pay_error', StoreOrder::getErrorInfo());
  204. } else {
  205. try {
  206. if ($from == 'routine') {
  207. $jsConfig = OrderRepository::jsPay($orderId); //创建订单jspay
  208. } else if ($from == 'weixinh5') {
  209. $jsConfig = OrderRepository::h5Pay($orderId);
  210. } else {
  211. $jsConfig = OrderRepository::wxPay($orderId);
  212. }
  213. } catch (\Exception $e) {
  214. return app('json')->status('pay_error', $e->getMessage(), $info);
  215. }
  216. $info['jsConfig'] = $jsConfig;
  217. if ($from == 'weixinh5') {
  218. return app('json')->status('wechat_h5_pay', '订单创建成功', $info);
  219. } else {
  220. return app('json')->status('wechat_pay', '订单创建成功', $info);
  221. }
  222. }
  223. break;
  224. case 'yue':
  225. if (StoreOrder::yuePay($orderId, $request->uid(), $formId))
  226. return app('json')->status('success', '余额支付成功', $info);
  227. else {
  228. $errorinfo = StoreOrder::getErrorInfo();
  229. if (is_array($errorinfo))
  230. return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
  231. else
  232. return app('json')->status('pay_error', $errorinfo);
  233. }
  234. break;
  235. case 'pink_integral':
  236. if (StoreOrder::pinkPay($orderId, $request->uid(), $formId))
  237. return app('json')->status('success', '拼团积分支付成功', $info);
  238. else {
  239. $errorinfo = StoreOrder::getErrorInfo();
  240. if (is_array($errorinfo))
  241. return app('json')->status($errorinfo['status'], $errorinfo['msg'], $info);
  242. else
  243. return app('json')->status('pay_error', $errorinfo);
  244. }
  245. break;
  246. case 'offline':
  247. return app('json')->status('success', '订单创建成功', $info);
  248. break;
  249. }
  250. } else return app('json')->fail(StoreOrder::getErrorInfo('订单生成失败!'));
  251. }
  252. /**
  253. * 订单 再次下单
  254. * @param Request $request
  255. * @return mixed
  256. */
  257. public function again(Request $request)
  258. {
  259. list($uni) = UtilService::postMore([
  260. ['uni', ''],
  261. ], $request, true);
  262. if (!$uni) return app('json')->fail('参数错误!');
  263. $order = StoreOrder::getUserOrderDetail($request->uid(), $uni);
  264. if (!$order) return app('json')->fail('订单不存在!');
  265. $order = StoreOrder::tidyOrder($order, true);
  266. $res = [];
  267. foreach ($order['cartInfo'] as $v) {
  268. if ($v['combination_id']) return app('json')->fail('拼团产品不能再来一单,请在拼团产品内自行下单!');
  269. else if ($v['bargain_id']) return app('json')->fail('砍价产品不能再来一单,请在砍价产品内自行下单!');
  270. else if ($v['seckill_id']) return app('json')->ail('秒杀产品不能再来一单,请在秒杀产品内自行下单!');
  271. else $res[] = StoreCart::setCart($request->uid(), $v['product_id'], $v['cart_num'], isset($v['productInfo']['attrInfo']['unique']) ? $v['productInfo']['attrInfo']['unique'] : '', 'product', 0, 0);
  272. }
  273. $cateId = [];
  274. foreach ($res as $v) {
  275. if (!$v) return app('json')->fail('再来一单失败,请重新下单!');
  276. $cateId[] = $v['id'];
  277. }
  278. event('OrderCreateAgain', implode(',', $cateId));
  279. return app('json')->successful('ok', ['cateId' => implode(',', $cateId)]);
  280. }
  281. /**
  282. * 订单支付
  283. * @param Request $request
  284. * @return mixed
  285. */
  286. public function pay(Request $request)
  287. {
  288. list($uni, $paytype, $from) = UtilService::postMore([
  289. ['uni', ''],
  290. ['paytype', 'weixin'],
  291. ['from', 'weixin']
  292. ], $request, true);
  293. if (!$uni) return app('json')->fail('参数错误!');
  294. $order = StoreOrder::getUserOrderDetail($request->uid(), $uni);
  295. if (!$order)
  296. return app('json')->fail('订单不存在!');
  297. if ($order['paid'])
  298. return app('json')->fail('该订单已支付!');
  299. if ($order['pink_id']) if (StorePink::isPinkStatus($order['pink_id']))
  300. return app('json')->fail('该订单已失效!');
  301. if ($from == 'weixin') {//0
  302. if (in_array($order->is_channel, [1, 2]))
  303. $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
  304. }
  305. if ($from == 'weixinh5') {//2
  306. if (in_array($order->is_channel, [0, 1]))
  307. $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
  308. }
  309. if ($from == 'routine') {//1
  310. if (in_array($order->is_channel, [0, 2]))
  311. $order['order_id'] = mt_rand(100, 999) . '_' . $order['order_id'];
  312. }
  313. $order['pay_type'] = $paytype; //重新支付选择支付方式
  314. switch ($order['pay_type']) {
  315. case 'weixin':
  316. try {
  317. if ($from == 'routine') {
  318. $jsConfig = OrderRepository::jsPay($order); //订单列表发起支付
  319. } else if ($from == 'weixinh5') {
  320. $jsConfig = OrderRepository::h5Pay($order);
  321. } else {
  322. $jsConfig = OrderRepository::wxPay($order);
  323. }
  324. } catch (\Exception $e) {
  325. return app('json')->fail($e->getMessage());
  326. }
  327. if ($from == 'weixinh5') {
  328. return app('json')->status('wechat_h5_pay', ['jsConfig' => $jsConfig, 'order_id' => $order['order_id']]);
  329. } else {
  330. return app('json')->status('wechat_pay', ['jsConfig' => $jsConfig, 'order_id' => $order['order_id']]);
  331. }
  332. break;
  333. case 'yue':
  334. if (StoreOrder::yuePay($order['order_id'], $request->uid()))
  335. return app('json')->status('success', '余额支付成功');
  336. else {
  337. $error = StoreOrder::getErrorInfo();
  338. return app('json')->fail(is_array($error) && isset($error['msg']) ? $error['msg'] : $error);
  339. }
  340. break;
  341. case 'offline':
  342. StoreOrder::createOrderTemplate($order);
  343. if (StoreOrder::setOrderTypePayOffline($order['order_id']))
  344. return app('json')->status('success', '订单创建成功');
  345. else
  346. return app('json')->status('success', '支付失败');
  347. break;
  348. }
  349. return app('json')->fail('支付方式错误');
  350. }
  351. /**
  352. * 订单列表
  353. * @param Request $request
  354. * @return mixed
  355. */
  356. public function lst(Request $request)
  357. {
  358. list($type, $page, $limit, $search) = UtilService::getMore([
  359. ['type', ''],
  360. ['page', 0],
  361. ['limit', ''],
  362. ['search', ''],
  363. ], $request, true);
  364. return app('json')->successful(StoreOrder::getUserOrderSearchList($request->uid(), $type, $page, $limit, $search));
  365. }
  366. /**
  367. * 订单详情
  368. * @param Request $request
  369. * @param $uni
  370. * @return mixed
  371. */
  372. public function detail(Request $request, $uni)
  373. {
  374. if (!strlen(trim($uni))) return app('json')->fail('参数错误');
  375. $order = StoreOrder::getUserOrderDetail($request->uid(), $uni);
  376. if (!$order) return app('json')->fail('订单不存在');
  377. $order = $order->toArray();
  378. //是否开启门店自提
  379. $store_self_mention = sys_config('store_self_mention');
  380. //关闭门店自提后 订单隐藏门店信息
  381. if ($store_self_mention == 0) $order['shipping_type'] = 1;
  382. if ($order['verify_code']) {
  383. $verify_code = $order['verify_code'];
  384. $verify[] = substr($verify_code, 0, 4);
  385. $verify[] = substr($verify_code, 4, 4);
  386. $verify[] = substr($verify_code, 8);
  387. $order['_verify_code'] = implode(' ', $verify);
  388. }
  389. $order['add_time_y'] = date('Y-m-d', $order['add_time']);
  390. $order['add_time_h'] = date('H:i:s', $order['add_time']);
  391. $order['system_store'] = SystemStore::getStoreDispose($order['store_id']);
  392. if ($order['shipping_type'] === 2 && $order['verify_code']) {
  393. $name = $order['verify_code'] . '.jpg';
  394. $imageInfo = SystemAttachment::getInfo($name, 'name');
  395. $siteUrl = sys_config('site_url');
  396. if (!$imageInfo) {
  397. $imageInfo = UtilService::getQRCodePath($order['verify_code'], $name);
  398. if (is_array($imageInfo)) {
  399. SystemAttachment::attachmentAdd($imageInfo['name'], $imageInfo['size'], $imageInfo['type'], $imageInfo['dir'], $imageInfo['thumb_path'], 1, $imageInfo['image_type'], $imageInfo['time'], 2);
  400. $url = $imageInfo['dir'];
  401. } else
  402. $url = '';
  403. } else $url = $imageInfo['att_dir'];
  404. if (isset($imageInfo['image_type']) && $imageInfo['image_type'] == 1) $url = $siteUrl . $url;
  405. $order['code'] = $url;
  406. }
  407. $order['mapKey'] = sys_config('tengxun_map_key');
  408. return app('json')->successful('ok', StoreOrder::tidyOrder($order, true, true));
  409. }
  410. /**
  411. * 订单删除
  412. * @param Request $request
  413. * @return mixed
  414. */
  415. public function del(Request $request)
  416. {
  417. list($uni) = UtilService::postMore([
  418. ['uni', ''],
  419. ], $request, true);
  420. if (!$uni) return app('json')->fail('参数错误!');
  421. $res = StoreOrder::removeOrder($uni, $request->uid());
  422. if ($res)
  423. return app('json')->successful();
  424. else
  425. return app('json')->fail(StoreOrder::getErrorInfo());
  426. }
  427. /**
  428. * 订单收货
  429. * @param Request $request
  430. * @return mixed
  431. */
  432. public function take(Request $request)
  433. {
  434. list($uni) = UtilService::postMore([
  435. ['uni', ''],
  436. ], $request, true);
  437. if (!$uni) return app('json')->fail('参数错误!');
  438. $res = StoreOrder::takeOrder($uni, $request->uid());
  439. if ($res) {
  440. $order_info = StoreOrder::where('order_id', $uni)->find();
  441. $gain_integral = intval($order_info['gain_integral']);
  442. $gain_coupon = StoreCouponIssue::alias('a')
  443. ->join('store_coupon b', 'a.cid = b.id')
  444. ->where('a.status', 1)
  445. ->where('a.is_full_give', 1)
  446. ->where('a.is_del', 0)
  447. ->where('a.full_reduction', '<=', $order_info['total_price'])
  448. ->sum('b.coupon_price');
  449. return app('json')->successful(['gain_integral' => $gain_integral, 'gain_coupon' => $gain_coupon]);
  450. } else
  451. return app('json')->fail(StoreOrder::getErrorInfo());
  452. }
  453. /**
  454. * 订单 查看物流
  455. * @param Request $request
  456. * @param $uni
  457. * @return mixed
  458. */
  459. public function express(Request $request, $uni)
  460. {
  461. if (!$uni || !($order = StoreOrder::getUserOrderDetail($request->uid(), $uni))) return app('json')->fail('查询订单不存在!');
  462. if ($order['delivery_type'] != 'express' || !$order['delivery_id']) return app('json')->fail('该订单不存在快递单号!');
  463. $cacheName = $uni . $order['delivery_id'];
  464. $result = CacheService::get($cacheName, null);
  465. if ($result === NULL) {
  466. $result = ExpressService::query($order['delivery_id']);
  467. if (is_array($result) &&
  468. isset($result['result']) &&
  469. isset($result['result']['deliverystatus']) &&
  470. $result['result']['deliverystatus'] >= 3)
  471. $cacheTime = 0;
  472. else
  473. $cacheTime = 1800;
  474. CacheService::set($cacheName, $result, $cacheTime);
  475. }
  476. $orderInfo = [];
  477. $cartInfo = StoreOrderCartInfo::where('oid', $order['id'])->column('cart_info', 'unique') ?? [];
  478. $info = [];
  479. $cartNew = [];
  480. foreach ($cartInfo as $k => $cart) {
  481. $cart = json_decode($cart, true);
  482. $cartNew['cart_num'] = $cart['cart_num'];
  483. $cartNew['truePrice'] = $cart['truePrice'];
  484. $cartNew['productInfo']['image'] = $cart['productInfo']['image'];
  485. $cartNew['productInfo']['store_name'] = $cart['productInfo']['store_name'];
  486. $cartNew['productInfo']['unit_name'] = $cart['productInfo']['unit_name'] ?? '';
  487. array_push($info, $cartNew);
  488. unset($cart);
  489. }
  490. $orderInfo['delivery_id'] = $order['delivery_id'];
  491. $orderInfo['delivery_name'] = $order['delivery_name'];
  492. $orderInfo['delivery_type'] = $order['delivery_type'];
  493. $orderInfo['cartInfo'] = $info;
  494. return app('json')->successful(['order' => $orderInfo, 'express' => $result ? $result : []]);
  495. }
  496. /**
  497. * 订单评价
  498. * @param Request $request
  499. * @return mixed
  500. * @throws \think\db\exception\DataNotFoundException
  501. * @throws \think\db\exception\ModelNotFoundException
  502. * @throws \think\exception\DbException
  503. */
  504. public function comment(Request $request)
  505. {
  506. $group = UtilService::postMore([
  507. ['unique', ''], ['comment', ''], ['pics', ''], ['product_score', 5], ['service_score', 5]
  508. ], $request);
  509. $unique = $group['unique'];
  510. unset($group['unique']);
  511. if (!$unique) return app('json')->fail('参数错误!');
  512. $cartInfo = StoreOrderCartInfo::where('unique', $unique)->find();
  513. $uid = $request->uid();
  514. $user_info = User::get($uid);
  515. $group['nickname'] = $user_info['nickname'];
  516. $group['avatar'] = $user_info['avatar'];
  517. if (!$cartInfo) return app('json')->fail('评价产品不存在!');
  518. $orderUid = StoreOrder::getOrderInfo($cartInfo['oid'], 'uid')['uid'];
  519. if ($uid != $orderUid) return app('json')->fail('评价产品不存在!');
  520. if (StoreProductReply::be(['oid' => $cartInfo['oid'], 'unique' => $unique]))
  521. return app('json')->fail('该产品已评价!');
  522. $group['comment'] = htmlspecialchars(trim($group['comment']));
  523. if ($group['product_score'] < 1) return app('json')->fail('请为产品评分');
  524. else if ($group['service_score'] < 1) return app('json')->fail('请为商家服务评分');
  525. if ($cartInfo['cart_info']['combination_id']) $productId = $cartInfo['cart_info']['product_id'];
  526. else if ($cartInfo['cart_info']['seckill_id']) $productId = $cartInfo['cart_info']['product_id'];
  527. else if ($cartInfo['cart_info']['bargain_id']) $productId = $cartInfo['cart_info']['product_id'];
  528. else $productId = $cartInfo['product_id'];
  529. if ($group['pics']) $group['pics'] = json_encode(is_array($group['pics']) ? $group['pics'] : explode(',', $group['pics']));
  530. $group = array_merge($group, [
  531. 'uid' => $uid,
  532. 'oid' => $cartInfo['oid'],
  533. 'unique' => $unique,
  534. 'product_id' => $productId,
  535. 'add_time' => time(),
  536. 'reply_type' => 'product'
  537. ]);
  538. StoreProductReply::beginTrans();
  539. $res = StoreProductReply::reply($group, 'product');
  540. if (!$res) {
  541. StoreProductReply::rollbackTrans();
  542. return app('json')->fail('评价失败!');
  543. }
  544. try {
  545. StoreOrder::checkOrderOver($cartInfo['oid']);
  546. } catch (\Exception $e) {
  547. StoreProductReply::rollbackTrans();
  548. return app('json')->fail($e->getMessage());
  549. }
  550. StoreProductReply::commitTrans();
  551. event('UserCommented', $res);
  552. event('AdminNewPush');
  553. return app('json')->successful();
  554. }
  555. /**
  556. * 订单统计数据
  557. * @param Request $request
  558. * @return mixed
  559. */
  560. public function data(Request $request)
  561. {
  562. return app('json')->successful(StoreOrder::getOrderData($request->uid()));
  563. }
  564. /**
  565. * 订单退款理由
  566. * @return mixed
  567. */
  568. public function refund_reason()
  569. {
  570. $reason = sys_config('stor_reason') ?: [];//退款理由
  571. $reason = str_replace("\r\n", "\n", $reason);//防止不兼容
  572. $reason = explode("\n", $reason);
  573. return app('json')->successful($reason);
  574. }
  575. /**
  576. * 订单退款审核
  577. * @param Request $request
  578. * @return mixed
  579. */
  580. public function refund_verify(Request $request)
  581. {
  582. $data = UtilService::postMore([
  583. ['text', ''],
  584. ['refund_reason_wap_img', ''],
  585. ['refund_reason_wap_explain', ''],
  586. ['uni', '']
  587. ], $request);
  588. $uni = $data['uni'];
  589. unset($data['uni']);
  590. if ($data['refund_reason_wap_img']) $data['refund_reason_wap_img'] = explode(',', $data['refund_reason_wap_img']);
  591. if (!$uni || $data['text'] == '') return app('json')->fail('参数错误!');
  592. $res = StoreOrder::orderApplyRefund($uni, $request->uid(), $data['text'], $data['refund_reason_wap_explain'], $data['refund_reason_wap_img']);
  593. if ($res)
  594. return app('json')->successful('提交申请成功');
  595. else
  596. return app('json')->fail(StoreOrder::getErrorInfo());
  597. }
  598. /**
  599. * 订单取消 未支付的订单回退积分,回退优惠券,回退库存
  600. * @param Request $request
  601. * @return mixed
  602. * @throws \think\db\exception\DataNotFoundException
  603. * @throws \think\db\exception\ModelNotFoundException
  604. * @throws \think\exception\DbException
  605. */
  606. public function cancel(Request $request)
  607. {
  608. list($id) = UtilService::postMore([['id', 0]], $request, true);
  609. if (!$id) return app('json')->fail('参数错误');
  610. if (StoreOrder::cancelOrder($id, $request->uid()))
  611. return app('json')->successful('取消订单成功');
  612. return app('json')->fail(StoreOrder::getErrorInfo('取消订单失败'));
  613. }
  614. /**
  615. * 订单产品信息
  616. * @param Request $request
  617. * @return mixed
  618. * @throws \think\db\exception\DataNotFoundException
  619. * @throws \think\db\exception\ModelNotFoundException
  620. * @throws \think\exception\DbException
  621. */
  622. public function product(Request $request)
  623. {
  624. list($unique) = UtilService::postMore([['unique', '']], $request, true);
  625. if (!$unique || !StoreOrderCartInfo::be(['unique' => $unique]) || !($cartInfo = StoreOrderCartInfo::where('unique', $unique)->find())) return app('json')->fail('评价产品不存在!');
  626. $cartInfo = $cartInfo->toArray();
  627. $cartProduct = [];
  628. $cartProduct['cart_num'] = $cartInfo['cart_info']['cart_num'];
  629. $cartProduct['productInfo']['image'] = isset($cartInfo['cart_info']['productInfo']['image']) ? $cartInfo['cart_info']['productInfo']['image'] : '';
  630. $cartProduct['productInfo']['price'] = isset($cartInfo['cart_info']['productInfo']['price']) ? $cartInfo['cart_info']['productInfo']['price'] : 0;
  631. $cartProduct['productInfo']['store_name'] = isset($cartInfo['cart_info']['productInfo']['store_name']) ? $cartInfo['cart_info']['productInfo']['store_name'] : '';
  632. if (isset($cartInfo['cart_info']['productInfo']['attrInfo'])) {
  633. $cartProduct['productInfo']['attrInfo']['product_id'] = isset($cartInfo['cart_info']['productInfo']['attrInfo']['product_id']) ? $cartInfo['cart_info']['productInfo']['attrInfo']['product_id'] : '';
  634. $cartProduct['productInfo']['attrInfo']['suk'] = isset($cartInfo['cart_info']['productInfo']['attrInfo']['suk']) ? $cartInfo['cart_info']['productInfo']['attrInfo']['suk'] : '';
  635. $cartProduct['productInfo']['attrInfo']['price'] = isset($cartInfo['cart_info']['productInfo']['attrInfo']['price']) ? $cartInfo['cart_info']['productInfo']['attrInfo']['price'] : '';
  636. $cartProduct['productInfo']['attrInfo']['image'] = isset($cartInfo['cart_info']['productInfo']['attrInfo']['image']) ? $cartInfo['cart_info']['productInfo']['attrInfo']['image'] : '';
  637. }
  638. $cartProduct['product_id'] = isset($cartInfo['cart_info']['product_id']) ? $cartInfo['cart_info']['product_id'] : 0;
  639. $cartProduct['combination_id'] = isset($cartInfo['cart_info']['combination_id']) ? $cartInfo['cart_info']['combination_id'] : 0;
  640. $cartProduct['seckill_id'] = isset($cartInfo['cart_info']['seckill_id']) ? $cartInfo['cart_info']['seckill_id'] : 0;
  641. $cartProduct['bargain_id'] = isset($cartInfo['cart_info']['bargain_id']) ? $cartInfo['cart_info']['bargain_id'] : 0;
  642. $cartProduct['order_id'] = StoreOrder::where('id', $cartInfo['oid'])->value('order_id');
  643. return app('json')->successful($cartProduct);
  644. }
  645. /**
  646. * 首页获取未支付订单
  647. */
  648. public function get_noPay(Request $request)
  649. {
  650. return app('json')->successful(StoreOrder::getUserOrderSearchList($request->uid(), 0, 0, 0, ''));
  651. }
  652. }