1.修复问题同时打开多个标签页,退出登录,其它几个标签页并没有退出登录

2.点击智能体时,若当前已经打开智能体标签,则不会再打开新标签页
This commit is contained in:
hugo committed 2026-05-26 20:58:11 +08:00
1 parent 3642ed8529
commit 63ea9d7ed7
6 files changed
+201 -19

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
const agentWindowName = (agentId: number): string => `hxops_agent_${agentId}`;
const isBlankWindow = (win: Window): boolean => {
try {
const href = win.location.href;
return href === 'about:blank' || href.endsWith('about:blank');
} catch {
return true;
}
};
/** Mark this browser tab as the chat window for an agent (used by window.open target name). */
export const registerOpenAgentId = (agentId: number | null): void => {
if (agentId && Number.isFinite(agentId) && agentId > 0) {
window.name = agentWindowName(agentId);
return;
}
if (window.name.startsWith('hxops_agent_')) {
window.name = '';
}
};
/** App startup hook (kept for compatibility). */
export const initAgentTabSync = (): void => {
// Named-window focus does not require background listeners.
};
/**
* Focus an existing same-agent tab without reload, or open a new one.
*
* Per HTML: window.open('', target) focuses an existing named window and
* ignores the URL, so the page is not refreshed.
*/
export const openAgentChatTab = (url: string, agentId: number): void => {
const target = agentWindowName(agentId);
const existing = window.open('', target);
if (!existing || existing.closed) {
const opened = window.open(url, target);
opened?.focus();
return;
}
existing.focus();
if (isBlankWindow(existing)) {
try {
existing.location.href = url;
} catch {
window.open(url, target);
}
}
};
+74 -1
View File
@@ -58,13 +58,86 @@ export const saveAuthUser = (user: AuthUser, remember = false): void => {
}
};
export const clearAuthUser = (): void => {
const AUTH_LOGOUT_KEY = 'hxops_auth_logout';
const AUTH_BC_NAME = 'hxops_auth';
let _authBroadcast: BroadcastChannel | null = null;
let _authSyncInitialized = false;
const clearAuthStorage = (): void => {
localStorage.removeItem('hxops_user');
sessionStorage.removeItem('hxops_user');
sessionStorage.removeItem('hxops_wecom_state');
sessionStorage.removeItem('hxops_casdoor_state');
};
const broadcastLogout = (): void => {
try {
localStorage.setItem(AUTH_LOGOUT_KEY, String(Date.now()));
localStorage.removeItem(AUTH_LOGOUT_KEY);
} catch {
// ignore quota / private mode errors
}
try {
if (!_authBroadcast) {
_authBroadcast = new BroadcastChannel(AUTH_BC_NAME);
}
_authBroadcast.postMessage({ type: 'logout', ts: Date.now() });
} catch {
// BroadcastChannel may be unavailable
}
};
const dispatchLogout = (detail: Record<string, unknown> = {}): void => {
window.dispatchEvent(new CustomEvent('hxops:logout', { detail }));
};
const handleRemoteLogout = (): void => {
if (!getStoredAuthUser()) return;
clearAuthStorage();
dispatchLogout({ remote: true });
};
/** Clear credentials and notify other tabs in the same browser. */
export const logoutAuth = (): void => {
clearAuthStorage();
broadcastLogout();
dispatchLogout({ source: 'local' });
};
export const clearAuthUser = (): void => {
logoutAuth();
};
/** Listen for logout in other tabs (storage + BroadcastChannel). */
export const initAuthSync = (): void => {
if (_authSyncInitialized) return;
_authSyncInitialized = true;
window.addEventListener('storage', (event: StorageEvent) => {
if (event.key === 'hxops_user' && event.newValue === null && event.oldValue != null) {
handleRemoteLogout();
return;
}
if (event.key === AUTH_LOGOUT_KEY && event.newValue != null) {
handleRemoteLogout();
}
});
try {
if (!_authBroadcast) {
_authBroadcast = new BroadcastChannel(AUTH_BC_NAME);
}
_authBroadcast.onmessage = (event: MessageEvent) => {
if (event.data?.type === 'logout') {
handleRemoteLogout();
}
};
} catch {
// ignore
}
};
let _refreshing: Promise<string | null> | null = null;
export const refreshAccessToken = async (): Promise<string | null> => {