This commit is contained in:
aaron 2025-05-18 23:23:20 +08:00
parent db1eece99d
commit 155a2e5cba
6 changed files with 41 additions and 74 deletions

View File

@ -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'

View File

@ -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<string, string> = {
'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
}
}

View File

@ -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),
})
},
}

View File

@ -1,6 +1,7 @@
<script setup lang="ts">
import { ref, nextTick, computed, onMounted, watch } from 'vue'
import { useUserStore } from '../stores/user'
import { http } from '../services/api'
// Agent
interface Agent {
@ -39,19 +40,11 @@ const apiBaseUrl =
// Agent
const fetchAgents = async () => {
if (!isAuthenticated.value) return
if (isLoadingAgents.value) return
isLoadingAgents.value = true
try {
const headers: Record<string, string> = {}
if (userStore.authHeader) {
headers['Authorization'] = userStore.authHeader
}
const response = await fetch(`${apiBaseUrl}/agent/list`, {
method: 'GET',
headers,
})
try {
const response = await http.get(`${apiBaseUrl}/agent/list`)
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
@ -228,23 +221,10 @@ const sendMessage = async () => {
await scrollToBottom()
try {
//
const headers: Record<string, string> = {
'Content-Type': 'application/json',
}
if (userStore.authHeader) {
headers['Authorization'] = userStore.authHeader
}
//
const response = await fetch(`${apiBaseUrl}/agent/chat`, {
method: 'POST',
headers,
body: JSON.stringify({
user_prompt: currentInput,
agent_id: selectedAgent.value.id,
}),
const response = await http.post(`${apiBaseUrl}/agent/chat`, {
agent_id: selectedAgent.value.id,
user_prompt: currentInput,
})
if (!response.ok) {

View File

@ -1,8 +1,7 @@
<script setup lang="ts">
import { ref, nextTick } from 'vue'
import { useUserStore } from '../stores/user'
import { http } from '../services/api'
const userStore = useUserStore()
const stockCode = ref('')
const isAnalyzing = ref(false)
const analysisContent = ref('')
@ -52,17 +51,7 @@ const handleAnalysis = async () => {
currentThought.value = '准备开始分析...'
try {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
}
if (userStore.authHeader) {
headers['Authorization'] = userStore.authHeader
}
const response = await fetch(`${apiBaseUrl}/adata/${code}/analysis`, {
method: 'POST',
headers,
})
const response = await http.post(`${apiBaseUrl}/adata/${code}/analysis`, {})
if (!response.ok) {
//

View File

@ -1,12 +1,11 @@
<script setup lang="ts">
import { ref, nextTick } from 'vue'
import { useUserStore } from '../stores/user'
import { http } from '../services/api'
interface AnalysisRequest {
symbol: string
}
const userStore = useUserStore()
const cryptoCode = ref('')
const isAnalyzing = ref(false)
const analysisContent = ref('')
@ -50,22 +49,11 @@ const handleAnalysis = async () => {
currentThought.value = '准备开始分析...'
try {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
}
if (userStore.authHeader) {
headers['Authorization'] = userStore.authHeader
}
const requestData: AnalysisRequest = {
symbol: cryptoCode.value.trim().toUpperCase(),
}
const response = await fetch(`${apiBaseUrl}/crypto/analysis_v2`, {
method: 'POST',
headers,
body: JSON.stringify(requestData),
})
const response = await http.post(`${apiBaseUrl}/crypto/analysis_v2`, requestData)
if (!response.ok) {
//