修正了切换角色功能
This commit is contained in:
1 parent
f062fb1e29
commit
15bcb072f9
3 files changed
+64
-19
No files matched your search
+12
-7
@@ -1,14 +1,14 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import type { UserInfo, MenuItem } from '@/types/api'
|
||||
import { getCurrentUser, getUserRoles, switchRole as apiSwitchRole, login as apiLogin, logout as apiLogout, type LoginParams } from '@/api/auth'
|
||||
import { getCurrentUser, getUserRoles, switchRole as apiSwitchRole, login as apiLogin, logout as apiLogout, type LoginParams, type RoleItem } from '@/api/auth'
|
||||
import { getMenuList } from '@/api/menu'
|
||||
|
||||
/** 用户状态管理:当前登录用户、角色、菜单、权限 */
|
||||
export const useUserStore = defineStore('user', () => {
|
||||
// === 状态 ===
|
||||
const userInfo = ref<UserInfo | null>(null)
|
||||
const roles = ref<string[]>([])
|
||||
const roles = ref<RoleItem[]>([])
|
||||
const menus = ref<MenuItem[]>([])
|
||||
const token = ref<string>('')
|
||||
const loading = ref(false)
|
||||
@@ -23,9 +23,10 @@ export const useUserStore = defineStore('user', () => {
|
||||
try {
|
||||
const raw = localStorage.getItem(CACHE_KEY)
|
||||
if (!raw) return
|
||||
const cached = JSON.parse(raw) as { userInfo?: UserInfo | null; menus?: MenuItem[] | null }
|
||||
const cached = JSON.parse(raw) as { userInfo?: UserInfo | null; menus?: MenuItem[] | null; roles?: RoleItem[] | null }
|
||||
if (cached.userInfo) userInfo.value = cached.userInfo
|
||||
if (cached.menus) menus.value = cached.menus
|
||||
if (cached.roles?.length) roles.value = cached.roles
|
||||
} catch {
|
||||
// 缓存损坏:清除避免反复解析失败
|
||||
localStorage.removeItem(CACHE_KEY)
|
||||
@@ -37,7 +38,7 @@ export const useUserStore = defineStore('user', () => {
|
||||
try {
|
||||
localStorage.setItem(
|
||||
CACHE_KEY,
|
||||
JSON.stringify({ userInfo: userInfo.value, menus: menus.value }),
|
||||
JSON.stringify({ userInfo: userInfo.value, menus: menus.value, roles: roles.value }),
|
||||
)
|
||||
} catch {
|
||||
// 写入失败(隐私模式/配额满):忽略,不影响功能
|
||||
@@ -76,6 +77,7 @@ export const useUserStore = defineStore('user', () => {
|
||||
await apiLogin(params)
|
||||
await fetchUserInfo()
|
||||
await fetchMenus()
|
||||
await fetchRoles()
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
@@ -125,15 +127,18 @@ export const useUserStore = defineStore('user', () => {
|
||||
}
|
||||
|
||||
/** 获取角色列表 */
|
||||
async function fetchRoles(): Promise<string[]> {
|
||||
async function fetchRoles(): Promise<RoleItem[]> {
|
||||
const list = await getUserRoles()
|
||||
roles.value = list
|
||||
saveToCache()
|
||||
return list
|
||||
}
|
||||
|
||||
/** 切换当前角色(身份切换) */
|
||||
async function switchRole(role: string): Promise<void> {
|
||||
await apiSwitchRole(role)
|
||||
async function switchRole(role: RoleItem | string): Promise<void> {
|
||||
// 传对象时取 RoleName(后端 SwitchRole 期望角色名)
|
||||
const roleName = typeof role === 'string' ? role : role.RoleName
|
||||
await apiSwitchRole(roleName)
|
||||
// 刷新用户信息和菜单(不同角色可能有不同菜单权限)
|
||||
await fetchUserInfo()
|
||||
await fetchMenus()
|
||||
|
||||
Reference in new issue
Block a user