Files
ssw-mvp/web/src/utils/user.ts
T
admin 35edaaa702 chore: dag-panel 重组子目录 + web 头部用户修复 + 清理临时文件
- docs/e2e: 将 DAG 演示面板(dag-panel.mjs/html/data.mjs/sh/DAG演示.command)用 git mv 归入 dag-panel/ 子目录;修正相对依赖 ../lib、../results/dag-runs;清理 _diag_*.mjs 等临时诊断脚本(git rm 已跟踪项,rm 未跟踪项)
- web: 修复头部用户名称切账号不变(新增 utils/user.ts 响应式 userName,登录/挂载/路由切换时拉取 /oauth/CurrentUser)
- .gitignore: 新增 __pycache__/*.pyc 忽略规则(排除 demo 构建产物)
- demo: 多个演示脚本与算子集市用例更新
- .workbuddy/memory: 补充本次工作记录

注意:docs/e2e/sswflow 与 **/__pycache__/*.pyc 按要求排除,未纳入。
2026-08-11 15:31:41 +08:00

32 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { ref } from 'vue'
import request from '@/api/request'
import { getToken } from '@/api/request'
const NAME_KEY = 'ssw_user_name'
/**
* 头部显示的当前用户名称。模块级响应式 ref,App.vue 头部直接读取,
* 登录 / 切换用户 / 刷新后由 fetchCurrentUserName() 更新。
* 之前头部用 computed(() => localStorage.getItem('ssw_user_name') || '管理员'),
* 但 ssw_user_name 全程从未被写入,导致永远显示「管理员」,且 computed 读
* localStorage 不具响应式,切用户也不会重渲染。
*/
export const userName = ref(localStorage.getItem(NAME_KEY) || '')
export async function fetchCurrentUserName(): Promise<void> {
if (!getToken()) {
userName.value = ''
return
}
try {
// request 拦截器已解开 ActionResult.data,这里拿到 { menuList, userInfo, ... }
const data = await request.get('/oauth/CurrentUser')
const ui = (data && data.userInfo) || {}
const name: string = ui.userName || ui.userAccount || ui.account || ''
userName.value = name
if (name) localStorage.setItem(NAME_KEY, name)
} catch {
// 未登录 / 接口异常时保留原值,头部回退到默认「管理员」
}
}