Skip to content

Commit

Permalink
fix(common): 🐛 修复登出账号时没有清空系统托盘图标上的未读数(MacOS)
Browse files Browse the repository at this point in the history
  • Loading branch information
nongyehong committed Feb 18, 2025
1 parent 5098733 commit 383a776
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/hooks/useLogin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import { EventEnum } from '@/enums'
import { useWindow } from '@/hooks/useWindow.ts'
import { useGlobalStore } from '@/stores/global.ts'
import { type } from '@tauri-apps/plugin-os'
import { useChatStore } from '@/stores/chat.ts'
import { invoke } from '@tauri-apps/api/core'

const isMobile = computed(() => type() === 'android' || type() === 'ios')
export const useLogin = () => {
const { resizeWindow } = useWindow()
const globalStore = useGlobalStore()
const { isTrayMenuShow } = storeToRefs(globalStore)
const chatStore = useChatStore()
/**
* 设置登录状态(系统托盘图标,系统托盘菜单选项)
*/
Expand Down Expand Up @@ -36,6 +39,10 @@ export const useLogin = () => {
await resizeWindow('tray', 130, 44)
// 发送登出事件
await emit(EventEnum.LOGOUT)
// 清除未读数
chatStore.clearUnreadCount()
// 清除系统托盘图标上的未读数
await invoke('set_badge_count', { count: null })
} catch (error) {
console.error('创建登录窗口失败:', error)
}
Expand Down
5 changes: 5 additions & 0 deletions src/layout/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { emitTo } from '@tauri-apps/api/event'
import { useThrottleFn } from '@vueuse/core'
import apis from '@/services/apis.ts'
import { confirm } from '@tauri-apps/plugin-dialog'
import { invoke } from '@tauri-apps/api/core'
// 异步加载组件时增加缓存配置
const AsyncLeft = defineAsyncComponent({
Expand Down Expand Up @@ -138,6 +139,10 @@ useMitt.on(WsResponseMessageType.TOKEN_EXPIRED, async (wsTokenExpire: WsTokenExp
// token已在后端清空,只需要返回登录页
await logout()
await apis.logout()
// 清除未读数
chatStore.clearUnreadCount()
// 清除系统托盘图标上的未读数
await invoke('set_badge_count', { count: null })
userStore.isSign = false
userStore.userInfo = {}
localStorage.removeItem('user')
Expand Down
6 changes: 6 additions & 0 deletions src/layout/left/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import apis from '@/services/apis.ts'
import { LoginStatus, useWsLoginStore } from '@/stores/ws.ts'
import { useUserStore } from '@/stores/user.ts'
import { invoke } from '@tauri-apps/api/core'
import { useChatStore } from '@/stores/chat'

const { createWebviewWindow } = useWindow()
const { logout } = useLogin()
const loginStore = useWsLoginStore()
const userStore = useUserStore()
const chatStore = useChatStore()
/**
* 这里的顶部的操作栏使用pinia写入了localstorage中
*/
Expand Down Expand Up @@ -111,6 +113,10 @@ const moreList = ref<OPT.L.MoreList[]>([
userStore.isSign = false
userStore.userInfo = {}
loginStore.loginStatus = LoginStatus.Init
// 清除未读数
chatStore.clearUnreadCount()
// 清除系统托盘图标上的未读数
await invoke('set_badge_count', { count: null })
// 5. 最后调用登出方法(这会创建登录窗口)
await logout()
} catch (error) {
Expand Down
13 changes: 12 additions & 1 deletion src/stores/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,16 @@ export const useChatStore = defineStore(
invoke('set_badge_count', { count: totalUnread > 0 ? totalUnread : null })
}

// 在 useChatStore 中添加新方法
const clearUnreadCount = () => {
// 清空所有会话的未读数
sessionList.forEach((session) => {
session.unreadCount = 0
})
// 更新全局未读数
updateTotalUnreadCount()
}

return {
getMsgIndex,
chatMessageList,
Expand Down Expand Up @@ -635,7 +645,8 @@ export const useChatStore = defineStore(
getRecalledMessage,
recalledMessages,
clearAllExpirationTimers,
updateTotalUnreadCount
updateTotalUnreadCount,
clearUnreadCount
}
},
{
Expand Down

0 comments on commit 383a776

Please sign in to comment.