StoreOrderController.php 33 KB

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