UserController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\http\validates\user\AddressValidate;
  4. use app\models\system\SystemCity;
  5. use app\models\user\UserVisit;
  6. use think\db\exception\DataNotFoundException;
  7. use think\db\exception\DbException;
  8. use think\db\exception\ModelNotFoundException;
  9. use think\exception\ValidateException;
  10. use app\Request;
  11. use app\models\user\UserLevel;
  12. use app\models\user\UserSign;
  13. use app\models\store\StoreBargain;
  14. use app\models\store\StoreCombination;
  15. use app\models\store\StoreCouponUser;
  16. use app\models\store\StoreOrder;
  17. use app\models\store\StoreProductRelation;
  18. use app\models\store\StoreSeckill;
  19. use app\models\user\User;
  20. use app\models\user\UserAddress;
  21. use app\models\user\UserBill;
  22. use app\models\user\UserExtract;
  23. use app\models\user\UserNotice;
  24. use crmeb\services\GroupDataService;
  25. use crmeb\services\UtilService;
  26. use crmeb\basic\BaseModel;
  27. /**
  28. * 用户类
  29. * Class UserController
  30. * @package app\api\controller\store
  31. */
  32. class UserController
  33. {
  34. /**
  35. * 获取用户信息
  36. * @param Request $request
  37. * @return mixed
  38. */
  39. public function userInfo(Request $request)
  40. {
  41. $info = $request->user()->toArray();
  42. $broken_time = intval(sys_config('extract_time'));
  43. $search_time = time() - 86400 * $broken_time;
  44. //返佣 +
  45. $brokerage_commission = UserBill::where(['uid' => $info['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  46. ->where('add_time', '>', $search_time)
  47. ->where('pm', 1)
  48. ->sum('number');
  49. //退款退的佣金 -
  50. $refund_commission = UserBill::where(['uid' => $info['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  51. ->where('add_time', '>', $search_time)
  52. ->where('pm', 0)
  53. ->sum('number');
  54. $info['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  55. if ($info['broken_commission'] < 0)
  56. $info['broken_commission'] = 0;
  57. $info['commissionCount'] = bcsub($info['brokerage_price'], $info['broken_commission'], 2);
  58. if ($info['commissionCount'] < 0)
  59. $info['commissionCount'] = 0;
  60. return app('json')->success($info);
  61. }
  62. /**
  63. * 用户资金统计
  64. * @param Request $request
  65. * @return mixed
  66. * @throws \think\Exception
  67. * @throws DataNotFoundException
  68. * @throws ModelNotFoundException
  69. * @throws \think\exception\DbException
  70. */
  71. public function balance(Request $request)
  72. {
  73. $uid = $request->uid();
  74. $user['now_money'] = User::getUserInfo($uid, 'now_money')['now_money'];//当前总资金
  75. $user['recharge'] = UserBill::getRecharge($uid);//累计充值
  76. $user['orderStatusSum'] = StoreOrder::getOrderStatusSum($uid);//累计消费
  77. return app('json')->successful($user);
  78. }
  79. /**
  80. * 个人中心
  81. * @param Request $request
  82. * @return mixed
  83. */
  84. public function user(Request $request)
  85. {
  86. $user = $request->user();
  87. $user = $user->toArray();
  88. $user['couponCount'] = StoreCouponUser::getUserValidCouponCount($user['uid']);
  89. $user['like'] = StoreProductRelation::getUserIdCollect($user['uid']);
  90. $user['orderStatusNum'] = StoreOrder::getOrderData($user['uid']);
  91. $user['notice'] = UserNotice::getNotice($user['uid']);
  92. // $user['brokerage'] = UserBill::getBrokerage($user['uid']);//获取总佣金
  93. $user['recharge'] = UserBill::getRecharge($user['uid']);//累计充值
  94. $user['orderStatusSum'] = StoreOrder::getOrderStatusSum($user['uid']);//累计消费
  95. $user['extractTotalPrice'] = UserExtract::userExtractTotalPrice($user['uid']);//累计提现
  96. $user['extractPrice'] = $user['brokerage_price'];//可提现
  97. $user['statu'] = (int)sys_config('store_brokerage_statu');
  98. $broken_time = intval(sys_config('extract_time'));
  99. $search_time = time() - 86400 * $broken_time;
  100. if (!$user['is_promoter'] && $user['statu'] == 2) {
  101. $price = StoreOrder::where(['paid' => 1, 'refund_status' => 0, 'uid' => $user['uid']])->sum('pay_price');
  102. $status = is_brokerage_statu($price);
  103. if ($status) {
  104. User::where('uid', $user['uid'])->update(['is_promoter' => 1]);
  105. $user['is_promoter'] = 1;
  106. } else {
  107. $storeBrokeragePrice = sys_config('store_brokerage_price', 0);
  108. $user['promoter_price'] = bcsub($storeBrokeragePrice, $price, 2);
  109. }
  110. }
  111. //可提现佣金
  112. //返佣 +
  113. $brokerage_commission = UserBill::where(['uid' => $user['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  114. ->where('add_time', '>', $search_time)
  115. ->where('pm', 1)
  116. ->sum('number');
  117. //退款退的佣金 -
  118. $refund_commission = UserBill::where(['uid' => $user['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  119. ->where('add_time', '>', $search_time)
  120. ->where('pm', 0)
  121. ->sum('number');
  122. $user['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  123. if ($user['broken_commission'] < 0)
  124. $user['broken_commission'] = 0;
  125. $user['commissionCount'] = bcsub($user['brokerage_price'], $user['broken_commission'], 2);
  126. if ($user['commissionCount'] < 0)
  127. $user['commissionCount'] = 0;
  128. if (!sys_config('vip_open'))
  129. $user['vip'] = false;
  130. else {
  131. $vipId = UserLevel::getUserLevel($user['uid']);
  132. $user['vip'] = $vipId !== false ? true : false;
  133. if ($user['vip']) {
  134. $user['vip_id'] = $vipId;
  135. $user['vip_icon'] = UserLevel::getUserLevelInfo($vipId, 'icon');
  136. $user['vip_name'] = UserLevel::getUserLevelInfo($vipId, 'name');
  137. }
  138. }
  139. $user['yesterDay'] = UserBill::yesterdayCommissionSum($user['uid']);
  140. $user['recharge_switch'] = (int)sys_config('recharge_switch');//充值开关
  141. $user['adminid'] = (boolean)\app\models\store\StoreService::orderServiceStatus($user['uid']);
  142. if ($user['phone'] && $user['user_type'] != 'h5') {
  143. $user['switchUserInfo'][] = $request->user();
  144. if ($h5UserInfo = User::where('account', $user['phone'])->where('user_type', 'h5')->find()) {
  145. $user['switchUserInfo'][] = $h5UserInfo;
  146. }
  147. } else if ($user['phone'] && $user['user_type'] == 'h5') {
  148. if ($wechatUserInfo = User::where('phone', $user['phone'])->where('user_type', '<>', 'h5')->find()) {
  149. $user['switchUserInfo'][] = $wechatUserInfo;
  150. }
  151. $user['switchUserInfo'][] = $request->user();
  152. } else if (!$user['phone']) {
  153. $user['switchUserInfo'][] = $request->user();
  154. }
  155. return app('json')->successful($user);
  156. }
  157. /**
  158. * 地址 获取单个
  159. * @param Request $request
  160. * @param $id
  161. * @return mixed
  162. * @throws DataNotFoundException
  163. * @throws ModelNotFoundException
  164. * @throws \think\exception\DbException
  165. */
  166. public function address(Request $request, $id)
  167. {
  168. $addressInfo = [];
  169. if ($id && is_numeric($id) && UserAddress::be(['is_del' => 0, 'id' => $id, 'uid' => $request->uid()])) {
  170. $addressInfo = UserAddress::find($id)->toArray();
  171. }
  172. return app('json')->successful($addressInfo);
  173. }
  174. /**
  175. * 地址列表
  176. * @param Request $request
  177. * @param $page
  178. * @param $limit
  179. * @return mixed
  180. */
  181. public function address_list(Request $request)
  182. {
  183. list($page, $limit) = UtilService::getMore([['page', 0], ['limit', 20]], $request, true);
  184. $list = UserAddress::getUserValidAddressList($request->uid(), $page, $limit, 'id,real_name,phone,province,city,district,detail,is_default');
  185. return app('json')->successful($list);
  186. }
  187. /**
  188. * 设置默认地址
  189. *
  190. * @param Request $request
  191. * @return mixed
  192. */
  193. public function address_default_set(Request $request)
  194. {
  195. list($id) = UtilService::getMore([['id', 0]], $request, true);
  196. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  197. if (!UserAddress::be(['is_del' => 0, 'id' => $id, 'uid' => $request->uid()]))
  198. return app('json')->fail('地址不存在!');
  199. $res = UserAddress::setDefaultAddress($id, $request->uid());
  200. if (!$res)
  201. return app('json')->fail('地址不存在!');
  202. else
  203. return app('json')->successful();
  204. }
  205. /**
  206. * 获取默认地址
  207. * @param Request $request
  208. * @return mixed
  209. */
  210. public function address_default(Request $request)
  211. {
  212. $defaultAddress = UserAddress::getUserDefaultAddress($request->uid(), 'id,real_name,phone,province,city,district,detail,is_default');
  213. if ($defaultAddress) {
  214. $defaultAddress = $defaultAddress->toArray();
  215. return app('json')->successful('ok', $defaultAddress);
  216. }
  217. return app('json')->successful('empty', []);
  218. }
  219. /**
  220. * 修改 添加地址
  221. * @param Request $request
  222. * @return mixed
  223. */
  224. public function address_edit(Request $request)
  225. {
  226. $addressInfo = UtilService::postMore([
  227. ['address', []],
  228. ['is_default', false],
  229. ['real_name', ''],
  230. ['post_code', ''],
  231. ['phone', ''],
  232. ['detail', ''],
  233. ['id', 0],
  234. ['type', 0]
  235. ], $request);
  236. if (!isset($addressInfo['address']['province'])) return app('json')->fail('收货地址格式错误!');
  237. if (!isset($addressInfo['address']['city'])) return app('json')->fail('收货地址格式错误!');
  238. if (!isset($addressInfo['address']['district'])) return app('json')->fail('收货地址格式错误!');
  239. if (!isset($addressInfo['address']['city_id']) && $addressInfo['type'] == 0) {
  240. return app('json')->fail('收货地址格式错误!请重新选择!');
  241. } else if ($addressInfo['type'] == 1) {
  242. $city = $addressInfo['address']['city'];
  243. $cityId = SystemCity::where('name', $city)->where('parent_id', '<>', 0)->value('city_id');
  244. if ($cityId) {
  245. $addressInfo['address']['city_id'] = $cityId;
  246. } else {
  247. if (!($cityId = SystemCity::where('parent_id', '<>', 0)->where('name', 'like', "%$city%")->value('city_id'))) {
  248. return app('json')->fail('收货地址格式错误!修改后请重新导入!');
  249. }
  250. $addressInfo['address']['city_id'] = $cityId;
  251. }
  252. }
  253. $addressInfo['province'] = $addressInfo['address']['province'];
  254. $addressInfo['city'] = $addressInfo['address']['city'];
  255. $addressInfo['city_id'] = $addressInfo['address']['city_id'] ?? 0;
  256. $addressInfo['district'] = $addressInfo['address']['district'];
  257. $addressInfo['is_default'] = (int)$addressInfo['is_default'] == true ? 1 : 0;
  258. $addressInfo['uid'] = $request->uid();
  259. unset($addressInfo['address'], $addressInfo['type']);
  260. try {
  261. validate(AddressValidate::class)->check($addressInfo);
  262. } catch (ValidateException $e) {
  263. return app('json')->fail($e->getError());
  264. }
  265. if ($addressInfo['id'] && UserAddress::be(['id' => $addressInfo['id'], 'uid' => $request->uid(), 'is_del' => 0])) {
  266. $id = $addressInfo['id'];
  267. unset($addressInfo['id']);
  268. if (UserAddress::edit($addressInfo, $id, 'id')) {
  269. if ($addressInfo['is_default'])
  270. UserAddress::setDefaultAddress($id, $request->uid());
  271. return app('json')->successful();
  272. } else
  273. return app('json')->fail('编辑收货地址失败!');
  274. } else {
  275. $addressInfo['add_time'] = time();
  276. if ($address = UserAddress::create($addressInfo)) {
  277. if ($addressInfo['is_default']) {
  278. UserAddress::setDefaultAddress($address->id, $request->uid());
  279. }
  280. return app('json')->successful(['id' => $address->id]);
  281. } else {
  282. return app('json')->fail('添加收货地址失败!');
  283. }
  284. }
  285. }
  286. /**
  287. * 删除地址
  288. *
  289. * @param Request $request
  290. * @return mixed
  291. */
  292. public function address_del(Request $request)
  293. {
  294. list($id) = UtilService::postMore([['id', 0]], $request, true);
  295. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误!');
  296. if (!UserAddress::be(['is_del' => 0, 'id' => $id, 'uid' => $request->uid()]))
  297. return app('json')->fail('地址不存在!');
  298. if (UserAddress::edit(['is_del' => '1'], $id, 'id'))
  299. return app('json')->successful();
  300. else
  301. return app('json')->fail('删除地址失败!');
  302. }
  303. /**
  304. * 获取收藏产品
  305. *
  306. * @param Request $request
  307. * @return mixed
  308. */
  309. public function collect_user(Request $request)
  310. {
  311. list($page, $limit) = UtilService::getMore([
  312. ['page', 0],
  313. ['limit', 0]
  314. ], $request, true);
  315. if (!(int)$limit) return app('json')->successful([]);
  316. $productRelationList = StoreProductRelation::getUserCollectProduct($request->uid(), (int)$page, (int)$limit);
  317. return app('json')->successful($productRelationList);
  318. }
  319. /**
  320. * 添加收藏
  321. * @param Request $request
  322. * @param $id
  323. * @param $category
  324. * @return mixed
  325. */
  326. public function collect_add(Request $request)
  327. {
  328. list($id, $category) = UtilService::postMore([['id', 0], ['category', 'product']], $request, true);
  329. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误');
  330. $res = StoreProductRelation::productRelation($id, $request->uid(), 'collect', $category);
  331. if (!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  332. else return app('json')->successful();
  333. }
  334. /**
  335. * 取消收藏
  336. *
  337. * @param Request $request
  338. * @return mixed
  339. */
  340. public function collect_del(Request $request)
  341. {
  342. list($id, $category) = UtilService::postMore([['id', 0], ['category', 'product']], $request, true);
  343. if (!$id || !is_numeric($id)) return app('json')->fail('参数错误');
  344. $res = StoreProductRelation::unProductRelation($id, $request->uid(), 'collect', $category);
  345. if (!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  346. else return app('json')->successful();
  347. }
  348. /**
  349. * 批量收藏
  350. * @param Request $request
  351. * @return mixed
  352. */
  353. public function collect_all(Request $request)
  354. {
  355. $collectInfo = UtilService::postMore([
  356. ['id', []],
  357. ['category', 'product'],
  358. ], $request);
  359. if (!count($collectInfo['id'])) return app('json')->fail('参数错误');
  360. $productIdS = $collectInfo['id'];
  361. $res = StoreProductRelation::productRelationAll($productIdS, $request->uid(), 'collect', $collectInfo['category']);
  362. if (!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  363. else return app('json')->successful('收藏成功');
  364. }
  365. /**
  366. * 添加点赞
  367. *
  368. * @param Request $request
  369. * @return mixed
  370. */
  371. // public function like_add(Request $request)
  372. // {
  373. // list($id, $category) = UtilService::postMore([['id',0], ['category','product']], $request, true);
  374. // if(!$id || !is_numeric($id)) return app('json')->fail('参数错误');
  375. // $res = StoreProductRelation::productRelation($id,$request->uid(),'like',$category);
  376. // if(!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  377. // else return app('json')->successful();
  378. // }
  379. /**
  380. * 取消点赞
  381. *
  382. * @param Request $request
  383. * @return mixed
  384. */
  385. // public function like_del(Request $request)
  386. // {
  387. // list($id, $category) = UtilService::postMore([['id',0], ['category','product']], $request, true);
  388. // if(!$id || !is_numeric($id)) return app('json')->fail('参数错误');
  389. // $res = StoreProductRelation::unProductRelation($id, $request->uid(),'like',$category);
  390. // if(!$res) return app('json')->fail(StoreProductRelation::getErrorInfo());
  391. // else return app('json')->successful();
  392. // }
  393. /**
  394. * 签到 配置
  395. * @return mixed
  396. * @throws DataNotFoundException
  397. * @throws ModelNotFoundException
  398. * @throws \think\exception\DbException
  399. */
  400. public function sign_config()
  401. {
  402. $signConfig = sys_data('sign_day_num') ?? [];
  403. return app('json')->successful($signConfig);
  404. }
  405. /**
  406. * 签到 列表
  407. * @param Request $request
  408. * @param $page
  409. * @param $limit
  410. * @return mixed
  411. */
  412. public function sign_list(Request $request)
  413. {
  414. list($page, $limit) = UtilService::getMore([
  415. ['page', 0],
  416. ['limit', 0]
  417. ], $request, true);
  418. if (!$limit) return app('json')->successful([]);
  419. $signList = UserSign::getSignList($request->uid(), (int)$page, (int)$limit);
  420. if ($signList) $signList = $signList->toArray();
  421. return app('json')->successful($signList);
  422. }
  423. /**
  424. * 签到
  425. * @param Request $request
  426. * @return mixed
  427. */
  428. public function sign_integral(Request $request)
  429. {
  430. $signed = UserSign::getIsSign($request->uid());
  431. if ($signed) return app('json')->fail('已签到');
  432. if (false !== ($integral = UserSign::sign($request->uid())))
  433. return app('json')->successful('签到获得' . floatval($integral) . '积分', ['integral' => $integral]);
  434. return app('json')->fail(UserSign::getErrorInfo('签到失败'));
  435. }
  436. /**
  437. * 签到用户信息
  438. * @param Request $request
  439. * @return mixed
  440. */
  441. public function sign_user(Request $request)
  442. {
  443. list($sign, $integral, $all) = UtilService::postMore([
  444. ['sign', 0],
  445. ['integral', 0],
  446. ['all', 0],
  447. ], $request, true);
  448. $user = $request->user();
  449. //是否统计签到
  450. if ($sign || $all) {
  451. $user['sum_sgin_day'] = UserSign::getSignSumDay($user['uid']);
  452. $user['is_day_sgin'] = UserSign::getIsSign($user['uid']);
  453. $user['is_YesterDay_sgin'] = UserSign::getIsSign($user['uid'], 'yesterday');
  454. if (!$user['is_day_sgin'] && !$user['is_YesterDay_sgin']) {
  455. $user['sign_num'] = 0;
  456. }
  457. }
  458. //是否统计积分使用情况
  459. if ($integral || $all) {
  460. $user['sum_integral'] = (int)UserBill::getRecordCount($user['uid'], 'integral', 'sign,system_add,gain');
  461. $user['deduction_integral'] = (int)UserBill::getRecordCount($user['uid'], 'integral', 'deduction', '', true) ?? 0;
  462. $user['today_integral'] = (int)UserBill::getRecordCount($user['uid'], 'integral', 'sign,system_add,gain', 'today');
  463. }
  464. unset($user['pwd']);
  465. if (!$user['is_promoter']) {
  466. $user['is_promoter'] = (int)sys_config('store_brokerage_statu') == 2 ? true : false;
  467. }
  468. return app('json')->successful($user->hidden(['account', 'real_name', 'birthday', 'card_id', 'mark', 'partner_id', 'group_id', 'add_time', 'add_ip', 'phone', 'last_time', 'last_ip', 'spread_uid', 'spread_time', 'user_type', 'status', 'level', 'clean_time', 'addres'])->toArray());
  469. }
  470. /**
  471. * 签到列表(年月)
  472. *
  473. * @param Request $request
  474. * @return mixed
  475. */
  476. public function sign_month(Request $request)
  477. {
  478. list($page, $limit) = UtilService::getMore([
  479. ['page', 0],
  480. ['limit', 0]
  481. ], $request, true);
  482. if (!$limit) return app('json')->successful([]);
  483. $userSignList = UserSign::getSignMonthList($request->uid(), (int)$page, (int)$limit);
  484. return app('json')->successful($userSignList);
  485. }
  486. /**
  487. * 获取活动状态
  488. * @return mixed
  489. */
  490. public function activity()
  491. {
  492. $data['is_bargin'] = StoreBargain::validBargain() ? true : false;
  493. $data['is_pink'] = StoreCombination::getPinkIsOpen() ? true : false;
  494. $data['is_seckill'] = StoreSeckill::getSeckillCount() ? true : false;
  495. return app('json')->successful($data);
  496. }
  497. /**
  498. * 用户修改信息
  499. * @param Request $request
  500. * @return mixed
  501. */
  502. public function edit(Request $request)
  503. {
  504. list($avatar, $nickname) = UtilService::postMore([
  505. ['avatar', ''],
  506. ['nickname', ''],
  507. ], $request, true);
  508. if (User::editUser($avatar, $nickname, $request->uid())) return app('json')->successful('修改成功');
  509. return app('json')->fail('修改失败');
  510. }
  511. /**
  512. * 推广人排行
  513. * @param Request $request
  514. * @return mixed
  515. * @throws DataNotFoundException
  516. * @throws ModelNotFoundException
  517. * @throws \think\exception\DbException
  518. */
  519. public function rank(Request $request)
  520. {
  521. $data = UtilService::getMore([
  522. ['page', ''],
  523. ['limit', ''],
  524. ['type', '']
  525. ], $request);
  526. $users = User::getRankList($data);
  527. return app('json')->success($users);
  528. }
  529. /**
  530. * 佣金排行
  531. * @param Request $request
  532. * @return mixed
  533. */
  534. public function brokerage_rank(Request $request)
  535. {
  536. $data = UtilService::getMore([
  537. ['page', ''],
  538. ['limit'],
  539. ['type']
  540. ], $request);
  541. return app('json')->success([
  542. 'rank' => User::brokerageRank($data),
  543. 'position' => User::currentUserRank($data['type'], $request->user()['brokerage_price'])
  544. ]);
  545. }
  546. /**
  547. * 添加访问记录
  548. * @param Request $request
  549. * @return mixed
  550. */
  551. public function set_visit(Request $request)
  552. {
  553. $data = UtilService::postMore([
  554. ['url', ''],
  555. ['stay_time', 0]
  556. ], $request);
  557. if ($data['url'] == '') return app('json')->fail('未获取页面路径');
  558. $data['uid'] = $request->uid();
  559. $data['ip'] = $request->ip();
  560. $data['add_time'] = time();
  561. $res = UserVisit::insert($data);
  562. if ($res) {
  563. return app('json')->success('添加访问记录成功');
  564. } else {
  565. return app('json')->fail('添加访问记录失败');
  566. }
  567. }
  568. /**
  569. * 静默绑定推广人
  570. * @param Request $request
  571. * @return mixed
  572. * @throws DataNotFoundException
  573. * @throws DbException
  574. * @throws ModelNotFoundException
  575. */
  576. public function spread(Request $request)
  577. {
  578. $puid = $request->post('puid/d', 0);
  579. return app('json')->success(User::setSpread($puid, $request->uid()));
  580. }
  581. /**
  582. * @param Request $request
  583. * @return mixed
  584. * @throws DataNotFoundException
  585. * @throws DbException
  586. * @throws ModelNotFoundException
  587. */
  588. public function set_parent(Request $request)
  589. {
  590. $uid = $request->param('uid');
  591. $user = User::getUserInfo($uid);
  592. if ($user['spread_uid']) return app('json')->fail('该用户已有上级');
  593. if ($user['is_best'] != 1) return app('json')->fail('请先购买会员商品');
  594. $p_user = User::where('is_best', 1)->orderBy('id')->find();
  595. if (!$p_user) return app('json')->fail('暂无符合条件的上级');
  596. $user_c = User::where('parent', $p_user['uid'])->find();
  597. $user_ab = User::where('parent', $p_user['uid'])->where('parent_area', 'in', ['A', 'B'])->find();
  598. if (!$user_c && !$user_ab) return app('json')->fail('上级的ABC区均无空位');
  599. if (!$user_c) $parent = $p_user['uid']; $parent_area = 'C';
  600. if (!$user_ab) $parent = $p_user['uid']; $parent_area = 'A';
  601. if ($user_ab && $user_ab['parent_area'] == 'A') $parent = $p_user['uid']; $parent_area = 'B';
  602. if ($parent != $p_user['uid'] || $parent_area != $parent_area) {
  603. return app('json')->fail('要设置的上级或区域不正确');
  604. }
  605. BaseModel::beginTrans();
  606. try{
  607. User::where('uid', $uid)->update(['parent' => $parent, 'parent_area' => $parent_area]);
  608. $res = User::setParentUser($uid, $parent, $parent_area);
  609. if ($res) {
  610. BaseModel::commitTrans();
  611. } else {
  612. BaseModel::rollbackTrans();
  613. return app('json')->fail(User::getErrorInfo('设置失败'));
  614. }
  615. } catch (\Exception $e) {
  616. BaseModel::rollbackTrans();
  617. return app('json')->fail('设置失败' . $e->getMessage());
  618. }
  619. return [
  620. 'parent' => $parent,
  621. 'parent_area' => $parent_area
  622. ];
  623. }
  624. public function getLowerUser(Request $request)
  625. {
  626. $uid = $request->param('uid');
  627. if (!$uid) return app('json')->fail('请传递用户UID');
  628. $user = User::get($uid);
  629. if (!$user) return app('json')->fail('用户不存在');
  630. $A_user = User::where('parent', $uid)->where('parent_area', 'A')->field('uid,nickname')->find();
  631. $B_user = User::where('parent', $uid)->where('parent_area', 'B')->field('uid,nickname')->find();
  632. $C_user = User::where('parent', $uid)->where('parent_area', 'C')->field('uid,nickname')->find();
  633. return app('json')->success([
  634. 'A' => $A_user,
  635. 'B' => $B_user,
  636. 'C' => $C_user
  637. ]);
  638. }
  639. }