This commit is contained in:
Putoo
2026-04-22 12:56:10 +08:00
parent 996bdbbd1c
commit 5857e6073f
11 changed files with 277 additions and 327 deletions

View File

@@ -5,8 +5,6 @@ export interface IAppConfig {
showDebug: boolean
}
const STORAGE_KEY = 'app-config-store'
export const useAppStore = defineStore('app', {
state: () => ({
sidebarCollapsed: false,
@@ -29,17 +27,14 @@ export const useAppStore = defineStore('app', {
actions: {
toggleSidebar() {
this.sidebarCollapsed = !this.sidebarCollapsed
this.syncToLocalStorage()
},
setSidebarCollapsed(collapsed: boolean) {
this.sidebarCollapsed = collapsed
this.syncToLocalStorage()
},
setShowDebug(show: boolean) {
this.showDebug = show
this.syncToLocalStorage()
},
startLoading(text: string = '加载中...') {
@@ -83,38 +78,11 @@ export const useAppStore = defineStore('app', {
this.showDebug = false
this.isLoading = false
this.loadingText = ''
this.clearLocalStorage()
},
syncToLocalStorage() {
if (typeof localStorage !== 'undefined') {
const data = {
sidebarCollapsed: this.sidebarCollapsed,
showDebug: this.showDebug
}
localStorage.setItem(STORAGE_KEY, JSON.stringify(data))
}
},
restoreFromLocalStorage() {
if (typeof localStorage !== 'undefined') {
const stored = localStorage.getItem(STORAGE_KEY)
if (stored) {
try {
const data = JSON.parse(stored)
this.sidebarCollapsed = data.sidebarCollapsed || false
this.showDebug = data.showDebug || false
} catch (e) {
console.error('恢复应用配置失败:', e)
}
}
}
},
clearLocalStorage() {
if (typeof localStorage !== 'undefined') {
localStorage.removeItem(STORAGE_KEY)
}
}
},
persist: {
storage: piniaPluginPersistedstate.localStorage(),
pick: ['sidebarCollapsed', 'showDebug']
}
})

View File

@@ -39,14 +39,11 @@ export const useUserStore = defineStore('user', {
this.userInfo = data
this.token = token
this.isLoading = false
// 同步到localStorage
this.syncToLocalStorage()
},
// 仅更新Token
setToken(token: string) {
this.token = token
this.syncToLocalStorage()
},
// 退出登录:清空用户状态
@@ -54,7 +51,6 @@ export const useUserStore = defineStore('user', {
this.userInfo = null
this.token = ''
this.isLoading = false
this.clearLocalStorage()
},
// 设置登录加载态
@@ -66,41 +62,13 @@ export const useUserStore = defineStore('user', {
updateUserInfo(partialData: Partial<IUserInfo>) {
if (this.userInfo) {
this.userInfo = { ...this.userInfo, ...partialData }
this.syncToLocalStorage()
}
},
// 同步到localStorage
syncToLocalStorage() {
if (typeof localStorage !== 'undefined') {
localStorage.setItem('user-auth-store', JSON.stringify({
token: this.token,
userInfo: this.userInfo
}))
}
},
// 从localStorage恢复
restoreFromLocalStorage() {
if (typeof localStorage !== 'undefined') {
const stored = localStorage.getItem('user-auth-store')
if (stored) {
try {
const data = JSON.parse(stored)
this.token = data.token || ''
this.userInfo = data.userInfo || null
} catch (e) {
console.error('恢复用户状态失败:', e)
}
}
}
},
// 清除localStorage
clearLocalStorage() {
if (typeof localStorage !== 'undefined') {
localStorage.removeItem('user-auth-store')
}
}
},
// 4. 持久化配置仅缓存核心状态token + userInfo
persist: {
storage: piniaPluginPersistedstate.localStorage(),
pick: ['token', 'userInfo']
}
})
})