From f61716f215be74ec9c6b613a19e4567ebaa4cecb Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Sat, 22 Aug 2026 01:23:28 +0800 Subject: [PATCH] =?UTF-8?q?feat(auth):=20=E9=BB=98=E8=AE=A4=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E8=A7=92=E8=89=B2=E8=AE=BE=E7=BD=AE=EF=BC=88=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E8=A7=92=E8=89=B2=E5=AF=B9=E8=AF=9D=E6=A1=86=E6=98=9F?= =?UTF-8?q?=E6=A0=87=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 切换角色对话框每张角色卡片加星标按钮:设为/取消默认登录角色(金色高亮) - user store 新增 defaultRoleUuid 状态与 setDefaultRole 动作,fetchRoles 同步 IsDefault - auth.ts 新增 SetDefaultRole 接口(POST /Auth/SetDefaultRole) --- src/api/auth.ts | 7 +++++ src/components/RoleSwitcher.vue | 52 +++++++++++++++++++++++++++++++++ src/stores/user.ts | 14 ++++++++- 3 files changed, 72 insertions(+), 1 deletion(-) diff --git a/src/api/auth.ts b/src/api/auth.ts index d41b0c3..e3d9682 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -48,6 +48,8 @@ export interface RoleItem { CompanyUUID?: string /** 是否为当前激活角色 */ active?: boolean + /** 是否为默认登录角色(后端 User.DefaultRoleUUID) */ + IsDefault?: boolean } /** 获取当前用户角色列表(解包后端 data.roles 两层结构) */ @@ -60,6 +62,11 @@ export const switchRole = (role: RoleItem): Promise => { return post('/Auth/SwitchRole', { RoleUUID: role.RoleUUID }) } +/** 设置/清除默认登录角色(RoleUUID 为空 = 清除默认,登录后自动激活该角色) */ +export const setDefaultRole = (roleUuid: string): Promise => { + return post('/Auth/SetDefaultRole', { RoleUUID: roleUuid }) +} + /** 获取当前登录用户信息(silent=true 时失败不弹全局提示,用于登录页会话恢复) */ export const getCurrentUser = (silent = false): Promise => { return get('/Query/GetELTUserInfo', undefined, silent ? { silent: true } : undefined) diff --git a/src/components/RoleSwitcher.vue b/src/components/RoleSwitcher.vue index 2a9b23d..82072e6 100644 --- a/src/components/RoleSwitcher.vue +++ b/src/components/RoleSwitcher.vue @@ -72,6 +72,23 @@ async function confirm(): Promise { } } +const settingDefault = ref(false) + +/** 切换"默认登录角色":再点已默认的角色 = 取消默认(登录后自动激活该角色) */ +async function toggleDefault(role: RoleItem): Promise { + if (settingDefault.value || !isEnabled(role)) return + settingDefault.value = true + try { + const isDefault = userStore.defaultRoleUuid === role.RoleUUID + await userStore.setDefaultRole(isDefault ? null : role) + ElMessage.success(isDefault ? '已取消默认登录角色' : `已设为默认登录角色:"${role.RoleName}"`) + } catch { + ElMessage.error('设置默认角色失败,请重试') + } finally { + settingDefault.value = false + } +} + /** 角色描述:公司全名(公司简称),无公司时显示"无所属公司" */ function roleDesc(role: RoleItem): string { if (role.CompanyName) { @@ -123,6 +140,15 @@ function roleDesc(role: RoleItem): string {
+ + @@ -350,6 +376,32 @@ function roleDesc(role: RoleItem): string { } } +/* 默认登录角色星标(右下角,再点取消) */ +.role-default-btn { + position: absolute; + right: 10px; + bottom: 10px; + width: 28px; + height: 28px; + display: flex; + align-items: center; + justify-content: center; + border: none; + border-radius: 8px; + background: var(--el-fill-color); + color: var(--el-text-color-placeholder); + cursor: pointer; + transition: all 0.2s; +} +.role-default-btn:hover { + background: rgba(245, 158, 11, 0.15); + color: #f59e0b; +} +.role-default-btn.is-default { + background: rgba(245, 158, 11, 0.18); + color: #f59e0b; +} + /* 底部 */ .role-switcher-footer { display: flex; diff --git a/src/stores/user.ts b/src/stores/user.ts index 15b2c0e..8fa8330 100644 --- a/src/stores/user.ts +++ b/src/stores/user.ts @@ -1,7 +1,7 @@ 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, type RoleItem } from '@/api/auth' +import { getCurrentUser, getUserRoles, switchRole as apiSwitchRole, setDefaultRole as apiSetDefaultRole, login as apiLogin, logout as apiLogout, type LoginParams, type RoleItem } from '@/api/auth' import { getMenuList } from '@/api/menu' import { get } from '@/api/request' @@ -31,6 +31,8 @@ export const useUserStore = defineStore('user', () => { // === 状态 === const userInfo = ref(null) const roles = ref([]) + // 默认登录角色 UUID(后端 User.DefaultRoleUUID;登录后自动激活该角色) + const defaultRoleUuid = ref('') const menus = ref([]) // CheckAdmin 结果(后端按当前角色权限码判定管理员,不依赖 GetELTUserInfo 的 IsAdmin 字段) const adminCheck = ref(null) @@ -186,10 +188,18 @@ export const useUserStore = defineStore('user', () => { async function fetchRoles(): Promise { const list = await getUserRoles() roles.value = list + defaultRoleUuid.value = list.find((r) => r.IsDefault)?.RoleUUID || '' saveToCache() return list } + /** 设置/清除默认登录角色(role 为 null = 清除默认;后端存 User.DefaultRoleUUID) */ + async function setDefaultRole(role: RoleItem | null): Promise { + await apiSetDefaultRole(role?.RoleUUID || '') + defaultRoleUuid.value = role?.RoleUUID || '' + await fetchRoles() + } + /** 切换当前角色(身份切换) */ async function switchRole(role: RoleItem): Promise { await apiSwitchRole(role) @@ -245,6 +255,7 @@ export const useUserStore = defineStore('user', () => { // 状态 userInfo, roles, + defaultRoleUuid, menus, adminCheck, adminChecked, @@ -264,6 +275,7 @@ export const useUserStore = defineStore('user', () => { fetchRoles, fetchMenus, switchRole, + setDefaultRole, restoreSession, reset, }