过渡版:本地验发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>
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
* 逻辑关系:封装 Fetch API 处理流式响应,供 ChatView 组件使用
|
||||
*/
|
||||
import { ref } from 'vue';
|
||||
import { getAuthHeaders } from '../utils/auth';
|
||||
|
||||
interface RetrieverResource {
|
||||
// 知识库/检索来源的基础字段,具体键可能因后端返回而扩展
|
||||
@@ -119,32 +120,6 @@ export function useChat() {
|
||||
*/
|
||||
const conversationId = ref<string | null>(null);
|
||||
|
||||
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 {};
|
||||
};
|
||||
|
||||
/**
|
||||
* sendMessage
|
||||
* 用途:发送消息并处理 SSE 流式响应
|
||||
|
||||
+18
-2
@@ -1,5 +1,4 @@
|
||||
|
||||
import { createApp } from 'vue'
|
||||
import { createApp } from 'vue'
|
||||
import './style.css'
|
||||
import App from './App.vue'
|
||||
import router from './router'
|
||||
@@ -9,6 +8,23 @@ import { config } from './config'
|
||||
// Set document title from config
|
||||
document.title = config.appTitle || 'AI工作台';
|
||||
|
||||
// Global 401 interceptor for API calls.
|
||||
const _rawFetch = window.fetch.bind(window);
|
||||
window.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
||||
const res = await _rawFetch(input, init);
|
||||
try {
|
||||
const url = typeof input === 'string'
|
||||
? input
|
||||
: (input instanceof URL ? input.toString() : input.url);
|
||||
if (res.status === 401 && url.includes('/api/')) {
|
||||
window.dispatchEvent(new CustomEvent('hxops:unauthorized', { detail: { url, status: res.status } }));
|
||||
}
|
||||
} catch {
|
||||
// no-op
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
createApp(App)
|
||||
.use(router)
|
||||
.use(i18n)
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
export interface AuthUser {
|
||||
id?: number;
|
||||
username?: string;
|
||||
role?: string;
|
||||
token?: string;
|
||||
access_token?: string;
|
||||
}
|
||||
|
||||
export const getStoredAuthUser = (): AuthUser | null => {
|
||||
const candidates = [
|
||||
sessionStorage.getItem('hxops_user'),
|
||||
localStorage.getItem('hxops_user'),
|
||||
];
|
||||
for (const stored of candidates) {
|
||||
if (!stored) continue;
|
||||
try {
|
||||
const data = JSON.parse(stored);
|
||||
return (data.user || data) as AuthUser;
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
export const getAuthToken = (): string => {
|
||||
const user = getStoredAuthUser();
|
||||
const token = user?.access_token || user?.token;
|
||||
return token ? String(token) : '';
|
||||
};
|
||||
|
||||
export const getAuthHeaders = (): Record<string, string> => {
|
||||
const token = getAuthToken();
|
||||
if (!token) return {};
|
||||
return { Authorization: `Bearer ${token}` };
|
||||
};
|
||||
@@ -19,6 +19,7 @@ import DeleteConfirmDialog from '../components/DeleteConfirmDialog.vue';
|
||||
import { loadReportLayoutPref, saveReportLayoutPref } from '../composables/useReportLayoutStorage';
|
||||
import { useGlobalMessageDialog } from '../composables/useGlobalMessageDialog';
|
||||
import { buildWecomAuditMeta, formatWecomAuditReason } from '../utils/wecom';
|
||||
import { getAuthHeaders } from '../utils/auth';
|
||||
import {
|
||||
ArrowLeft,
|
||||
// Save,
|
||||
@@ -748,29 +749,6 @@ const submitBatchPermission = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
const getAuthHeaders = () => {
|
||||
let stored = localStorage.getItem('hxops_user');
|
||||
if (!stored) stored = sessionStorage.getItem('hxops_user');
|
||||
|
||||
if (!stored) return {};
|
||||
|
||||
try {
|
||||
const data = JSON.parse(stored);
|
||||
const user = data.user || data; // Handle both wrapped and unwrapped format
|
||||
|
||||
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) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 加载 Agent 列表
|
||||
* 核心逻辑:调用 GET /api/admin/agents
|
||||
@@ -1327,7 +1305,7 @@ const resetQuestionForm = () => {
|
||||
// 组件挂载时加载初始数据
|
||||
onMounted(async () => {
|
||||
const headers = getAuthHeaders();
|
||||
if (!headers['X-User-Id'] && !headers['X-Username']) {
|
||||
if (!headers['Authorization']) {
|
||||
console.warn('No auth headers found, redirecting to login');
|
||||
router.push('/');
|
||||
return;
|
||||
|
||||
@@ -21,6 +21,7 @@ import FilePreviewModal from '../components/FilePreviewModal.vue';
|
||||
import ThemeToggleButton from '../components/ThemeToggleButton.vue';
|
||||
import GeneralFeedbackDialog from '../components/GeneralFeedbackDialog.vue';
|
||||
import { useGlobalMessageDialog } from '../composables/useGlobalMessageDialog';
|
||||
import { getAuthHeaders } from '../utils/auth';
|
||||
|
||||
const { t, locale } = useI18n();
|
||||
const { showMessageDialog } = useGlobalMessageDialog();
|
||||
@@ -402,33 +403,6 @@ const presetQuestion = computed(() => {
|
||||
return typeof q === 'string' ? q : '';
|
||||
});
|
||||
|
||||
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);
|
||||
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 {};
|
||||
};
|
||||
|
||||
/**
|
||||
* handleSend
|
||||
* 用途:将输入框内容发送给当前 Agent
|
||||
|
||||
@@ -6,6 +6,7 @@ import { useTheme } from '../composables/useTheme';
|
||||
import type { ThemePreference } from '../composables/useTheme';
|
||||
import ThemeToggleButton from '../components/ThemeToggleButton.vue';
|
||||
import { useGlobalMessageDialog } from '../composables/useGlobalMessageDialog';
|
||||
import { getAuthHeaders } from '../utils/auth';
|
||||
import {
|
||||
User,
|
||||
Mail,
|
||||
@@ -99,25 +100,6 @@ const cancelEditing = () => {
|
||||
};
|
||||
|
||||
// Methods
|
||||
const getAuthHeaders = (): Record<string, string> => {
|
||||
let stored = localStorage.getItem('hxops_user');
|
||||
if (!stored) stored = sessionStorage.getItem('hxops_user');
|
||||
|
||||
if (!stored) return {};
|
||||
|
||||
try {
|
||||
const data = JSON.parse(stored);
|
||||
const u = data.user || data;
|
||||
return {
|
||||
'X-Username': u.username,
|
||||
'X-Role': u.role,
|
||||
'X-User-Id': String(u.id)
|
||||
};
|
||||
} catch (e) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
const fetchProfile = async () => {
|
||||
isLoading.value = true;
|
||||
try {
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
type WecomLoginDialogContent,
|
||||
type WecomMatchField,
|
||||
} from '../utils/wecom';
|
||||
import { getAuthHeaders, getStoredAuthUser } from '../utils/auth';
|
||||
import {
|
||||
Settings,
|
||||
LayoutGrid,
|
||||
@@ -233,33 +234,11 @@ const getSectionTitleKey = (sectionId: string) => {
|
||||
};
|
||||
|
||||
// Methods
|
||||
const getAuthHeaders = () => {
|
||||
let stored = localStorage.getItem('hxops_user');
|
||||
if (!stored) stored = sessionStorage.getItem('hxops_user');
|
||||
|
||||
if (!stored) return {};
|
||||
|
||||
try {
|
||||
const data = JSON.parse(stored);
|
||||
const user = data.user || data; // Handle both wrapped and unwrapped format
|
||||
|
||||
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) {
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
const fetchHome = async () => {
|
||||
try {
|
||||
const res = await fetch('/api/user/home', { headers: getAuthHeaders() });
|
||||
if (res.status === 401) {
|
||||
showMessageDialog('error', t('login.loginRequired'));
|
||||
handleLogout();
|
||||
return;
|
||||
}
|
||||
@@ -389,12 +368,12 @@ const loadAuthUser = () => {
|
||||
if (!stored) return;
|
||||
|
||||
try {
|
||||
const data = JSON.parse(stored);
|
||||
const user = data.user || data; // Handle both wrapped and unwrapped format
|
||||
const user = getStoredAuthUser();
|
||||
if (!user) return;
|
||||
currentUser.value = {
|
||||
id: user.id || 0,
|
||||
username: user.username,
|
||||
role: user.role,
|
||||
username: user.username || '',
|
||||
role: user.role || 'user',
|
||||
};
|
||||
} catch (e) {
|
||||
localStorage.removeItem('hxops_user');
|
||||
@@ -403,12 +382,16 @@ const loadAuthUser = () => {
|
||||
};
|
||||
|
||||
const persistLoginUser = (user: any) => {
|
||||
const nextUser = { ...user };
|
||||
if (user?.access_token && !user?.token) {
|
||||
nextUser.token = user.access_token;
|
||||
}
|
||||
if (rememberMe.value) {
|
||||
const expiry = new Date().getTime() + 30 * 24 * 60 * 60 * 1000;
|
||||
localStorage.setItem('hxops_user', JSON.stringify({ user, expiry }));
|
||||
localStorage.setItem('hxops_user', JSON.stringify({ user: nextUser, expiry }));
|
||||
sessionStorage.removeItem('hxops_user');
|
||||
} else {
|
||||
sessionStorage.setItem('hxops_user', JSON.stringify({ user }));
|
||||
sessionStorage.setItem('hxops_user', JSON.stringify({ user: nextUser }));
|
||||
localStorage.removeItem('hxops_user');
|
||||
}
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user