StoreOrderController.php 34 KB

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