front/src/utils/auth.ts
zzh 7585e0c7b7 feat: 完善三级按钮同步、我的订单与助手等前端能力
新增页面按钮目录与菜单同步三级按钮;增强我的订单(问题件/理赔件、费用、详情抽屉)、Nana 助手、联系客服、渠道问题模板及登录/RBAC 等相关功能。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-07 12:36:18 +08:00

46 lines
1.3 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.

const TOKEN_KEY = 'token';
// RBAC 系统使用的 token key,与 isLogin 保持一致
const RBAC_TOKEN_KEY = 'rbac_access_token';
const SSO_SESSION_FLAG = 'rbac_sso_session';
function isSsoSession(): boolean {
return sessionStorage.getItem(SSO_SESSION_FLAG) === '1';
}
const isLogin = () => {
if (isSsoSession()) {
return !!(
sessionStorage.getItem(TOKEN_KEY) ||
sessionStorage.getItem(RBAC_TOKEN_KEY)
);
}
// 同时检查两种 token 存储,确保登录后首次跳转能正确识别
return !!(localStorage.getItem(TOKEN_KEY) || localStorage.getItem(RBAC_TOKEN_KEY));
};
const getToken = () => {
if (isSsoSession()) {
return sessionStorage.getItem(TOKEN_KEY) || sessionStorage.getItem(RBAC_TOKEN_KEY);
}
return localStorage.getItem(TOKEN_KEY);
};
const setToken = (token: string, sessionOnly = false) => {
if (sessionOnly) {
sessionStorage.setItem(SSO_SESSION_FLAG, '1');
sessionStorage.setItem(TOKEN_KEY, token);
sessionStorage.setItem(RBAC_TOKEN_KEY, token);
return;
}
localStorage.setItem(TOKEN_KEY, token);
};
const clearToken = () => {
localStorage.removeItem(TOKEN_KEY);
sessionStorage.removeItem(TOKEN_KEY);
sessionStorage.removeItem(RBAC_TOKEN_KEY);
sessionStorage.removeItem(SSO_SESSION_FLAG);
};
export { isLogin, getToken, setToken, clearToken };