HomeService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. <?php
  2. namespace App\Services\User;
  3. use App\Exceptions\AlertException;
  4. use App\Models\Common\FormidModel;
  5. use App\Models\Deed\FriendsModel;
  6. use App\Models\Deed\InvitationCardModel;
  7. use App\Models\Log\ClickUserHomeLogModel;
  8. use App\Models\Log\FeedLogModel;
  9. use App\Models\PartnerModel;
  10. use App\Models\PraiseModel;
  11. use App\Models\User\AuthKey;
  12. use App\Models\User\FeedType4RemindModel;
  13. use App\Models\User\Openid;
  14. use App\Models\User\SuperLikeModel;
  15. use App\Models\User\UserModel;
  16. use App\Models\User\UserStateModel;
  17. use App\Models\User\UserSysTagModel;
  18. use App\Services\Service;
  19. use Illuminate\Database\Eloquent\Builder;
  20. use Illuminate\Database\Eloquent\Collection;
  21. use Illuminate\Support\Facades\DB;
  22. use Illuminate\Support\Facades\Redis;
  23. /**
  24. * 用户主页
  25. */
  26. class HomeService extends Service
  27. {
  28. /**
  29. * 喜欢我的列表
  30. * @param int $uid
  31. * @param array $pages
  32. * @return array
  33. * @deprecated
  34. */
  35. public function likeMeList(int $uid, array $pages): array
  36. {
  37. $user = UserModel::findOrFail($uid);
  38. $datas = collect(array());
  39. $total = 0;
  40. if ($user->partner_id > 0) {
  41. $total = DB::table('kdgx_partner_charge_praise as praise')
  42. ->join('kdgx_partner_charge_partner as partner', 'praise.uid', '=', 'partner.uid')
  43. ->where(array(['praise.partner_id', $user->partner_id], ['praise.type', 1]))
  44. ->count();
  45. $datas = DB::table('kdgx_partner_charge_praise as praise')
  46. ->join('kdgx_partner_charge_partner as partner', 'praise.uid', '=', 'partner.uid')
  47. ->selectRaw("praise.lock, praise.read, praise.id as praise_id, partner.id, partner.uid, partner.photo_src, partner.check_photo,
  48. 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,
  49. partner.black_at,partner.is_sell,partner.praises, praise.updated_at")
  50. ->where(array(['praise.partner_id', $user->partner_id], ['partner.is_sell', 1], ['praise.type', 1]))
  51. ->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])
  52. ->orderBy('praise.updated_at', 'desc')->get();
  53. }
  54. $lookmeuid = array();
  55. if (!$datas->isEmpty() && $pages['page'] * $pages['limit'] >= $total) {
  56. if (PraiseModel::where([['uid', $uid], ['partner_id', 1], ['type', 1]])->exists()) {
  57. $partner = PartnerModel::findOrFail(
  58. 1,
  59. ['id', 'uid', 'photo_src', 'voice', 'photo_1', 'photo_2', 'photo_3', 'photo_4']
  60. );
  61. $datas->push($partner);
  62. }
  63. if (PraiseModel::where([['uid', $uid], ['partner_id', 2], ['type', 1]])->exists()) {
  64. $partner = PartnerModel::findOrFail(
  65. 2,
  66. ['id', 'uid', 'photo_src', 'voice', 'photo_1', 'photo_2', 'photo_3', 'photo_4']
  67. );
  68. $datas->push($partner);
  69. }
  70. }
  71. foreach ($datas as &$data) {
  72. $data->photo_src = empty($data->photo_src) ? null : "https://oss.pocketuniversity.cn{$data->photo_src}";
  73. $data->photo_1 = empty($data->photo_1) ? null : "https://oss.pocketuniversity.cn{$data->photo_1}";
  74. $data->photo_2 = empty($data->photo_2) ? null : "https://oss.pocketuniversity.cn{$data->photo_2}";
  75. $data->photo_3 = empty($data->photo_3) ? null : "https://oss.pocketuniversity.cn{$data->photo_3}";
  76. $data->photo_4 = empty($data->photo_4) ? null : "https://oss.pocketuniversity.cn{$data->photo_4}";
  77. if (1 != $data->check_photo) {
  78. unset($data->photo_src);
  79. }
  80. if (1 != $data->photo_1_check) {
  81. unset($data->photo_1);
  82. }
  83. if (1 != $data->photo_2_check) {
  84. unset($data->photo_2);
  85. }
  86. if (1 != $data->photo_3_check) {
  87. unset($data->photo_3);
  88. }
  89. if (1 != $data->photo_4_check) {
  90. unset($data->photo_4);
  91. }
  92. if (1 != $data->voice_check) {
  93. unset($data->voice);
  94. }
  95. unset($data->check_photo);
  96. unset($data->photo_1_check);
  97. unset($data->photo_2_check);
  98. unset($data->photo_3_check);
  99. unset($data->photo_4_check);
  100. unset($data->voice_check);
  101. $data->user = UserModel::find($data->uid, [
  102. 'uid',
  103. 'nickname',
  104. 'headimgurl',
  105. 'sex',
  106. 'be_vip_at',
  107. 'supvip_endat',
  108. 'identity_auth',
  109. 'wx_auth',
  110. 'login_at',
  111. ]);
  112. if (FriendsModel::where([['uid', $data->uid], ['friend_uid', $uid]])->exists()) {
  113. $data->lock = 100;
  114. }
  115. $data->remind = FeedType4RemindModel::where([
  116. 'uid' => $uid,
  117. 'is_remind_uid' => $data->uid,
  118. 'type' => 3,
  119. ])->exists() ? true : false;
  120. $data->superlike = SuperLikeModel::where([
  121. ['uid', $data->uid],
  122. ['partner_id', $user->partner_id],
  123. ])->exists() ? true : false;
  124. array_push($lookmeuid, $data->uid);
  125. }
  126. $unreads = DB::table('kdgx_partner_charge_praise as praise')
  127. ->join('kdgx_partner_charge_partner as partner', 'praise.uid', '=', 'partner.uid')
  128. ->selectRaw("partner.id")
  129. ->where(array(['praise.partner_id', $user->partner_id], ['praise.type', 1], ['read', 0]))
  130. ->get()
  131. ->pluck('id');
  132. PraiseModel::where('partner_id', $user->partner_id)->update(['read' => 1]);
  133. return array(
  134. 'pages' => array(
  135. 'page' => $pages['page'],
  136. 'limit' => $pages['limit'],
  137. 'total' => $total + 1,
  138. ),
  139. 'tag_count' => count($unreads),
  140. 'list' => $datas,
  141. 'unread_list' => $unreads,
  142. );
  143. }
  144. /**
  145. * 我喜欢的列表
  146. * @param int $uid
  147. * @param array $pages
  148. * @return array
  149. * @deprecated
  150. */
  151. public function likeList(int $uid, array $pages): array
  152. {
  153. $total = DB::table('kdgx_partner_charge_praise as praise')
  154. ->join('kdgx_partner_charge_partner as partner', 'praise.partner_id', '=', 'partner.id')
  155. ->where([['praise.uid', $uid], ['type', 1], ['is_sell', 1]])->count();
  156. $datas = DB::table('kdgx_partner_charge_praise as praise')
  157. ->join('kdgx_partner_charge_partner as partner', 'praise.partner_id', '=', 'partner.id')
  158. ->select(DB::raw("partner.id, praise.type, praise.id as praise_id, partner.uid, partner.school, partner.sex,
  159. partner.age, partner.star, partner.photo_src, partner.check_photo, partner.voice, partner.voice_check,
  160. partner.photo_1, partner.photo_1_check, partner.photo_2, partner.photo_2_check, partner.photo_3, partner.photo_3_check,
  161. partner.photo_4, partner.photo_4_check, partner.is_sell, partner.black_at, partner.praises, praise.updated_at"))->where([
  162. ['praise.uid', $uid],
  163. ['type', 1],
  164. ['is_sell', 1],
  165. ['is_hide', 0],
  166. ])
  167. ->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->orderBy(
  168. 'praise.updated_at',
  169. 'desc'
  170. )->get();
  171. foreach ($datas as &$data) {
  172. $data->photo_src = "https://oss.pocketuniversity.cn{$data->photo_src}";
  173. $data->photo_1 = "https://oss.pocketuniversity.cn{$data->photo_1}";
  174. $data->photo_4 = "https://oss.pocketuniversity.cn{$data->photo_4}";
  175. $data->photo_2 = "https://oss.pocketuniversity.cn{$data->photo_2}";
  176. $data->photo_3 = "https://oss.pocketuniversity.cn{$data->photo_3}";
  177. if (1 != $data->check_photo) {
  178. unset($data->photo_src);
  179. }
  180. if (1 != $data->photo_1_check) {
  181. unset($data->photo_1);
  182. }
  183. if (1 != $data->photo_2_check) {
  184. unset($data->photo_2);
  185. }
  186. if (1 != $data->photo_3_check) {
  187. unset($data->photo_3);
  188. }
  189. if (1 != $data->photo_4_check) {
  190. unset($data->photo_4);
  191. }
  192. if (1 != $data->voice_check) {
  193. unset($data->voice);
  194. }
  195. unset($data->check_photo);
  196. unset($data->photo_1_check);
  197. unset($data->photo_2_check);
  198. unset($data->photo_3_check);
  199. unset($data->photo_4_check);
  200. unset($data->voice_check);
  201. try {
  202. $data->user = UserModel::findOrFail($data->uid, [
  203. 'uid',
  204. 'headimgurl',
  205. 'nickname',
  206. 'login_at',
  207. 'age',
  208. 'star',
  209. 'introduce',
  210. 'expect',
  211. 'height',
  212. 'sex',
  213. 'school',
  214. 'address',
  215. 'home',
  216. 'tag_1',
  217. 'tag_2',
  218. 'tag_3',
  219. 'tag_4',
  220. 'be_vip_at',
  221. 'supvip_endat',
  222. 'identity_auth',
  223. 'wx_auth',
  224. ]);
  225. $data->user->invite_cnt = InvitationCardModel::where('invite_uid', $data->uid)->count();
  226. } catch (\Exception $e) {
  227. $data->user = null;
  228. }
  229. $data->is_self = ($uid == $data->uid) ? 1 : 0;
  230. $data->remind = FeedType4RemindModel::where([
  231. 'uid' => $uid,
  232. 'is_remind_uid' => $data->uid,
  233. 'type' => 2,
  234. ])->exists() ? true : false;
  235. }
  236. return array(
  237. 'pages' => array(
  238. 'page' => $pages['page'],
  239. 'limit' => $pages['limit'],
  240. 'total' => $total,
  241. ),
  242. 'list' => $datas,
  243. );
  244. }
  245. /**
  246. * 看过我的人数
  247. * @param int $uid
  248. * @return int
  249. */
  250. public function lastLookMe(int $uid)
  251. {
  252. $cnt = ClickUserHomeLogModel::where([['home_uid', $uid], ['into_at', '>', time() - 86400 * 7]])->count();
  253. $cnt = $cnt + 3;
  254. return $cnt;
  255. }
  256. /**
  257. * 获取未解锁的「心动我的」卡片数量
  258. * @param int $uid
  259. * @return mixed
  260. */
  261. public function unLookLikemeCnt(int $uid)
  262. {
  263. $user = UserModel::findOrFail($uid);
  264. $cnt = DB::table('kdgx_partner_charge_praise as praise')
  265. ->join('kdgx_partner_charge_partner as partner', 'praise.uid', '=', 'partner.uid')
  266. ->where(array(['praise.partner_id', $user->partner_id], ['praise.type', 1], ['lock', '<', 100]))
  267. ->count();
  268. $datas = DB::table('kdgx_partner_charge_praise as praise')
  269. ->join('kdgx_partner_charge_partner as partner', 'praise.uid', '=', 'partner.uid')
  270. ->selectRaw("praise.uid")
  271. ->where(array(['praise.partner_id', $user->partner_id], ['praise.type', 1], ['lock', '<', 100]))
  272. ->take(3)->orderBy('praise.updated_at', 'desc')->get();
  273. $users = UserModel::whereIn('uid', collect($datas)->pluck('uid'))->get(['uid', 'headimgurl', 'nickname']);
  274. return array(
  275. 'users' => $users,
  276. 'cnt' => $cnt,
  277. );
  278. }
  279. /**
  280. * 获取某人最近七天的热度
  281. * @param int $uid
  282. * @return array
  283. */
  284. public function getLast7dayHot(int $uid)
  285. {
  286. $user = UserModel::findOrFail($uid);
  287. $start_at = mktime(0, 0, 0);
  288. $result = array();
  289. for ($i = 0; $i < 7; $i++) {
  290. if (0 == $user->partner_id) {
  291. $inviteme = 0;
  292. $thumbme = 0;
  293. $feedme = 0;
  294. } else {
  295. $inviteme = InvitationCardModel::where('invite_uid', $uid)->whereBetween(
  296. 'created_at',
  297. [$start_at, $start_at + 86400]
  298. )->count();
  299. $thumbme = PraiseModel::where('partner_id', $user->partner_id)->whereBetween(
  300. 'created_at',
  301. [$start_at, $start_at + 86400]
  302. )->count();
  303. $feedme = FeedLogModel::where('partner_id', $user->partner_id)->whereBetween(
  304. 'created_at',
  305. [$start_at, $start_at + 86400]
  306. )->count();
  307. }
  308. $result[] = [
  309. 'date' => date('m-d', $start_at),
  310. 'inviteme' => $inviteme,
  311. 'thumbme' => $thumbme,
  312. 'feedme' => $feedme,
  313. ];
  314. $start_at -= 86400;
  315. }
  316. return $result;
  317. }
  318. public function popularityExpire()
  319. {
  320. $start = time();
  321. $ns = new \App\Services\Share\NoticeService();
  322. $flowers = UserSysTagModel::whereBetween('popularity_flower_end_at', [$start, $start + 600])->get(['uid']);
  323. foreach ($flowers as $flower) {
  324. try {
  325. if (Redis::exists("popularity:flower:expire:{$flower->uid}")) {
  326. continue;
  327. }
  328. $ns->popularityFlawerExpire($flower->uid);
  329. Redis::setex("popularity:flower:expire:{$flower->uid}", 1200, $flower->uid);
  330. } catch (\Exception $exception) {
  331. dump($exception);
  332. }
  333. }
  334. $shares = UserSysTagModel::whereBetween('popularity_share_end_at', [$start, $start + 600])->get(['uid']);
  335. foreach ($shares as $share) {
  336. try {
  337. if (Redis::exists("popularity:share:expire:{$share->uid}")) {
  338. continue;
  339. }
  340. $ns->popularitySharehelpExpire($share->uid);
  341. Redis::setex("popularity:share:expire:{$share->uid}", 1200, $share->uid);
  342. } catch (\Exception $exception) {
  343. dump($exception);
  344. }
  345. }
  346. }
  347. public function logTodayPopularity()
  348. {
  349. $partners = PartnerModel::where([['is_sell', 1], ['base_score', '>', 0]])->get();
  350. $total = $partners->count();
  351. $datas = array();
  352. foreach ($partners as $partner) {
  353. try {
  354. $user = UserModel::findOrFail($partner->uid);
  355. $pdata = $this->getPopularity($partner->uid);
  356. $tmp = array(
  357. 'created_at' => time(),
  358. 'uid' => $partner->uid,
  359. 'sex' => $user->sex,
  360. 'date' => date('Y-m-d'),
  361. 'popularity' => $pdata['base'],
  362. 'location' => $user->location,
  363. );
  364. $datas[] = $tmp;
  365. } catch (\Exception $exception) {
  366. }
  367. }
  368. usort($datas, function ($v1, $v2) {
  369. return ($v1['popularity'] < $v2['popularity']) ? -1 : 1;
  370. });
  371. $i = 1;
  372. $popularity = 0;
  373. $add = array();
  374. foreach ($datas as &$data) {
  375. $i++;
  376. $add[] = $data;
  377. if (0 == $i % 100) {
  378. DB::table("kdgx_fpdx_populary_log")->insert($add);
  379. $add = array();
  380. }
  381. }
  382. }
  383. // 增加人气值时间到期通知
  384. /**
  385. * 获取当前人气值
  386. * @param int $uid
  387. * @return array
  388. * @throws AlertException
  389. */
  390. public function getPopularity(int $uid)
  391. {
  392. $user = UserModel::findOrFail($uid);
  393. if (0 == $user->partner_id) {
  394. throw new AlertException("还未生成卡片", 201);
  395. }
  396. /** @var PartnerModel $partner */
  397. $partner = PartnerModel::findOrFail($user->partner_id);
  398. if (1 != $partner->is_sell) {
  399. PartnerModel::where('id', $user->partner_id)->update(['base_score' => -1]);
  400. throw new AlertException("{$user->partner_id}:卡片未上架", 202);
  401. }
  402. $canNotice = false;
  403. // 是否关注fpdx或可以分小程序模板消息
  404. if (Openid::isSubscribe($uid, "gh_b598cb7474d8")) {
  405. $canNotice = true;
  406. }
  407. if (
  408. FormidModel::where([
  409. ['uid', $uid],
  410. ['public_id', config('miniprogram.public_id')],
  411. ['state', 0],
  412. ['created_at', '>', time() - 86400 * 5],
  413. ])->exists()
  414. ) {
  415. $canNotice = true;
  416. }
  417. try {
  418. $auth = AuthKey::where(array(['uid', $uid], ['auth_type', config("qqprogram.app_id")]))->firstOrFail();
  419. if (
  420. \App\Models\QQ\FormIdModel::where(array(
  421. ['openid', $auth->auth_key],
  422. ['created_at', '>', time() - 6 * 86400],
  423. ['send_at', 0],
  424. ))->exists()
  425. ) {
  426. $canNotice = true;
  427. }
  428. } catch (\Exception $exception) {
  429. }
  430. if (!$canNotice) {
  431. PartnerModel::where('id', $user->partner_id)->update(['base_score' => -1]);
  432. throw new AlertException("请关注公众号", 203);
  433. }
  434. if ($partner->base_score < 0) {
  435. $partner->base_score = $this->calcBaseScore($partner);
  436. $partner->save();
  437. }
  438. // 基础人气值
  439. switch ($partner->base_score) {
  440. case $partner->base_score >= 0 && $partner->base_score < 40:
  441. $base = 0 + (100 / 40) * ($partner->base_score - 0);
  442. break;
  443. case $partner->base_score >= 40 && $partner->base_score < 50:
  444. $base = 100 + (100 / 10) * ($partner->base_score - 40);
  445. break;
  446. case $partner->base_score >= 50 && $partner->base_score < 60:
  447. $base = 200 + (100 / 10) * ($partner->base_score - 50);
  448. break;
  449. case $partner->base_score >= 60 && $partner->base_score < 70:
  450. $base = 300 + (200 / 10) * ($partner->base_score - 60);
  451. break;
  452. case $partner->base_score >= 70 && $partner->base_score < 90:
  453. $base = 500 + (300 / 10) * ($partner->base_score - 70);
  454. break;
  455. case $partner->base_score >= 90:
  456. $base = 800 + (400 / 10) * ($partner->base_score - 90);
  457. break;
  458. default:
  459. throw new AlertException("人气值生成中", 204);
  460. }
  461. $popularity = $base;
  462. // 附加分 = 好友助力+消耗小fa+超级会员
  463. $systag = UserSysTagModel::firstOrCreate(
  464. ['uid' => $user->uid],
  465. ['uid' => $user->uid, 'popularity_share_end_at' => 0]
  466. );
  467. $systag->popularity_share_end_at > time() && $popularity += 100;
  468. $popularity_sign_end_at = intval(UserStateModel::get($user->uid, "popularity_sign_end_at"));
  469. $popularity_sign_end_at > time() && $popularity += 100;
  470. $user->supvip_endat > time() && $popularity += 100;
  471. return array(
  472. 'popularity_share_end_at' => $systag->popularity_share_end_at,
  473. 'popularity_sign_end_at' => $popularity_sign_end_at,
  474. 'popularity_supvip_endat' => $user->supvip_endat,
  475. 'popularity' => $popularity,
  476. 'base' => $base,
  477. );
  478. }
  479. // 存储用户今日人气值
  480. /**
  481. * 估算基础分
  482. * @param PartnerModel $partner
  483. * @return float
  484. */
  485. private function calcBaseScore($partner)
  486. {
  487. $DT1 = -0.004; // 最近更新加分衰变率1
  488. $DT2 = -0.01; // 赞数衰变率2*z(x)
  489. $DT3 = -0.00002; // 发布时间加分衰变率3
  490. $DT4 = -0.003; // 最近上线加分衰变率4
  491. $params = Redis::hgetAll("msy-feed-params");
  492. if (empty($params)) {
  493. $params = array(
  494. 'pt1' => 0.1,
  495. 'pz' => 0.3,
  496. 'pg' => 0.3,
  497. 'pp' => 0.1,
  498. 'pd' => 0.15,
  499. 'pt3' => 0.1,
  500. );
  501. }
  502. // 最近更新时间
  503. $t1 = (time() - $partner->update_at) / 3600;
  504. // 0 == $partner->update_at && $t1 = (time() - $partner->created_at) / 3600;
  505. // 点赞数
  506. $z = $partner->praises + 1;
  507. // 曝光量
  508. $b = $partner->feed_cnt + 2;
  509. if ($b < $z) {
  510. $b = $z * 2;
  511. }
  512. // 是否有语音
  513. $p = 60;
  514. empty($partner->voice) && $p = 100;
  515. // 图片数量
  516. $d = 50;
  517. empty($partner->photo_src) && $d += 10;
  518. empty($partner->photo_1) && $d += 10;
  519. empty($partner->photo_2) && $d += 10;
  520. empty($partner->photo_3) && $d += 10;
  521. empty($partner->photo_4) && $d += 10;
  522. // 最近登录时间
  523. $t2 = (time() - $partner->login_at) / 3600;
  524. // 发布时间
  525. $t3 = (time() - $partner->upload_at) / 3600;
  526. 0 == $partner->upload_at && $t3 = (time() - $partner->created_at) / 3600;
  527. $score1 = 100 * exp($DT1 * $t1) * $params['pt1'] ?? 0.1;
  528. $score2 = ($z / $b) * 100 * $params['pz'] ?? 0.5;
  529. $score3 = 0.2 * $params['pg'] ?? 0.3;
  530. $score4 = $p * $params['pp'] ?? 0.1;
  531. $score5 = $d * $params['pd'] ?? 0.15;
  532. $score6 = 100 * exp($DT3 * $t3) * $params['pt3'] ?? 0.1;
  533. $base_score = round(($score1 + $score2 + $score3 + $score4 + $score5 + $score6) * exp($DT4 * $t2));
  534. return $base_score;
  535. }
  536. /**
  537. * 访客数据概览
  538. * @param int $uid
  539. * @return array
  540. */
  541. public function getVisitorOverView(int $uid)
  542. {
  543. $user = UserModel::findOrFail($uid);
  544. $today = $feedme = FeedLogModel::where('partner_id', $user->partner_id)->where(
  545. 'created_at',
  546. '>',
  547. mktime(0, 0, 0)
  548. )->select(DB::raw("count(DISTINCT(`uid`)) as count"))->first()->toArray()['count'];
  549. $month = $feedme = FeedLogModel::where('partner_id', $user->partner_id)->where(
  550. 'created_at',
  551. '>',
  552. mktime(0, 0, 0, date('m'), 1, date('Y'))
  553. )->count();
  554. $uids = PraiseModel::where('partner_id', $user->partner_id)->get()->pluck('uid')->toArray();
  555. $users = json_decode(Redis::get("fpdx:user:visitoroverview:feedusers:{$uid}"), true);
  556. if (!$users) {
  557. $feeds = FeedLogModel::whereNotIn('kdgx_fpdx_feed_log.uid', $uids)->join(
  558. 'kdgx_partner_charge_partner',
  559. 'kdgx_partner_charge_partner.uid',
  560. '=',
  561. 'kdgx_fpdx_feed_log.uid'
  562. )->where([
  563. ['partner_id', $user->partner_id],
  564. ['is_sell', 1],
  565. ])
  566. ->select(DB::raw("kdgx_fpdx_feed_log.uid,kdgx_fpdx_feed_log.created_at,count(*) as count"))
  567. ->orderBy('kdgx_fpdx_feed_log.created_at', 'desc')->take(4)->get();
  568. $users = UserModel::whereIn('uid', $feeds->pluck('uid')->toArray())->get(['uid', 'nickname', 'headimgurl']);
  569. Redis::setex("fpdx:user:visitoroverview:feedusers:{$uid}", 86400, json_encode($users));
  570. }
  571. try {
  572. $pdata = $this->getPopularity($uid);
  573. $popularity = $pdata['popularity'];
  574. } catch (\Exception $exception) {
  575. $popularity = 0;
  576. }
  577. return compact('today', 'month', 'users', 'popularity');
  578. }
  579. /**
  580. * 访客数据列表
  581. * @param int $uid
  582. * @param array $pages
  583. * @return array
  584. * @deprecated
  585. */
  586. public function getVisitorList(int $uid, array $pages)
  587. {
  588. $user = UserModel::findOrFail($uid);
  589. $uids = PraiseModel::where('partner_id', $user->partner_id)->get()->pluck('uid')->toArray();
  590. $total = FeedLogModel::whereHas('uidPartner', function (/** @var Builder $query */ $query) {
  591. $query->where('is_sell', 1);
  592. })
  593. ->whereNotIn('uid', $uids)
  594. ->where('partner_id', $user->partner_id)
  595. ->distinct()
  596. ->count();
  597. $innerDB = FeedLogModel::whereHas('uidPartner', function (/** @var Builder $query */ $query) {
  598. $query->where('is_sell', 1);
  599. })
  600. ->whereNotIn('uid', $uids)
  601. ->where('partner_id', $user->partner_id)
  602. ->orderBy('id');
  603. $innerSql = $innerDB->toSql();
  604. $innerBind = $innerDB->getBindings();
  605. $offset = ($pages['page'] - 1) * $pages['limit'];
  606. $sql = "SELECT tab.uid, tab.created_at, count(*) as count FROM($innerSql) as tab group by `uid` ORDER BY `created_at` DESC limit {$pages['limit']} offset $offset";
  607. $feeds = Collection::make(DB::select($sql, $innerBind));
  608. $users = UserModel::whereIn('uid', $feeds->pluck('uid')->toArray())->get([
  609. 'uid',
  610. 'nickname',
  611. 'headimgurl',
  612. 'sex',
  613. ])->toArray();
  614. $users = array_combine(array_column($users, 'uid'), $users);
  615. foreach ($feeds as &$feed) {
  616. $feed->users = $users[$feed->uid];
  617. }
  618. return [
  619. 'total' => $total,
  620. 'page' => $pages['page'],
  621. 'limit' => $pages['limit'],
  622. 'list' => $feeds,
  623. ];
  624. }
  625. }