diff --git a/docker-compose.yml b/docker-compose.yml index 9b910e8..2ae67dd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ services: build: context: . dockerfile: Dockerfile - image: tradus-web:1.2.8 + image: tradus-web:1.2.9 container_name: tradus-web ports: - '6000:80' diff --git a/src/App.vue b/src/App.vue index dc35395..4b3e85f 100644 --- a/src/App.vue +++ b/src/App.vue @@ -2,7 +2,7 @@ import { RouterView, useRoute } from 'vue-router' import { computed, ref, onMounted, onUnmounted, watch } from 'vue' import { useUserStore } from './stores/user' -import { authApi } from './services/api' +import { authApi, http } from './services/api' const route = useRoute() const isStandalonePage = computed(() => route.meta.standalone) @@ -15,24 +15,13 @@ const showUserMenu = ref(false) const showLoginModal = ref(false) const loginMode = ref('login') // 'login' 或 'register' -// 获取最新用户数据 +// 获取用户信息 const fetchUserInfo = async () => { if (!isAuthenticated.value) return try { - const headers: Record = { - 'Content-Type': 'application/json', - } - if (userStore.authHeader) { - headers['Authorization'] = userStore.authHeader - } - - const response = await fetch( + const response = await http.get( `${import.meta.env.MODE === 'development' ? 'http://127.0.0.1:8000' : 'https://api.ibtc.work'}/user/me`, - { - method: 'GET', - headers, - }, ) if (!response.ok) { @@ -45,10 +34,7 @@ const fetchUserInfo = async () => { }) } catch (error) { console.error('获取用户数据失败:', error) - // 如果获取用户信息失败,可能是 token 过期,清除登录状态 - if (error instanceof Error && error.message.includes('401')) { - userStore.logout() - } + // 处理在http工具中已经实现,这里不需要重复处理 } } diff --git a/src/services/api.ts b/src/services/api.ts index cda49c6..0219cd6 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -121,10 +121,34 @@ export function createAuthenticatedFetch() { // 处理401错误(未授权) if (response.status === 401) { userStore.logout() - window.location.href = '/login' + // 使用路由跳转到首页 + window.location.href = '/' + // 弹出提示 + alert('登录过期,需要重新登录') throw new Error('登录已过期,请重新登录') } return response } } + +// 导出通用的全局HTTP请求函数 +export const http = { + async get(url: string, options: RequestInit = {}) { + const authenticatedFetch = createAuthenticatedFetch() + return authenticatedFetch(url, { ...options, method: 'GET' }) + }, + + async post(url: string, data: any, options: RequestInit = {}) { + const authenticatedFetch = createAuthenticatedFetch() + return authenticatedFetch(url, { + ...options, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + ...options.headers, + }, + body: JSON.stringify(data), + }) + }, +} diff --git a/src/views/AIAgentView.vue b/src/views/AIAgentView.vue index 67a2919..131a33f 100644 --- a/src/views/AIAgentView.vue +++ b/src/views/AIAgentView.vue @@ -1,6 +1,7 @@