StoreOrderController.php 33 KB

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