StoreOrderController.php 35 KB

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