修复个人中心问题

This commit is contained in:
hero920103 committed 2026-09-29 18:25:09 +08:00
1 parent 0dc5c2f075
commit ca4220f92c
44 files changed
+1793 -310

No files matched your search

+31 -2
View File
@@ -106,6 +106,28 @@ async function loadTenants() {
}
}
/**
* 企业切换后,上一家的菜单 / 标签页 / 企业信息缓存都必须失效。
* 菜单虽然是按 tid 建 key,但旧 key 仍会留在本地,顺手清掉可避免脏数据累积。
*/
function clearTenantScopedCache() {
try {
const removeKeys: string[] = [];
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key && (key.startsWith("menu_cache_") || key === "tabs_list" || key === "active_tab")) {
removeKeys.push(key);
}
}
removeKeys.forEach((key) => localStorage.removeItem(key));
sessionStorage.removeItem("tabs_list");
localStorage.removeItem("tenant");
sessionStorage.removeItem("tenant");
} catch (e) {
console.warn("清理企业级缓存失败:", e);
}
}
async function handleSwitch(tid: number | string) {
if (tid === RELOAD_COMMAND) {
await loadTenants();
@@ -115,9 +137,16 @@ async function handleSwitch(tid: number | string) {
const target = tenantOptions.value.find((item) => String(item.tid) === String(tid));
try {
await switchTenant(tid);
clearTenantScopedCache();
ElMessage.success(`已切换到「${target?.tenant_name || "新企业"}」`);
// 菜单、按钮权限、模块套餐均按企业隔离,整页重载确保全部按新企业重新初始化
setTimeout(() => window.location.reload(), 300);
// 切企业后必须离开当前页:当前页可能属于上一家才开通的模块,
// 停留在原地会让新企业未开通的功能继续可用。回到首页(门户)锁死入口,
// 菜单/权限/套餐再按新企业整页初始化。
// 用 replaceState 改 hash(不触发路由跳转),避免用旧企业状态多渲染一次
setTimeout(() => {
window.history.replaceState({}, document.title, `${window.location.pathname}#/home`);
window.location.reload();
}, 300);
} catch (e: any) {
ElMessage.error(e?.message || "切换企业失败");
}