StoreOrderController.php 31 KB

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