diff --git a/backend/IM_API/Controllers/FriendController.cs b/backend/IM_API/Controllers/FriendController.cs index 790cd0e..01a7f57 100644 --- a/backend/IM_API/Controllers/FriendController.cs +++ b/backend/IM_API/Controllers/FriendController.cs @@ -49,7 +49,7 @@ namespace IM_API.Controllers var userIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier); int userId = int.Parse(userIdStr); var list = await _friendService.GetFriendRequestListAsync(userId,page,limit,desc); - var res = new BaseResponse>(list); + var res = new BaseResponse>(list); return Ok(res); } /// diff --git a/backend/IM_API/Dtos/FriendRequestResDto.cs b/backend/IM_API/Dtos/FriendRequestResDto.cs new file mode 100644 index 0000000..1fbf76d --- /dev/null +++ b/backend/IM_API/Dtos/FriendRequestResDto.cs @@ -0,0 +1,36 @@ +using IM_API.Models; + +namespace IM_API.Dtos +{ + public class FriendRequestResDto + { + public int Id { get; set; } + + /// + /// 申请人 + /// + public int RequestUser { get; set; } + + /// + /// 被申请人 + /// + public int ResponseUser { get; set; } + public string Avatar { get; set; } + public string NickName { get; set; } + + /// + /// 申请时间 + /// + public DateTime Created { get; set; } + + /// + /// 申请附言 + /// + public string? Description { get; set; } + + /// + /// 申请状态(0:待通过,1:拒绝,2:同意,3:拉黑) + /// + public FriendRequestState State { get; set; } + } +} diff --git a/backend/IM_API/Interface/Services/IFriendSerivce.cs b/backend/IM_API/Interface/Services/IFriendSerivce.cs index ad22285..16aa4c5 100644 --- a/backend/IM_API/Interface/Services/IFriendSerivce.cs +++ b/backend/IM_API/Interface/Services/IFriendSerivce.cs @@ -27,7 +27,7 @@ namespace IM_API.Interface.Services /// /// /// - Task> GetFriendRequestListAsync(int userId,int page,int limit, bool desc); + Task> GetFriendRequestListAsync(int userId,int page,int limit, bool desc); /// /// 处理好友请求 /// diff --git a/backend/IM_API/Services/FriendService.cs b/backend/IM_API/Services/FriendService.cs index 4c1a31c..9e6cd63 100644 --- a/backend/IM_API/Services/FriendService.cs +++ b/backend/IM_API/Services/FriendService.cs @@ -73,15 +73,29 @@ namespace IM_API.Services } #endregion #region 获取好友请求列表 - public async Task> GetFriendRequestListAsync(int userId, int page, int limit, bool desc) + public async Task> GetFriendRequestListAsync(int userId, int page, int limit, bool desc) { var query = _context.FriendRequests + .Include(x => x.ResponseUserNavigation) + .Include(x => x.RequestUserNavigation) .Where( x => (x.ResponseUser == userId) || x.RequestUser == userId - ); + ) + .Select(s => new FriendRequestResDto + { + Id = s.Id, + RequestUser = s.RequestUser, + ResponseUser = s.ResponseUser, + Avatar = s.RequestUser == userId ? s.ResponseUserNavigation.Avatar : s.RequestUserNavigation.Avatar, + Created = s.Created, + NickName = s.RequestUser == userId ? s.ResponseUserNavigation.NickName : s.RequestUserNavigation.NickName, + Description = s.Description, + State = (FriendRequestState)s.State + }) + ; query = query.OrderByDescending(x => x.Id); - var friendRequestList = await query.Skip((page - 1 * limit)).Take(limit).ToListAsync(); + var friendRequestList = await query.Skip(((page - 1) * limit)).Take(limit).ToListAsync(); return friendRequestList; } #endregion diff --git a/frontend/web/src/handler/messageHandler.js b/frontend/web/src/handler/messageHandler.js index 3101816..484f53e 100644 --- a/frontend/web/src/handler/messageHandler.js +++ b/frontend/web/src/handler/messageHandler.js @@ -9,7 +9,6 @@ export const messageHandler = (msg) => { } else { conversation.unreadCount += 1; } - console.log(conversation) conversation.dateTime = new Date().toISOString(); } \ No newline at end of file diff --git a/frontend/web/src/services/friend.js b/frontend/web/src/services/friend.js index dcdbc2d..90b8788 100644 --- a/frontend/web/src/services/friend.js +++ b/frontend/web/src/services/friend.js @@ -28,5 +28,5 @@ export const friendService = { * @param {*} limit * @returns */ - getFriendRequests: (page = 1, limit = 100) => request.get(`/friend/request?page=${page}&limit=${limit}`) + getFriendRequests: (page = 1, limit = 100) => request.get(`/friend/requests?page=${page}&limit=${limit}`) } \ No newline at end of file diff --git a/frontend/web/src/services/useBrowserNotification.js b/frontend/web/src/services/useBrowserNotification.js new file mode 100644 index 0000000..d97a58c --- /dev/null +++ b/frontend/web/src/services/useBrowserNotification.js @@ -0,0 +1,21 @@ +export function useBrowserNotification() { + const requestPermission = async () => { + if ("Notification" in window && Notification.permission === "default") { + await Notification.requestPermission(); + } + }; + + const send = (title, options = {}) => { + if ("Notification" in window && Notification.permission === "granted") { + // 如果页面正处于激活状态,通常不需要弹窗提醒,以免干扰用户 + /* + if (document.visibilityState === 'visible' && document.hasFocus()) { + return; + } + */ + return new Notification(title, options); + } + }; + + return { requestPermission, send }; +} \ No newline at end of file diff --git a/frontend/web/src/stores/signalr.js b/frontend/web/src/stores/signalr.js index b8481f3..a5a2288 100644 --- a/frontend/web/src/stores/signalr.js +++ b/frontend/web/src/stores/signalr.js @@ -6,6 +6,8 @@ import { useChatStore } from "./chat"; import { authService } from "@/services/auth"; import { generateSessionId } from "@/utils/sessionIdTools"; import { messageHandler } from "@/handler/messageHandler"; +import { useBrowserNotification } from "@/services/useBrowserNotification"; +import { useConversationStore } from "./conversation"; export const useSignalRStore = defineStore('signalr', { state: () => ({ @@ -40,11 +42,17 @@ export const useSignalRStore = defineStore('signalr', { }, registerHandlers() { const chatStore = useChatStore() + const browserNotification = useBrowserNotification(); this.connection.on('ReceiveMessage', (msg) => { const sessionId = generateSessionId(msg.senderId, msg.receiverId); messageHandler(msg); chatStore.addMessage(msg, sessionId); + const conversation = useConversationStore().conversations.find(x => x.targetId == msg.senderId); + browserNotification.send(`${conversation.targetName}发来一条消息`, { + body: msg.content, + icon: conversation.targetAvatar + }); }); this.connection.onclose(() => { this.isConnected = false }); diff --git a/frontend/web/src/views/contact/FriendRequestList.vue b/frontend/web/src/views/contact/FriendRequestList.vue index 6904444..f67a46c 100644 --- a/frontend/web/src/views/contact/FriendRequestList.vue +++ b/frontend/web/src/views/contact/FriendRequestList.vue @@ -10,19 +10,31 @@
{{ item.nickName }} - {{ item.time }} + {{ formatDate(item.created) }}
-

{{ item.desc }}

+

{{ item.description }}

{{ item.remark }}

-