核心功能:
1. 多项目切换:header 新增 ProjectDropdown,切项目时自动刷新权限菜单
2. 业务平台 SSO:header 新增「业务平台」按钮,OAuth2 授权码流程无感跳转
3. SSO 回调:/sso-callback 页面接收业务平台跳回的 code,换 IoT token 登录
共享包改动:
- packages/stores:access store 新增 projectId 字段并加入持久化
- packages/effects/layouts:新增 ProjectDropdown 共享组件
apps/web-antd 改动:
- api/request.ts:
· project-id 请求头仅在非 null 时设置(避免 axios 把 null 序列化为字符串 "null")
· X-Client-Id 改读 VITE_APP_CLIENT_ID,允许多个壳应用各自声明
- api/core/sso.ts:ssoCallback 参数走 body,避免 code 出现在浏览器历史/nginx 日志
- api/system/project:新增项目 simple-list API
- constants/sso.ts:集中 IOT_CLIENT_ID/BIZ_CLIENT_ID 等常量;
generateOauthState 用 crypto.randomUUID 生成 state,替代不安全的 Math.random
- store/auth.ts:抽 completeLogin 公共收尾逻辑,新增 ssoLogin 复用
- views/_core/authentication/sso-callback.vue:SSO 回调页;
dev 模式保留时延日志,失败时通过 query 透给登录页
- router/routes/core.ts:/sso-callback 路由 + beforeEnter 守卫
(缺 code 直接拦回登录页,避免死循环)
- layouts/basic.vue:
· 以 ProjectDropdown 替换 TenantDropdown(列表拉取失败兜底隐藏)
· 切项目时调用 fetchUserInfo,避免菜单/权限陈旧
· 新增「业务平台」跳转按钮;state 写 sessionStorage,
生产缺 VITE_BIZ_BASE_URL 时显式报错而非静默回 localhost
· setInterval 在 onUnmounted 中清理
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
157 lines
3.3 KiB
TypeScript
157 lines
3.3 KiB
TypeScript
import type { RouteRecordRaw } from 'vue-router';
|
|
|
|
import type { MenuRecordRaw } from '@vben-core/typings';
|
|
|
|
import { acceptHMRUpdate, defineStore } from 'pinia';
|
|
|
|
type AccessToken = null | string;
|
|
|
|
interface AccessState {
|
|
/**
|
|
* 权限码
|
|
*/
|
|
accessCodes: string[];
|
|
/**
|
|
* 可访问的菜单列表
|
|
*/
|
|
accessMenus: MenuRecordRaw[];
|
|
/**
|
|
* 可访问的路由列表
|
|
*/
|
|
accessRoutes: RouteRecordRaw[];
|
|
/**
|
|
* 登录 accessToken
|
|
*/
|
|
accessToken: AccessToken;
|
|
/**
|
|
* 是否已经检查过权限
|
|
*/
|
|
isAccessChecked: boolean;
|
|
/**
|
|
* 是否锁屏状态
|
|
*/
|
|
isLockScreen: boolean;
|
|
/**
|
|
* 锁屏密码
|
|
*/
|
|
lockScreenPassword?: string;
|
|
/**
|
|
* 登录是否过期
|
|
*/
|
|
loginExpired: boolean;
|
|
/**
|
|
* 当前项目编号
|
|
*/
|
|
projectId: null | number;
|
|
/**
|
|
* 登录 accessToken
|
|
*/
|
|
refreshToken: AccessToken;
|
|
/**
|
|
* 登录租户编号
|
|
*/
|
|
tenantId: null | number;
|
|
/**
|
|
* 访问租户编号
|
|
*/
|
|
visitTenantId: null | number;
|
|
}
|
|
|
|
/**
|
|
* @zh_CN 访问权限相关
|
|
*/
|
|
export const useAccessStore = defineStore('core-access', {
|
|
actions: {
|
|
getMenuByPath(path: string) {
|
|
function findMenu(
|
|
menus: MenuRecordRaw[],
|
|
path: string,
|
|
): MenuRecordRaw | undefined {
|
|
for (const menu of menus) {
|
|
if (menu.path === path) {
|
|
return menu;
|
|
}
|
|
if (menu.children) {
|
|
const matched = findMenu(menu.children, path);
|
|
if (matched) {
|
|
return matched;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return findMenu(this.accessMenus, path);
|
|
},
|
|
lockScreen(password: string) {
|
|
this.isLockScreen = true;
|
|
this.lockScreenPassword = password;
|
|
},
|
|
setAccessCodes(codes: string[]) {
|
|
this.accessCodes = codes;
|
|
},
|
|
setAccessMenus(menus: MenuRecordRaw[]) {
|
|
this.accessMenus = menus;
|
|
},
|
|
setAccessRoutes(routes: RouteRecordRaw[]) {
|
|
this.accessRoutes = routes;
|
|
},
|
|
setAccessToken(token: AccessToken) {
|
|
this.accessToken = token;
|
|
},
|
|
setIsAccessChecked(isAccessChecked: boolean) {
|
|
this.isAccessChecked = isAccessChecked;
|
|
},
|
|
setLoginExpired(loginExpired: boolean) {
|
|
this.loginExpired = loginExpired;
|
|
},
|
|
setRefreshToken(token: AccessToken) {
|
|
this.refreshToken = token;
|
|
},
|
|
setProjectId(projectId: null | number) {
|
|
this.projectId = projectId;
|
|
},
|
|
setTenantId(tenantId: null | number) {
|
|
this.tenantId = tenantId;
|
|
},
|
|
setVisitTenantId(visitTenantId: number) {
|
|
this.visitTenantId = visitTenantId;
|
|
},
|
|
unlockScreen() {
|
|
this.isLockScreen = false;
|
|
this.lockScreenPassword = undefined;
|
|
},
|
|
},
|
|
persist: {
|
|
// 持久化
|
|
pick: [
|
|
'accessToken',
|
|
'refreshToken',
|
|
'accessCodes',
|
|
'tenantId',
|
|
'visitTenantId',
|
|
'projectId',
|
|
'isLockScreen',
|
|
'lockScreenPassword',
|
|
],
|
|
},
|
|
state: (): AccessState => ({
|
|
accessCodes: [],
|
|
accessMenus: [],
|
|
accessRoutes: [],
|
|
accessToken: null,
|
|
isAccessChecked: false,
|
|
isLockScreen: false,
|
|
lockScreenPassword: undefined,
|
|
loginExpired: false,
|
|
projectId: null,
|
|
refreshToken: null,
|
|
tenantId: null,
|
|
visitTenantId: null,
|
|
}),
|
|
});
|
|
|
|
// 解决热更新问题
|
|
const hot = import.meta.hot;
|
|
if (hot) {
|
|
hot.accept(acceptHMRUpdate(useAccessStore, hot));
|
|
}
|