UserNoticeManager.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Managers;
  3. use App\Models\Deed\InvitationCardModel;
  4. use App\Models\NoticeModel;
  5. use App\Models\PraiseModel;
  6. use App\Models\User\UserModel;
  7. use Illuminate\Support\Facades\Config;
  8. class UserNoticeManager
  9. {
  10. /**
  11. * 获取系统通知
  12. * @param int $uid
  13. * @param array $pages
  14. * @param int $unread 不自动读
  15. * @return array
  16. */
  17. public function systemList(int $uid, array $pages, int $unread): array
  18. {
  19. if (in_array(Config::get("platform"), ['ios', 'android'])) {
  20. // APP通知
  21. if (Config::get("version") < "2.17.6") {
  22. $shield = [20, 13, 14];
  23. } else {
  24. $shield = [20];
  25. }
  26. } else {
  27. // 小程序通知
  28. $shield = [5];
  29. }
  30. $total = NoticeModel::where('uid', $uid)->whereIn('type', $shield)->count();
  31. $datas = NoticeModel::where('uid', $uid)->whereIn('type', $shield)
  32. ->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->orderBy('id', 'desc')->get();
  33. $tag_count = NoticeModel::where([['uid', $uid], ['is_read', 0]])->count();
  34. 0 == $unread && NoticeModel::where([['uid', $uid], ['is_read', 0]])->update(['is_read' => time()]);
  35. return [
  36. 'total' => $total,
  37. 'page' => $pages['page'],
  38. 'limit' => $pages['limit'],
  39. 'tag_count' => $tag_count,
  40. 'list' => $datas
  41. ];
  42. }
  43. /**
  44. * 获取最近系统通知
  45. * @param int $uid
  46. * @return NoticeModel|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object|null
  47. */
  48. public function systemListDesc(int $uid)
  49. {
  50. if (in_array(Config::get("platform"), ['ios', 'android'])) {
  51. // APP通知
  52. $shield = [20, 13, 14];
  53. } else {
  54. // 小程序通知
  55. $shield = [5, 13, 14];
  56. }
  57. $data = NoticeModel::where([
  58. ['uid', $uid],
  59. ['is_read', 0]
  60. ])->whereIn('type', $shield)->orderByDesc('id')->first(['id', 'uid', 'title', 'content', 'create_at', 'type']);
  61. if (collect($data)->isEmpty()) {
  62. return $data;
  63. }
  64. switch ($data->type) {
  65. case 13:
  66. $invite_me = NoticeModel::where([['is_read', 0], ['type', 13], ['uid', $uid]])->count();
  67. $data->content = "收到{$invite_me}条新的好友申请消息,点击查看";
  68. break;
  69. case 14:
  70. $total = NoticeModel::where([['is_read', 0], ['type', 14], ['uid', $uid]])->count();
  71. $data->content = "收到{$total}条新的心动消息,点击查看";
  72. break;
  73. }
  74. return $data;
  75. }
  76. /**
  77. * 喜欢我的通知概览
  78. * @param int $uid
  79. * @return array
  80. */
  81. public function likeMeOverView(int $uid)
  82. {
  83. try {
  84. $user = UserModel::findOrFail($uid);
  85. // 喜欢我的人数
  86. $total = PraiseModel::where([['partner_id', $user->partner_id], ['type', 1], ['read', 0]])->count();
  87. // 最近喜欢我的三个用户信息:昵称 + 头像
  88. $praise = PraiseModel::where([
  89. ['partner_id', $user->partner_id],
  90. ['type', 1],
  91. ['read', 0]
  92. ])->orderBy('updated_at', 'desc')->take(3)->get();
  93. $users = UserModel::whereIn('uid', $praise->pluck('uid'))->get(['uid', 'nickname', 'headimgurl']);
  94. return array(
  95. 'users' => $users,
  96. 'total' => $total,
  97. 'notice' => $total,
  98. 'likeme' => $total,
  99. );
  100. } catch (\Exception $exception) {
  101. return array(
  102. 'users' => null,
  103. 'likeme' => 0,
  104. 'total' => 0,
  105. 'notice' => 0
  106. );
  107. }
  108. }
  109. /**
  110. * 邀请通知概览
  111. * @param int $uid
  112. * @return array
  113. */
  114. public function inviteOverView(int $uid)
  115. {
  116. // 未读消息-我发出的邀请
  117. $send_invite = InvitationCardModel::where([['send_read', 0], ['uid', $uid]])->count();
  118. // 未读消息-收到的邀请
  119. $invite_me = InvitationCardModel::where([['read', 0], ['state', 0], ['invite_uid', $uid]])->count();
  120. // 最近我未读的邀请用户信息
  121. $invites = InvitationCardModel::where([['invite_uid', $uid], ['read', 0]])->orWhere([
  122. ['send_read', 0],
  123. ['uid', $uid]
  124. ])->take(3)->orderBy('id', 'desc')->get();
  125. $users = UserModel::whereIn('uid', $invites->pluck('uid'))->get(['uid', 'nickname', 'headimgurl']);
  126. return array(
  127. 'users' => $users,
  128. 'send_invite' => $send_invite,
  129. 'invite_me' => $invite_me,
  130. 'total' => $send_invite + $invite_me,
  131. 'notice' => $send_invite + $invite_me
  132. );
  133. }
  134. }