StoreOrderController.php 28 KB

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