148 lines
5.1 KiB
Vue
148 lines
5.1 KiB
Vue
<script setup lang="ts">
|
||
/**
|
||
* App.vue
|
||
* 根组件:主题初始化、全局消息弹窗、401 登出监听、html lang 与 i18n locale 同步。
|
||
* 依赖:useTheme、useGlobalMessageDialog、auth、linked-nav.store。
|
||
* 逻辑:已登录时预取关联服务侧栏入口,避免页面切换后入口后闪现。
|
||
* 书签/刷新冷启动遇到 401 时只回登录页;本页已确认会话后中途失效才弹窗。
|
||
*/
|
||
import { useI18n } from 'vue-i18n';
|
||
import { watch, onMounted, onBeforeUnmount } from 'vue';
|
||
import { useTheme } from '@/shared/composables/useTheme';
|
||
import LevelMessageDialog from '@/shared/components/LevelMessageDialog.vue';
|
||
import { useGlobalMessageDialog } from '@/shared/composables/useGlobalMessageDialog';
|
||
import { useRouter } from 'vue-router';
|
||
import { getAuthHeaders, logoutAuth, markAuthSessionConfirmed, shouldPromptOnSessionUnauthorized } from '@/shared/utils/auth';
|
||
import { isPublicPath } from '@/shared/constants/routes';
|
||
import { useLinkedNavStore } from '@/features/sso/stores/linked-nav.store';
|
||
|
||
const router = useRouter();
|
||
const { locale, t } = useI18n();
|
||
const { themePreference, initTheme, setThemePreference } = useTheme();
|
||
const {
|
||
messageDialogVisible,
|
||
messageDialogLevel,
|
||
messageDialogTitle,
|
||
messageDialogContent,
|
||
showMessageDialog,
|
||
closeMessageDialog,
|
||
} = useGlobalMessageDialog();
|
||
|
||
const updateLanguagePreference = async (lang: string) => {
|
||
const headers = getAuthHeaders();
|
||
if (Object.keys(headers).length === 0) return; // Not logged in
|
||
|
||
try {
|
||
await fetch('/api/user/profile', {
|
||
method: 'PUT',
|
||
headers: { 'Content-Type': 'application/json', ...headers },
|
||
body: JSON.stringify({ language: lang })
|
||
});
|
||
} catch (e) {
|
||
console.error('Failed to update language preference', e);
|
||
}
|
||
};
|
||
|
||
const updateThemePreference = async (theme: string) => {
|
||
const headers = getAuthHeaders();
|
||
if (Object.keys(headers).length === 0) return;
|
||
|
||
try {
|
||
await fetch('/api/user/profile', {
|
||
method: 'PUT',
|
||
headers: { 'Content-Type': 'application/json', ...headers },
|
||
body: JSON.stringify({ theme_preference: theme })
|
||
});
|
||
} catch (e) {
|
||
console.error('Failed to update theme preference', e);
|
||
}
|
||
};
|
||
|
||
watch(locale, (newLang) => {
|
||
updateLanguagePreference(newLang as string);
|
||
});
|
||
|
||
watch(themePreference, (newTheme) => {
|
||
updateThemePreference(newTheme);
|
||
});
|
||
|
||
/**
|
||
* 处理接口返回未登录状态,清理失效的本地登录信息。
|
||
* 核心逻辑:忽略可选的关联服务/SSO 目录请求。书签或刷新带来的冷启动 401 只回登录页;
|
||
* 本页已经成功加载过首页后会话才中途失效时,再提示重新登录。
|
||
* @param event - 包含请求地址和状态码的全局未授权事件。
|
||
*/
|
||
const onUnauthorized = (event: Event) => {
|
||
const url = String((event as CustomEvent<{ url?: string }>).detail?.url || '');
|
||
// 侧栏入口目录是可选导航能力,加载失败只能隐藏入口,不能清除有效 HXOPS 登录态。
|
||
if (/\/api\/sso\/apps(?:\?|$)/.test(url)) return;
|
||
if (/\/api\/linked-services(?:\?|$)/.test(url)) return;
|
||
if (shouldPromptOnSessionUnauthorized()) {
|
||
showMessageDialog('error', t('login.sessionExpiredHint'), t('login.sessionExpired'));
|
||
}
|
||
logoutAuth();
|
||
};
|
||
|
||
/**
|
||
* 登出后离开需要登录的页面。
|
||
* 核心逻辑:带上当前 fullPath 作为 redirect,登录成功后回到书签原来的地址。
|
||
* @param event - hxops:logout,remote 为 true 表示其他标签页触发。
|
||
*/
|
||
const onLogout = (event: Event) => {
|
||
useLinkedNavStore().reset();
|
||
const detail = (event as CustomEvent<{ remote?: boolean }>).detail;
|
||
if (detail?.remote) {
|
||
showMessageDialog('info', t('common.app.remoteLogout'));
|
||
}
|
||
const path = router.currentRoute.value.path;
|
||
if (!isPublicPath(path)) {
|
||
const redirect = router.currentRoute.value.fullPath;
|
||
router.push({ path: '/login', query: { redirect } });
|
||
}
|
||
};
|
||
|
||
onMounted(async () => {
|
||
window.addEventListener('hxops:unauthorized', onUnauthorized as EventListener);
|
||
window.addEventListener('hxops:logout', onLogout as EventListener);
|
||
|
||
initTheme();
|
||
|
||
const headers = getAuthHeaders();
|
||
if (Object.keys(headers).length > 0) {
|
||
// 尽早预取侧栏关联服务,避免进入 Chat/工作台后入口后加载闪现。
|
||
void useLinkedNavStore().ensureLoaded();
|
||
try {
|
||
const res = await fetch('/api/user/home', { headers });
|
||
if (res.ok) {
|
||
markAuthSessionConfirmed();
|
||
const data = await res.json();
|
||
if (data.user && data.user.language) {
|
||
locale.value = data.user.language;
|
||
}
|
||
if (data.user && data.user.theme_preference) {
|
||
setThemePreference(data.user.theme_preference);
|
||
}
|
||
}
|
||
} catch (e) {
|
||
console.error('Failed to sync language', e);
|
||
}
|
||
}
|
||
});
|
||
|
||
onBeforeUnmount(() => {
|
||
window.removeEventListener('hxops:unauthorized', onUnauthorized as EventListener);
|
||
window.removeEventListener('hxops:logout', onLogout as EventListener);
|
||
});
|
||
</script>
|
||
|
||
<template>
|
||
<router-view />
|
||
<LevelMessageDialog
|
||
v-model="messageDialogVisible"
|
||
:level="messageDialogLevel"
|
||
:title="messageDialogTitle"
|
||
:message="messageDialogContent"
|
||
@close="closeMessageDialog"
|
||
/>
|
||
</template>
|