feat: add user notification center
This commit is contained in:
@@ -18,6 +18,7 @@ use app\common\service\ai\AiService;
|
||||
use app\common\service\ai\KbSyncService;
|
||||
use app\common\service\chat\PrivateChatService;
|
||||
use app\common\service\FileService;
|
||||
use app\common\service\notice\UserNoticeService;
|
||||
use app\common\service\VipService;
|
||||
use think\facade\Db;
|
||||
|
||||
@@ -416,6 +417,19 @@ class CommunityController extends BaseApiController
|
||||
// 更新帖子评论数
|
||||
CommunityPost::where('id', $postId)->inc('comment_count')->update();
|
||||
|
||||
if ($parentId > 0) {
|
||||
$parentComment = CommunityComment::where('id', $parentId)
|
||||
->where('post_id', $postId)
|
||||
->where('status', 1)
|
||||
->findOrEmpty();
|
||||
$recipientUserId = $replyUserId > 0 ? $replyUserId : (int) ($parentComment->user_id ?? 0);
|
||||
if (!$parentComment->isEmpty()) {
|
||||
UserNoticeService::commentReply($recipientUserId, $this->userId, $postId, (int) $comment->id, $content);
|
||||
}
|
||||
} else {
|
||||
UserNoticeService::postComment((int) $post->user_id, $this->userId, $postId, (int) $comment->id, $content);
|
||||
}
|
||||
|
||||
return $this->data(['id' => $comment->id]);
|
||||
}
|
||||
|
||||
@@ -495,6 +509,11 @@ class CommunityController extends BaseApiController
|
||||
return $this->fail('不能关注自己');
|
||||
}
|
||||
|
||||
$followUser = User::where('id', $followUserId)->findOrEmpty();
|
||||
if ($followUser->isEmpty()) {
|
||||
return $this->fail('用户不存在');
|
||||
}
|
||||
|
||||
$exists = CommunityFollow::where([
|
||||
'user_id' => $this->userId,
|
||||
'follow_user_id' => $followUserId
|
||||
@@ -506,6 +525,7 @@ class CommunityController extends BaseApiController
|
||||
'follow_user_id' => $followUserId,
|
||||
'create_time' => time(),
|
||||
]);
|
||||
UserNoticeService::followed($followUserId, $this->userId);
|
||||
return $this->data(['is_followed' => true]);
|
||||
} else {
|
||||
$exists->delete();
|
||||
@@ -1079,6 +1099,7 @@ class CommunityController extends BaseApiController
|
||||
|
||||
$userPoints = User::where('id', $userId)->value('user_points') ?: 0;
|
||||
$chatUnreadCount = PrivateChatService::unreadCount($userId);
|
||||
$noticeUnreadCount = UserNoticeService::unreadCount($userId);
|
||||
|
||||
return $this->data([
|
||||
'post_count' => $postCount,
|
||||
@@ -1087,6 +1108,8 @@ class CommunityController extends BaseApiController
|
||||
'collect_count' => $collectCount,
|
||||
'user_points' => $userPoints,
|
||||
'chat_unread_count' => $chatUnreadCount,
|
||||
'notice_unread_count' => $noticeUnreadCount,
|
||||
'message_unread_count' => $chatUnreadCount + $noticeUnreadCount,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\enum\notice\UserNoticeEnum;
|
||||
use app\common\enum\YesNoEnum;
|
||||
use app\common\model\notice\NoticeRecord;
|
||||
use app\common\model\user\User;
|
||||
use app\common\service\chat\PrivateChatService;
|
||||
use app\common\service\notice\UserNoticeService;
|
||||
|
||||
/**
|
||||
* 用户站内消息中心。
|
||||
*/
|
||||
class NoticeController extends BaseApiController
|
||||
{
|
||||
public function lists()
|
||||
{
|
||||
$page = max(1, $this->request->get('page_no/d', 1));
|
||||
$size = min(50, max(1, $this->request->get('page_size/d', 20)));
|
||||
$query = NoticeRecord::where('user_id', $this->userId)
|
||||
->whereIn('scene_id', UserNoticeEnum::getSceneIds());
|
||||
|
||||
$total = (clone $query)->count();
|
||||
$records = $query->order('id desc')
|
||||
->page($page, $size)
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
return $this->data([
|
||||
'lists' => $this->formatRecords($records),
|
||||
'count' => $total,
|
||||
'page_no' => $page,
|
||||
'page_size' => $size,
|
||||
]);
|
||||
}
|
||||
|
||||
public function summary()
|
||||
{
|
||||
$noticeUnreadCount = UserNoticeService::unreadCount($this->userId);
|
||||
$chatUnreadCount = PrivateChatService::unreadCount($this->userId);
|
||||
|
||||
return $this->data([
|
||||
'notice_unread_count' => $noticeUnreadCount,
|
||||
'chat_unread_count' => $chatUnreadCount,
|
||||
'unread_count' => $noticeUnreadCount + $chatUnreadCount,
|
||||
]);
|
||||
}
|
||||
|
||||
public function read()
|
||||
{
|
||||
$id = $this->request->post('id/d');
|
||||
if ($id <= 0) {
|
||||
return $this->fail('消息参数错误');
|
||||
}
|
||||
|
||||
NoticeRecord::where('id', $id)
|
||||
->where('user_id', $this->userId)
|
||||
->whereIn('scene_id', UserNoticeEnum::getSceneIds())
|
||||
->update([
|
||||
'read' => YesNoEnum::YES,
|
||||
'update_time' => time(),
|
||||
]);
|
||||
|
||||
return $this->success('已读');
|
||||
}
|
||||
|
||||
public function readAll()
|
||||
{
|
||||
NoticeRecord::where('user_id', $this->userId)
|
||||
->whereIn('scene_id', UserNoticeEnum::getSceneIds())
|
||||
->where('read', YesNoEnum::NO)
|
||||
->update([
|
||||
'read' => YesNoEnum::YES,
|
||||
'update_time' => time(),
|
||||
]);
|
||||
|
||||
return $this->success('全部消息已读');
|
||||
}
|
||||
|
||||
private function formatRecords(array $records): array
|
||||
{
|
||||
$sourceUserIds = [];
|
||||
foreach ($records as $record) {
|
||||
$extra = UserNoticeService::decodeExtra($record['extra'] ?? '');
|
||||
if (!empty($extra['source_user_id'])) {
|
||||
$sourceUserIds[] = (int) $extra['source_user_id'];
|
||||
}
|
||||
}
|
||||
|
||||
$userMap = [];
|
||||
$sourceUserIds = array_values(array_unique(array_filter($sourceUserIds)));
|
||||
if (!empty($sourceUserIds)) {
|
||||
$users = User::whereIn('id', $sourceUserIds)
|
||||
->field('id,nickname,avatar')
|
||||
->select()
|
||||
->toArray();
|
||||
foreach ($users as $user) {
|
||||
$userMap[(int) $user['id']] = $user;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($records as &$record) {
|
||||
$extra = UserNoticeService::decodeExtra($record['extra'] ?? '');
|
||||
$meta = UserNoticeEnum::getMeta((int) $record['scene_id']);
|
||||
$sourceUserId = (int) ($extra['source_user_id'] ?? 0);
|
||||
$record = [
|
||||
'id' => (int) $record['id'],
|
||||
'title' => $record['title'],
|
||||
'content' => $record['content'],
|
||||
'is_read' => (int) $record['read'] === YesNoEnum::YES,
|
||||
'create_time' => $record['create_time'],
|
||||
'target_url' => UserNoticeService::sanitizeTargetUrl((string) ($extra['target_url'] ?? '')),
|
||||
'source_user' => $sourceUserId > 0 ? ($userMap[$sourceUserId] ?? null) : null,
|
||||
...$meta,
|
||||
];
|
||||
}
|
||||
unset($record);
|
||||
|
||||
return $records;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user