fix: 优化错误提示
This commit is contained in:
1 parent
d57ecc62f9
commit
239d373503
14 files changed
+333
-38
No files matched your search
+16
-5
@@ -4,6 +4,7 @@
|
||||
* 根组件:主题初始化、全局消息弹窗、401 登出监听、html lang 与 i18n locale 同步。
|
||||
* 依赖:useTheme、useGlobalMessageDialog、auth、linked-nav.store。
|
||||
* 逻辑:已登录时预取关联服务侧栏入口,避免页面切换后入口后闪现。
|
||||
* 书签/刷新冷启动遇到 401 时只回登录页;本页已确认会话后中途失效才弹窗。
|
||||
*/
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { watch, onMounted, onBeforeUnmount } from 'vue';
|
||||
@@ -11,7 +12,7 @@ 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 } from '@/shared/utils/auth';
|
||||
import { getAuthHeaders, logoutAuth, markAuthSessionConfirmed, shouldPromptOnSessionUnauthorized } from '@/shared/utils/auth';
|
||||
import { isPublicPath } from '@/shared/constants/routes';
|
||||
import { useLinkedNavStore } from '@/features/sso/stores/linked-nav.store';
|
||||
|
||||
@@ -66,8 +67,9 @@ watch(themePreference, (newTheme) => {
|
||||
});
|
||||
|
||||
/**
|
||||
* 处理接口返回未登录状态,提示用户重新登录并清理失效的本地登录信息。
|
||||
* 核心逻辑:忽略可选的关联服务/SSO 目录请求;其余未授权请求统一展示易懂提示,再执行登出。
|
||||
* 处理接口返回未登录状态,清理失效的本地登录信息。
|
||||
* 核心逻辑:忽略可选的关联服务/SSO 目录请求。书签或刷新带来的冷启动 401 只回登录页;
|
||||
* 本页已经成功加载过首页后会话才中途失效时,再提示重新登录。
|
||||
* @param event - 包含请求地址和状态码的全局未授权事件。
|
||||
*/
|
||||
const onUnauthorized = (event: Event) => {
|
||||
@@ -75,10 +77,17 @@ const onUnauthorized = (event: Event) => {
|
||||
// 侧栏入口目录是可选导航能力,加载失败只能隐藏入口,不能清除有效 HXOPS 登录态。
|
||||
if (/\/api\/sso\/apps(?:\?|$)/.test(url)) return;
|
||||
if (/\/api\/linked-services(?:\?|$)/.test(url)) return;
|
||||
showMessageDialog('error', t('login.sessionExpiredHint'), t('login.sessionExpired'));
|
||||
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;
|
||||
@@ -87,7 +96,8 @@ const onLogout = (event: Event) => {
|
||||
}
|
||||
const path = router.currentRoute.value.path;
|
||||
if (!isPublicPath(path)) {
|
||||
router.push('/login');
|
||||
const redirect = router.currentRoute.value.fullPath;
|
||||
router.push({ path: '/login', query: { redirect } });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -104,6 +114,7 @@ onMounted(async () => {
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user