fix(contact): isolate request unread state between PC accounts

This commit is contained in:
OrionMark
2026-08-15 17:50:57 +08:00
committed by Dawn
parent b07e9a3c35
commit 262737ef80
3 changed files with 40 additions and 3 deletions
+14
View File
@@ -28,6 +28,7 @@ import { info as logInfo } from '@tauri-apps/plugin-log'
import { ensureAppStateReady } from '@/utils/AppStateReady'
import { useI18nGlobal } from '../services/i18n'
import { useInitialSyncStore } from '@/stores/initialSync'
import { useContactStore } from '@/stores/contacts'
import { openExternalUrl } from './useLinkSegments'
import { TokenManager } from '@/utils/TokenManager'
@@ -45,6 +46,7 @@ export const useLogin = () => {
const userStore = useUserStore()
const loginHistoriesStore = useLoginHistoriesStore()
const initialSyncStore = useInitialSyncStore()
const contactStore = useContactStore()
const { createWebviewWindow } = useWindow()
const { t } = useI18nGlobal()
@@ -77,6 +79,13 @@ export const useLogin = () => {
console.log('[useLogin] Message cache has been cleared')
}
/** 清理当前账号的好友申请状态,避免窗口交接或异步响应造成红点串号 */
const clearAccountApplyState = () => {
if (!isDesktop()) return
globalStore.resetAccountApplyState()
contactStore.resetAccountState()
}
/**
* 在 composable 初始化时获取 router 实例
* 注意: useRouter() 必须在组件 setup 上下文中调用
@@ -124,6 +133,7 @@ export const useLogin = () => {
*/
const logout = async () => {
globalStore.updateCurrentSessionRoomId('')
clearAccountApplyState()
const sendLogoutEvent = async () => {
// ws 退出连接
@@ -246,6 +256,7 @@ export const useLogin = () => {
// 清空 localStorage,防止页面刷新时恢复旧账号数据
clearUserLocalStorage()
clearAccountApplyState()
// 清空消息缓存,避免旧消息混入新账号
clearMessageCache()
@@ -273,6 +284,9 @@ export const useLogin = () => {
}
userStore.userInfo = account
loginHistoriesStore.addLoginHistory(account)
if (isDesktop()) {
await contactStore.getApplyUnReadCount()
}
// 初始化表情列表并在后台预取本地缓存(使用 worker + 并发限制)
void emojiStore.initEmojis().catch(() => {
void logInfo('[login] 初始化表情失败')
+18 -3
View File
@@ -5,6 +5,7 @@ import { RequestNoticeAgreeStatus } from '@/services/types'
import { useGlobalStore } from '@/stores/global'
import { useFeedStore } from '@/stores/feed'
import { useGroupStore } from '@/stores/group'
import { useUserStore } from '@/stores/user'
import {
deleteFriend,
getFriendPage,
@@ -19,6 +20,7 @@ export const useContactStore = defineStore(StoresEnum.CONTACTS, () => {
const globalStore = useGlobalStore()
const feedStore = useFeedStore()
const groupStore = useGroupStore()
const userStore = useUserStore()
/** 联系人列表 */
const contactsList = ref<FriendItem[]>([])
@@ -69,9 +71,13 @@ export const useContactStore = defineStore(StoresEnum.CONTACTS, () => {
* 更新全局store中的未读计数
*/
const getApplyUnReadCount = async () => {
const requestUid = userStore.userInfo?.uid
if (!requestUid) return
const res: any = await getNoticeUnreadCount()
if (!res) return
// 更新全局store中的未读计数
if (!res || userStore.userInfo?.uid !== requestUid) return
// 仅允许当前账号的响应更新未读计数,避免切换账号时旧请求覆盖新状态
globalStore.unReadMark.newFriendUnreadCount = res.unReadCount4Friend
globalStore.unReadMark.newGroupUnreadCount = res.unReadCount4Group
@@ -211,6 +217,14 @@ export const useContactStore = defineStore(StoresEnum.CONTACTS, () => {
await getContactList(true)
}
/** 清理账号相关的联系人和申请状态,避免切换账号时复用旧数据 */
const resetAccountState = () => {
contactsList.value = []
requestFriendsList.value = []
contactsOptions.value = { isLast: false, isLoading: false, cursor: '' }
applyPageOptions.value = { isLast: false, cursor: '', pageNo: 1 }
}
return {
getContactList,
getApplyPage,
@@ -221,6 +235,7 @@ export const useContactStore = defineStore(StoresEnum.CONTACTS, () => {
applyPageOptions,
onDeleteFriend,
onHandleInvite,
deleteContact
deleteContact,
resetAccountState
}
})
+8
View File
@@ -164,6 +164,13 @@ export const useGlobalStore = defineStore(
currentSessionRoomId.value = id
}
/** 清理账号相关的申请未读状态,避免切换账号时共享旧账号红点 */
const resetAccountApplyState = () => {
unReadMark.newFriendUnreadCount = 0
unReadMark.newGroupUnreadCount = 0
currentSelectedContact.value = undefined
}
return {
unReadMark,
currentSession,
@@ -178,6 +185,7 @@ export const useGlobalStore = defineStore(
setTipVisible,
updateGlobalUnreadCount,
updateCurrentSessionRoomId,
resetAccountApplyState,
currentSessionRoomId
}
},