StoreOrderController.php 33 KB

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