StoreOrderController.php 32 KB

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