StoreOrderController.php 32 KB

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