增强SidebarMenu和MessageList组件,新增对currentContact变化的监听,自动切换聊天tab,优化联系人点击处理逻辑,调整置顶标识为数字类型,提升代码可读性和用户体验。

This commit is contained in:
2025-10-24 11:28:01 +08:00
parent 3b82908e8a
commit 1b02666531
6 changed files with 198 additions and 21 deletions

View File

@@ -538,7 +538,7 @@ export class MessageManager {
userId: number,
sessionId: number,
type: "friend" | "group",
isPinned: boolean,
isPinned: number,
): Promise<void> {
try {
const serverId = `${type}_${sessionId}`;
@@ -566,6 +566,43 @@ export class MessageManager {
}
}
/**
* 更新会话时间(用于联系人点击时更新)
* @param userId 用户ID
* @param sessionId 会话ID
* @param type 会话类型
* @param newTime 新的时间
*/
static async updateSessionTime(
userId: number,
sessionId: number,
type: "friend" | "group",
newTime: string,
): Promise<void> {
try {
const serverId = `${type}_${sessionId}`;
const session = (await chatSessionService.findByPrimaryKey(
serverId,
)) as ChatSession;
if (session) {
const updatedSession = {
...session,
lastUpdateTime: newTime,
};
// 重新生成 sortKey因为时间变了排序会改变
updatedSession.sortKey = this.generateSortKey(updatedSession);
await chatSessionService.update(serverId, updatedSession);
console.log(`会话时间已更新: ${serverId} -> ${newTime}`);
}
} catch (error) {
console.error("更新会话时间失败:", error);
throw error;
}
}
/**
* 更新会话备注
* @param userId 用户ID
@@ -690,4 +727,49 @@ export class MessageManager {
console.error("清空用户会话失败:", error);
}
}
/**
* 根据联系人ID获取会话
* @param userId 用户ID
* @param contactId 联系人ID
* @param type 类型friend/group
*/
static async getSessionByContactId(
userId: number,
contactId: number,
type: "friend" | "group",
): Promise<ChatSession | null> {
try {
const serverId = `${type}_${contactId}`;
const session = await chatSessionService.findByPrimaryKey(serverId);
return session as ChatSession | null;
} catch (error) {
console.error("根据联系人ID获取会话失败:", error);
return null;
}
}
/**
* 创建新会话
* @param userId 用户ID
* @param session 会话数据
*/
static async createSession(
userId: number,
session: ChatSession,
): Promise<void> {
try {
// 生成 sortKey
const sessionWithSortKey = {
...session,
sortKey: this.generateSortKey(session),
};
await chatSessionService.create(sessionWithSortKey);
console.log(`创建新会话: ${session.nickname || session.wechatId}`);
} catch (error) {
console.error("创建会话失败:", error);
throw error;
}
}
}