task-7(壳层/路由): 根路由跳转到首个可见页面,移除硬编码默认页
helpers.firstVisiblePath 深度优先返回首个可路由页面,无页面返回空串; store 增加 firstVisible;守卫在根路由/无权限时跳首个可见页,全部无页面则 停留根路由展示无菜单空态,删除 redirect:'/account/users' 硬编码。
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { fetchAdminMenuTree, fetchCurrentUser, logout } from '@/api/session'
|
||||
import type { AdminMenuNode, AdminUser } from '@/types/admin'
|
||||
import { firstVisiblePath } from '@/router/helpers'
|
||||
|
||||
export const useAdminSessionStore = defineStore('admin-session', {
|
||||
state: () => ({
|
||||
user: null as AdminUser | null,
|
||||
menuTree: [] as AdminMenuNode[],
|
||||
initialized: false,
|
||||
loading: false,
|
||||
error: '',
|
||||
}),
|
||||
getters: {
|
||||
isSuperAdmin: (state) => state.user?.role === 'super_admin',
|
||||
firstVisible: (state): string => {
|
||||
return firstVisiblePath(state.menuTree)
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
async initialize() {
|
||||
if (this.initialized || this.loading) return
|
||||
this.loading = true
|
||||
this.error = ''
|
||||
try {
|
||||
this.user = await fetchCurrentUser()
|
||||
this.menuTree = await fetchAdminMenuTree()
|
||||
this.initialized = true
|
||||
} catch (error) {
|
||||
this.error = error instanceof Error ? error.message : '后台初始化失败'
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
async signOut() {
|
||||
await logout()
|
||||
this.$reset()
|
||||
window.location.assign('/login')
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user