StoreOrderController.php 34 KB

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