过渡版:本地验发token
This commit is contained in:
1 parent
fb71def840
commit
a5f0ec645d
14 files changed
+313
-220
No files matched your search
+19
-27
@@ -1,10 +1,11 @@
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { watch, onMounted } from 'vue';
|
||||
import { watch, onMounted, onBeforeUnmount } from 'vue';
|
||||
import { useTheme } from './composables/useTheme';
|
||||
import LevelMessageDialog from './components/LevelMessageDialog.vue';
|
||||
import { useGlobalMessageDialog } from './composables/useGlobalMessageDialog';
|
||||
import { getAuthHeaders } from './utils/auth';
|
||||
|
||||
const { locale } = useI18n();
|
||||
const { themePreference, initTheme, setThemePreference } = useTheme();
|
||||
@@ -13,35 +14,10 @@ const {
|
||||
messageDialogLevel,
|
||||
messageDialogTitle,
|
||||
messageDialogContent,
|
||||
showMessageDialog,
|
||||
closeMessageDialog,
|
||||
} = useGlobalMessageDialog();
|
||||
|
||||
const getAuthHeaders = () => {
|
||||
const candidates = [
|
||||
sessionStorage.getItem('hxops_user'),
|
||||
localStorage.getItem('hxops_user'),
|
||||
];
|
||||
for (const stored of candidates) {
|
||||
if (!stored) continue;
|
||||
try {
|
||||
const data = JSON.parse(stored) as any;
|
||||
const user = data.user || data;
|
||||
if (!user?.username || !user?.role) continue;
|
||||
const headers: Record<string, string> = {
|
||||
'X-Username': user.username,
|
||||
'X-Role': user.role,
|
||||
};
|
||||
if (user.id) {
|
||||
headers['X-User-Id'] = String(user.id);
|
||||
}
|
||||
return headers;
|
||||
} catch (e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
const updateLanguagePreference = async (lang: string) => {
|
||||
const headers = getAuthHeaders();
|
||||
if (Object.keys(headers).length === 0) return; // Not logged in
|
||||
@@ -80,7 +56,19 @@ watch(themePreference, (newTheme) => {
|
||||
updateThemePreference(newTheme);
|
||||
});
|
||||
|
||||
const onUnauthorized = () => {
|
||||
showMessageDialog('error', '认证已过期或无效,请重新登录');
|
||||
localStorage.removeItem('hxops_user');
|
||||
sessionStorage.removeItem('hxops_user');
|
||||
sessionStorage.removeItem('hxops_wecom_state');
|
||||
if (window.location.pathname !== '/') {
|
||||
window.location.href = '/';
|
||||
}
|
||||
};
|
||||
|
||||
onMounted(async () => {
|
||||
window.addEventListener('hxops:unauthorized', onUnauthorized as EventListener);
|
||||
|
||||
initTheme();
|
||||
|
||||
const headers = getAuthHeaders();
|
||||
@@ -101,6 +89,10 @@ onMounted(async () => {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
window.removeEventListener('hxops:unauthorized', onUnauthorized as EventListener);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
||||
Reference in new issue
Block a user