UserManager.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. <?php
  2. namespace App\Managers;
  3. use App\Jobs\MockThumbJob;
  4. use App\Models\Deed\FriendsModel;
  5. use App\Models\Deed\InvitationCardModel;
  6. use App\Models\Log\AppSignModel;
  7. use App\Models\Log\FeedLogModel;
  8. use App\Models\PraiseModel;
  9. use App\Models\User\FeedType4RemindModel;
  10. use App\Models\User\InviteConfigModel;
  11. use App\Models\User\LocklikemeModel;
  12. use App\Models\User\SuperLikeModel;
  13. use App\Models\User\UserSysTagModel;
  14. use App\Services\Goodnight\UserService;
  15. use Carbon\Carbon;
  16. use Illuminate\Database\Eloquent\Builder;
  17. use Illuminate\Database\Eloquent\Collection;
  18. use Illuminate\Support\Facades\Config;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Facades\Redis;
  21. use App\Models\User\UserModel;
  22. use App\Models\PartnerModel;
  23. use App\Models\User\Openid;
  24. use App\Models\BlackListModel;
  25. use App\Models\TagModel;
  26. class UserManager
  27. {
  28. public function fpdxProfile(int $uid)
  29. {
  30. $userModel = new UserModel();
  31. $userModel->initUser($uid);
  32. $user = UserModel::findOrFail($uid);
  33. /** @var UserModel $user */
  34. $user->login_at = time();
  35. if (in_array(Config::get('platform'), ['ios', 'android'])) {
  36. $user->login_app_platform = Config::get("platform");
  37. }
  38. $user->save();
  39. if ($inviteConf = InviteConfigModel::find($uid)) {
  40. $user->task_photo = $inviteConf->task_photo;
  41. $user->task_question = $inviteConf->task_question;
  42. $user->task_sing = $inviteConf->task_sing;
  43. $user->task_question_data = $inviteConf->task_question_data ? true : false;
  44. } else {
  45. $user->task_photo = false;
  46. $user->task_question = false;
  47. $user->task_sing = false;
  48. }
  49. // 普通会员
  50. // 超级VIP相关
  51. $superVip = [
  52. 'supvip_endat' => 0,
  53. ];
  54. if ($user->supvip_endat > time()) {
  55. // 累计开通supvip天数
  56. $systag = UserSysTagModel::where('uid', $uid)->first();
  57. $superVip['supvip_days'] = $systag ? $systag->supvip_days : 0;
  58. // 每天心动我的解锁次数
  59. $superVip['locklikeme_cnt'] = 5 - LocklikemeModel::where([
  60. ['uid', $user->uid],
  61. ['created_at', '>', mktime(0, 0, 0)],
  62. ['origin', 'supvip']
  63. ])->count();
  64. // 到期时间
  65. $superVip['supvip_endat'] = $user->supvip_endat;
  66. }
  67. $user->supvip = $superVip;
  68. // 标签处理
  69. $tagModel = new TagModel();
  70. $user->tags = $tagModel->getAllTagByUser($user);
  71. // 是否关注fpdx
  72. $user->bind_fpdxfwh = (int)Openid::where([
  73. ['uid', $uid],
  74. ['subscribe', 1],
  75. ['public_id', 'gh_b598cb7474d8']
  76. ])->exists();
  77. // 黑名单
  78. $black = BlackListModel::where('uid', $uid)
  79. ->where('end_at', '>', time())
  80. ->whereIn('type', ['msy', 'all'])
  81. ->first();
  82. // 卡片和解锁卡统计
  83. $banner = [
  84. 'is_praises' => 0,
  85. 'charm' => 0,
  86. 'count' => PartnerModel::where([['uid', $uid], ['is_hide', 0]])->count(),
  87. 'black' => $black ? $black->end_at : 0,
  88. ];
  89. if ($user->partner_id > 0) {
  90. $partner = PartnerModel::findOrFail($user->partner_id, ['praises', 'charm']);
  91. $banner['is_praises'] = $partner->praises;
  92. $banner['charm'] = $partner->charm;
  93. }
  94. $user->lock_card = 0;
  95. $user->banner = $banner;
  96. // 黑名单
  97. $user->black = $user->black();
  98. // App签到
  99. $user->app_sign_at = AppSignModel::where('uid', $uid)->where('date', date('Y-m-d'))->first()
  100. ? mktime(0, 0, 0) + 86400
  101. : mktime(0, 0, 0);
  102. MockThumbJob::dispatch($uid)->delay(Carbon::now()->addHours(1));
  103. $user->course = Redis::getbit("app:course", $uid);
  104. return $user;
  105. }
  106. public function profile(int $uid, $app)
  107. {
  108. $userModel = new UserModel();
  109. $userModel->initUser($uid);
  110. switch ($app) {
  111. case "goodnight":
  112. $user = UserModel::findOrFail($uid, ['uid', 'headimgurl', 'nickname', 'phone', 'sex']);
  113. /** @var \App\Models\User\UserModel $user */
  114. $userService = new UserService();
  115. $userService->getUser($user);
  116. break;
  117. case "fpdx":
  118. default:
  119. $user = UserModel::findOrFail($uid);
  120. /** @var UserModel $user */
  121. $user->login_at = time();
  122. if (in_array(Config::get("platform"), ['ios', 'android'])) {
  123. $user->login_app_platform = Config::get("platform");
  124. }
  125. $user->save();
  126. if ($inviteConf = InviteConfigModel::find($uid)) {
  127. $user->setAttribute("task_photo", $inviteConf->task_photo);
  128. $user->setAttribute("task_question", $inviteConf->task_question);
  129. $user->setAttribute("task_sing", $inviteConf->task_sing);
  130. $user->setAttribute("task_paint", $inviteConf->task_paint);
  131. $user->setAttribute("task_question_data", $inviteConf->task_question_data ? true : false);
  132. } else {
  133. $user->setAttribute("task_paint", false);
  134. $user->setAttribute("task_photo", false);
  135. $user->setAttribute("task_question", false);
  136. $user->setAttribute("task_sing", false);
  137. }
  138. // 超级VIP相关
  139. $superVip = array('supvip_endat' => 0);
  140. if ($user->supvip_endat > time()) {
  141. // 累计开通supvip天数
  142. $systag = UserSysTagModel::where('uid', $uid)->first();
  143. if (collect($systag)->isEmpty()) {
  144. $superVip['supvip_days'] = 0;
  145. } else {
  146. $superVip['supvip_days'] = $systag->supvip_days;
  147. }
  148. // 每天心动我的解锁次数
  149. $superVip['locklikeme_cnt'] = 5 - LocklikemeModel::where(array(
  150. ['uid', $user->uid],
  151. ['created_at', '>', mktime(0, 0, 0)],
  152. ['origin', 'supvip']
  153. ))->count();
  154. // 到期时间
  155. $superVip['supvip_endat'] = $user->supvip_endat;
  156. }
  157. $user->supvip = $superVip;
  158. // 标签处理
  159. $tagModel = new TagModel();
  160. $user->tags = $tagModel->getAllTagByUser($user);
  161. // 是否关注fpdx
  162. $user->bind_fpdxfwh = Openid::where([
  163. 'uid' => $uid,
  164. 'subscribe' => 1,
  165. 'public_id' => 'gh_b598cb7474d8'
  166. ])->exists() ? 1 : 0;
  167. // 卡片和解锁卡统计
  168. $banner = array(
  169. 'is_praises' => 0,
  170. 'charm' => 0
  171. );
  172. if ($user->partner_id > 0) {
  173. $partner = PartnerModel::findOrFail($user->partner_id, ['praises', 'charm']);
  174. $banner['is_praises'] = $partner->praises;
  175. $banner['charm'] = $partner->charm;
  176. }
  177. $user->lock_card = 0;
  178. $banner['count'] = PartnerModel::where([['uid', $uid], ['is_hide', 0]])->count();
  179. // 黑名单
  180. $blacklistModel = new BlackListModel();
  181. $black = $blacklistModel->where([['uid', $uid], ['end_at', '>', time()]])
  182. ->whereIn('type', ['msy', 'all'])->first();
  183. if (collect($black)->isEmpty()) {
  184. $banner['black'] = 0;
  185. } else {
  186. $banner['black'] = $black->end_at;
  187. }
  188. $user->banner = $banner;
  189. // 黑名单
  190. $user->black = $user->black();
  191. // App签到
  192. $user->app_sign_at = AppSignModel::where('uid', $uid)->where('date', date('Y-m-d'))->first()
  193. ? mktime(0, 0, 0) + 86400
  194. : mktime(0, 0, 0);
  195. MockThumbJob::dispatch($uid)->delay(Carbon::now()->addHours(1));
  196. break;
  197. }
  198. $user->course = Redis::getbit("app:course", $uid);
  199. return $user;
  200. }
  201. /**
  202. * 获取心动我的列表(分页)
  203. * @param int $uid
  204. * @param int $page
  205. * @param int $limit
  206. * @return array
  207. */
  208. public function getLikeMeList(int $uid, $page, $limit = 20): array
  209. {
  210. $user = UserModel::findOrFail($uid);
  211. $datas = collect(array());
  212. $total = 0;
  213. if ($user->partner_id > 0) {
  214. $total = DB::table('kdgx_partner_charge_praise as praise')
  215. ->join('kdgx_partner_charge_partner as partner', 'praise.uid', '=', 'partner.uid')
  216. ->where(array(
  217. ['praise.partner_id', $user->partner_id],
  218. ['partner.is_sell', 1],
  219. ['praise.type', 1],
  220. ))->count();
  221. $datas = DB::table('kdgx_partner_charge_praise as praise')
  222. ->join('kdgx_partner_charge_partner as partner', 'praise.uid', '=', 'partner.uid')
  223. ->selectRaw("praise.lock, praise.read, praise.id as praise_id, partner.id, partner.uid, partner.photo_src, partner.check_photo,
  224. partner.voice, partner.voice_check, partner.photo_1, partner.photo_1_check, partner.photo_2, partner.photo_2_check, partner.photo_3, partner.photo_3_check, partner.photo_4, partner.photo_4_check,
  225. partner.black_at,partner.is_sell,partner.praises, praise.updated_at")
  226. ->where(array(
  227. ['praise.partner_id', $user->partner_id],
  228. ['partner.is_sell', 1],
  229. ['praise.type', 1],
  230. ))->skip(($page - 1) * $limit)
  231. ->take($limit)
  232. ->orderBy('praise.updated_at', 'desc')
  233. ->get();
  234. }
  235. $lookmeuid = array();
  236. if (!$datas->isEmpty() && $page * $limit >= $total) {
  237. if (PraiseModel::where([['uid', $uid], ['partner_id', 1], ['type', 1]])->exists()) {
  238. $partner = PartnerModel::findOrFail(
  239. 1,
  240. ['id', 'uid', 'photo_src', 'voice', 'photo_1', 'photo_2', 'photo_3', 'photo_4']
  241. );
  242. $datas->push($partner);
  243. }
  244. if (PraiseModel::where([['uid', $uid], ['partner_id', 2], ['type', 1]])->exists()) {
  245. $partner = PartnerModel::findOrFail(
  246. 2,
  247. ['id', 'uid', 'photo_src', 'voice', 'photo_1', 'photo_2', 'photo_3', 'photo_4']
  248. );
  249. $datas->push($partner);
  250. }
  251. }
  252. foreach ($datas as &$data) {
  253. $data->photo_src = empty($data->photo_src) ? null : "https://oss.pocketuniversity.cn{$data->photo_src}";
  254. $data->photo_1 = empty($data->photo_1) ? null : "https://oss.pocketuniversity.cn{$data->photo_1}";
  255. $data->photo_2 = empty($data->photo_2) ? null : "https://oss.pocketuniversity.cn{$data->photo_2}";
  256. $data->photo_3 = empty($data->photo_3) ? null : "https://oss.pocketuniversity.cn{$data->photo_3}";
  257. $data->photo_4 = empty($data->photo_4) ? null : "https://oss.pocketuniversity.cn{$data->photo_4}";
  258. if (1 != $data->check_photo) {
  259. unset($data->photo_src);
  260. }
  261. if (1 != $data->photo_1_check) {
  262. unset($data->photo_1);
  263. }
  264. if (1 != $data->photo_2_check) {
  265. unset($data->photo_2);
  266. }
  267. if (1 != $data->photo_3_check) {
  268. unset($data->photo_3);
  269. }
  270. if (1 != $data->photo_4_check) {
  271. unset($data->photo_4);
  272. }
  273. if (1 != $data->voice_check) {
  274. unset($data->voice);
  275. }
  276. unset($data->check_photo);
  277. unset($data->photo_1_check);
  278. unset($data->photo_2_check);
  279. unset($data->photo_3_check);
  280. unset($data->photo_4_check);
  281. unset($data->voice_check);
  282. $data->user = UserModel::find($data->uid, [
  283. 'uid',
  284. 'nickname',
  285. 'headimgurl',
  286. 'sex',
  287. 'be_vip_at',
  288. 'supvip_endat',
  289. 'identity_auth',
  290. 'wx_auth',
  291. 'login_at'
  292. ]);
  293. if (FriendsModel::where([['uid', $data->uid], ['friend_uid', $uid]])->exists()) {
  294. $data->lock = 100;
  295. }
  296. $data->remind = FeedType4RemindModel::where([
  297. 'uid' => $uid,
  298. 'is_remind_uid' => $data->uid,
  299. 'type' => 3
  300. ])->exists() ? true : false;
  301. $data->superlike = SuperLikeModel::where([
  302. ['uid', $data->uid],
  303. ['partner_id', $user->partner_id]
  304. ])->exists() ? true : false;
  305. array_push($lookmeuid, $data->uid);
  306. }
  307. $unreads = DB::table('kdgx_partner_charge_praise as praise')
  308. ->join('kdgx_partner_charge_partner as partner', 'praise.uid', '=', 'partner.uid')
  309. ->selectRaw("partner.id")
  310. ->where(array(['praise.partner_id', $user->partner_id], ['praise.type', 1], ['read', 0]))
  311. ->get()
  312. ->pluck('id');
  313. PraiseModel::where('partner_id', $user->partner_id)->update(['read' => 1]);
  314. return array(
  315. 'pages' => array(
  316. 'page' => $page,
  317. 'limit' => $limit,
  318. 'total' => $total + 1
  319. ),
  320. 'tag_count' => count($unreads),
  321. 'list' => $datas,
  322. 'unread_list' => $unreads
  323. );
  324. }
  325. /**
  326. * 获取我喜欢的列表
  327. * @param int $uid
  328. * @param int $page
  329. * @param int $limit
  330. * @return array
  331. */
  332. public function getMyLikeList(int $uid, $page, $limit = 20): array
  333. {
  334. $total = DB::table('kdgx_partner_charge_praise as praise')
  335. ->join('kdgx_partner_charge_partner as partner', 'praise.partner_id', '=', 'partner.id')
  336. ->where([['praise.uid', $uid], ['type', 1], ['is_sell', 1]])->count();
  337. $datas = DB::table('kdgx_partner_charge_praise as praise')
  338. ->join('kdgx_partner_charge_partner as partner', 'praise.partner_id', '=', 'partner.id')
  339. ->select(DB::raw("partner.id, praise.type, praise.id as praise_id, partner.uid, partner.school, partner.sex,
  340. partner.age, partner.star, partner.photo_src, partner.check_photo, partner.voice, partner.voice_check,
  341. partner.photo_1, partner.photo_1_check, partner.photo_2, partner.photo_2_check, partner.photo_3, partner.photo_3_check,
  342. partner.photo_4, partner.photo_4_check, partner.is_sell, partner.black_at, partner.praises, praise.updated_at"))->where([
  343. ['praise.uid', $uid],
  344. ['type', 1],
  345. ['is_sell', 1],
  346. ['is_hide', 0]
  347. ])
  348. ->skip(($page - 1) * $limit)
  349. ->take($limit)
  350. ->orderBy('praise.updated_at', 'desc')
  351. ->get();
  352. foreach ($datas as &$data) {
  353. $data->photo_src = "https://oss.pocketuniversity.cn{$data->photo_src}";
  354. $data->photo_1 = "https://oss.pocketuniversity.cn{$data->photo_1}";
  355. $data->photo_4 = "https://oss.pocketuniversity.cn{$data->photo_4}";
  356. $data->photo_2 = "https://oss.pocketuniversity.cn{$data->photo_2}";
  357. $data->photo_3 = "https://oss.pocketuniversity.cn{$data->photo_3}";
  358. if (1 != $data->check_photo) {
  359. unset($data->photo_src);
  360. }
  361. if (1 != $data->photo_1_check) {
  362. unset($data->photo_1);
  363. }
  364. if (1 != $data->photo_2_check) {
  365. unset($data->photo_2);
  366. }
  367. if (1 != $data->photo_3_check) {
  368. unset($data->photo_3);
  369. }
  370. if (1 != $data->photo_4_check) {
  371. unset($data->photo_4);
  372. }
  373. if (1 != $data->voice_check) {
  374. unset($data->voice);
  375. }
  376. unset($data->check_photo);
  377. unset($data->photo_1_check);
  378. unset($data->photo_2_check);
  379. unset($data->photo_3_check);
  380. unset($data->photo_4_check);
  381. unset($data->voice_check);
  382. try {
  383. $data->user = UserModel::findOrFail($data->uid, [
  384. 'uid',
  385. 'headimgurl',
  386. 'nickname',
  387. 'login_at',
  388. 'age',
  389. 'star',
  390. 'introduce',
  391. 'expect',
  392. 'height',
  393. 'sex',
  394. 'school',
  395. 'address',
  396. 'home',
  397. 'tag_1',
  398. 'tag_2',
  399. 'tag_3',
  400. 'tag_4',
  401. 'be_vip_at',
  402. 'supvip_endat',
  403. 'identity_auth',
  404. 'wx_auth'
  405. ]);
  406. $data->user->invite_cnt = InvitationCardModel::where('invite_uid', $data->uid)->count();
  407. } catch (\Exception $e) {
  408. $data->user = null;
  409. }
  410. $data->is_self = ($uid == $data->uid) ? 1 : 0;
  411. $data->remind = FeedType4RemindModel::where([
  412. 'uid' => $uid,
  413. 'is_remind_uid' => $data->uid,
  414. 'type' => 2
  415. ])->exists() ? true : false;
  416. }
  417. return array(
  418. 'pages' => array(
  419. 'page' => $page,
  420. 'limit' => $limit,
  421. 'total' => $total
  422. ),
  423. 'list' => $datas
  424. );
  425. }
  426. /**
  427. * 获取访客列表
  428. * @param int $uid
  429. * @param $page
  430. * @param int $limit
  431. * @return array
  432. */
  433. public function getVisitorList(int $uid, $page, $limit = 20): array
  434. {
  435. $user = UserModel::findOrFail($uid);
  436. $uids = PraiseModel::where('partner_id', $user->partner_id)->get()->pluck('uid')->toArray();
  437. $total = FeedLogModel::whereHas('uidPartner', function (/** @var Builder $query */ $query) {
  438. $query->where('is_sell', 1);
  439. })
  440. ->whereNotIn('uid', $uids)
  441. ->where('partner_id', $user->partner_id)
  442. ->distinct()
  443. ->count();
  444. $innerDB = FeedLogModel::whereHas('uidPartner', function (/** @var Builder $query */ $query) {
  445. $query->where('is_sell', 1);
  446. })
  447. ->whereNotIn('uid', $uids)
  448. ->where('partner_id', $user->partner_id)
  449. ->orderBy('id');
  450. $innerSql = $innerDB->toSql();
  451. $innerBind = $innerDB->getBindings();
  452. $offset = ($page - 1) * $limit;
  453. $sql = "SELECT tab.uid, tab.created_at, count(*) as count FROM($innerSql) as tab group by `uid` ORDER BY `created_at` DESC limit {$limit} offset $offset";
  454. $feeds = Collection::make(DB::select($sql, $innerBind));
  455. $users = UserModel::whereIn('uid', $feeds->pluck('uid'))
  456. ->get(['uid', 'nickname', 'headimgurl', 'sex'])
  457. ->toArray();
  458. $users = array_combine(array_column($users, 'uid'), $users);
  459. $feeds->map(function ($value) use ($users) {
  460. $value->users = $users[$value->uid];
  461. });
  462. return [
  463. 'total' => $total,
  464. 'page' => $page,
  465. 'limit' => $limit,
  466. 'list' => $feeds
  467. ];
  468. }
  469. /**
  470. * 通过IMAccount获取uid
  471. * @param string $IMAccount im账号
  472. * @return int 用户uid;0=未绑定的im账号
  473. */
  474. public function getUidByIMAccount(string $IMAccount): int
  475. {
  476. $user = UserModel::where('im_account', $IMAccount)->first();
  477. if ($user) {
  478. return $user->uid;
  479. } else {
  480. return 0;
  481. }
  482. }
  483. }