StoreOrderController.php 32 KB

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