From a2fdf78121ff62e2b8964e2d13e336caca52ac68 Mon Sep 17 00:00:00 2001 From: zaunist Date: Mon, 7 Apr 2025 20:21:59 +0800 Subject: [PATCH] fix: use correct axios client --- mobile/eas.json | 5 +- mobile/src/api/agents.ts | 2 +- mobile/src/api/client.ts | 77 +++++++++++++++++++++----- mobile/src/api/monitors.ts | 2 +- mobile/src/api/notifications.ts | 2 +- mobile/src/utils/axios.ts | 95 --------------------------------- 6 files changed, 71 insertions(+), 112 deletions(-) delete mode 100644 mobile/src/utils/axios.ts diff --git a/mobile/eas.json b/mobile/eas.json index b3c02e0..bc86372 100644 --- a/mobile/eas.json +++ b/mobile/eas.json @@ -12,7 +12,10 @@ } }, "preview": { - "distribution": "internal" + "distribution": "internal", + "android": { + "buildType": "apk" + } }, "production": { "autoIncrement": true diff --git a/mobile/src/api/agents.ts b/mobile/src/api/agents.ts index c5cdd0a..e3b7dc1 100644 --- a/mobile/src/api/agents.ts +++ b/mobile/src/api/agents.ts @@ -1,4 +1,4 @@ -import axios from '../utils/axios'; +import axios from './client'; import { API_ENDPOINTS } from '../config/api'; // 客户端类型 diff --git a/mobile/src/api/client.ts b/mobile/src/api/client.ts index 4ae2558..c7b6d51 100644 --- a/mobile/src/api/client.ts +++ b/mobile/src/api/client.ts @@ -2,6 +2,9 @@ import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; import AsyncStorage from '@react-native-async-storage/async-storage'; import { API_BASE_URL, loadApiBaseUrl } from '../config/api'; +// 开启详细日志输出用于调试 +const enableDebugLogs = true; + // 创建一个Axios实例 const apiClient: AxiosInstance = axios.create({ baseURL: API_BASE_URL, @@ -25,6 +28,21 @@ apiClient.interceptors.request.use( config.baseURL = API_BASE_URL; } + // 调试日志 + if (enableDebugLogs) { + const method = config.method?.toUpperCase() || 'UNKNOWN'; + const baseUrl = config.baseURL || ''; + const url = config.url || ''; + + console.log(`🚀 发送${method}请求: ${baseUrl}${url}`); + if (config.data) { + console.log('📦 请求数据:', JSON.stringify(config.data).substring(0, 500)); + } + if (config.params) { + console.log('🔍 请求参数:', config.params); + } + } + // 从存储中获取token const token = await AsyncStorage.getItem('auth_token'); @@ -36,6 +54,7 @@ apiClient.interceptors.request.use( return config; }, (error) => { + console.error('❌ 请求发送失败:', error); return Promise.reject(error); } ); @@ -43,24 +62,56 @@ apiClient.interceptors.request.use( // 响应拦截器 apiClient.interceptors.response.use( (response) => { + // 添加调试日志 + if (enableDebugLogs) { + const method = response.config.method?.toUpperCase() || 'UNKNOWN'; + const url = response.config.url || 'unknown'; + console.log(`✅ ${method}请求成功: ${url}`); + console.log(`📊 状态码: ${response.status}`); + console.log('📦 响应数据:', JSON.stringify(response.data).substring(0, 500)); + } return response; }, async (error) => { - const originalRequest = error.config; - - // 如果是401错误且没有重试过,尝试刷新token - if (error.response && error.response.status === 401 && !originalRequest._retry) { - originalRequest._retry = true; + // 添加详细的错误日志 + if (enableDebugLogs) { + console.error('❌ 请求失败:', error.message); - try { - // 在实际应用中,这里应该调用刷新token的接口 - // 由于当前后端没有刷新token的接口,这里简单处理为清除token并返回错误 - await AsyncStorage.removeItem('auth_token'); + if (error.response) { + // 服务器返回了错误状态码 + const method = error.config?.method?.toUpperCase() || 'UNKNOWN'; + const url = error.config?.url || 'unknown'; + console.error(`🔥 ${method}请求错误: ${url}`); + console.error(`📊 状态码: ${error.response.status}`); - // 重定向到登录页的逻辑会在组件中处理 - return Promise.reject(error); - } catch (refreshError) { - return Promise.reject(refreshError); + try { + console.error('📦 错误响应:', JSON.stringify(error.response.data).substring(0, 500)); + } catch (e) { + console.error('📦 错误响应(非JSON):', String(error.response.data).substring(0, 500)); + } + + // 处理401未授权错误 + if (error.response.status === 401 && !error.config._retry) { + error.config._retry = true; + + try { + // 在实际应用中,这里应该调用刷新token的接口 + // 由于当前后端没有刷新token的接口,这里简单处理为清除token并返回错误 + await AsyncStorage.removeItem('auth_token'); + console.warn('⚠️ 用户未授权,已清除token'); + + // 重定向到登录页的逻辑会在组件中处理 + return Promise.reject(error); + } catch (refreshError) { + return Promise.reject(refreshError); + } + } + } else if (error.request) { + // 请求已发送但没有收到响应 + console.error('⏱️ 请求超时或无响应:', error.config?.url || 'unknown'); + } else { + // 设置请求时发生错误 + console.error('🔧 请求配置错误:', error.message); } } diff --git a/mobile/src/api/monitors.ts b/mobile/src/api/monitors.ts index 25d73ea..5ca53a7 100644 --- a/mobile/src/api/monitors.ts +++ b/mobile/src/api/monitors.ts @@ -1,4 +1,4 @@ -import axios from '../utils/axios'; +import axios from './client'; import { API_ENDPOINTS } from '../config/api'; // 监控类型 diff --git a/mobile/src/api/notifications.ts b/mobile/src/api/notifications.ts index eedbffc..abefee3 100644 --- a/mobile/src/api/notifications.ts +++ b/mobile/src/api/notifications.ts @@ -1,4 +1,4 @@ -import axios from '../utils/axios'; +import axios from './client'; import { API_ENDPOINTS } from '../config/api'; // 通知渠道类型 diff --git a/mobile/src/utils/axios.ts b/mobile/src/utils/axios.ts deleted file mode 100644 index b7dbb8f..0000000 --- a/mobile/src/utils/axios.ts +++ /dev/null @@ -1,95 +0,0 @@ -import axios from 'axios'; -import AsyncStorage from '@react-native-async-storage/async-storage'; -import { API_BASE_URL } from '../config/api'; - -// 开启详细日志输出用于调试 -const enableDebugLogs = true; - -// 创建axios实例 -const instance = axios.create({ - baseURL: API_BASE_URL, - timeout: 30000, // 超时时间30秒 - headers: { - 'Content-Type': 'application/json', - }, -}); - -// 请求拦截器 -instance.interceptors.request.use( - async (config) => { - if (enableDebugLogs) { - const method = config.method?.toUpperCase() || 'UNKNOWN'; - const baseUrl = config.baseURL || ''; - const url = config.url || ''; - - console.log(`🚀 发送${method}请求: ${baseUrl}${url}`); - if (config.data) { - console.log('📦 请求数据:', JSON.stringify(config.data).substring(0, 500)); - } - if (config.params) { - console.log('🔍 请求参数:', config.params); - } - } - - // 从本地存储获取token - const token = await AsyncStorage.getItem('auth_token'); - if (token) { - config.headers.Authorization = `Bearer ${token}`; - } - - return config; - }, - (error) => { - console.error('❌ 请求发送失败:', error); - return Promise.reject(error); - } -); - -// 响应拦截器 -instance.interceptors.response.use( - (response) => { - if (enableDebugLogs) { - const method = response.config.method?.toUpperCase() || 'UNKNOWN'; - const url = response.config.url || 'unknown'; - console.log(`✅ ${method}请求成功: ${url}`); - console.log(`📊 状态码: ${response.status}`); - console.log('📦 响应数据:', JSON.stringify(response.data).substring(0, 500)); - } - return response; - }, - async (error) => { - if (enableDebugLogs) { - console.error('❌ 请求失败:', error.message); - - if (error.response) { - // 服务器返回了错误状态码 - const method = error.config?.method?.toUpperCase() || 'UNKNOWN'; - const url = error.config?.url || 'unknown'; - console.error(`🔥 ${method}请求错误: ${url}`); - console.error(`📊 状态码: ${error.response.status}`); - - try { - console.error('📦 错误响应:', JSON.stringify(error.response.data).substring(0, 500)); - } catch (e) { - console.error('📦 错误响应(非JSON):', String(error.response.data).substring(0, 500)); - } - - // 处理401未授权错误 - if (error.response.status === 401) { - await AsyncStorage.removeItem('auth_token'); - console.warn('⚠️ 用户未授权,已清除token'); - } - } else if (error.request) { - // 请求已发送但没有收到响应 - console.error('⏱️ 请求超时或无响应:', error.config?.url || 'unknown'); - } else { - // 设置请求时发生错误 - console.error('🔧 请求配置错误:', error.message); - } - } - - return Promise.reject(error); - } -); - -export default instance; \ No newline at end of file