StoreOrderController.php 34 KB

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