瀏覽代碼

修改范围覆盖mobile/api目录下所有API文件(共9个,包括auth.ts)
统一使用index.ts中的全局axios配置
移除了所有重复的API_BASE_URL定义
简化了所有API调用路径格式
提高了代码一致性和可维护性
确保所有API功能保持正常

yourname 6 月之前
父節點
當前提交
ba64ad96ed

+ 8 - 11
client/mobile/api/auth.ts

@@ -1,9 +1,6 @@
 import axios from 'axios';
 import type { User } from '../../share/types.ts';
 
-// 从原api.ts导入基础配置
-const API_BASE_URL = '/api';
-
 // 定义API返回数据类型
 interface AuthLoginResponse {
   message: string;
@@ -34,7 +31,7 @@ export const AuthAPI: AuthAPIType = {
   // 登录API
   login: async (username: string, password: string, latitude?: number, longitude?: number) => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/auth/login`, {
+      const response = await axios.post('/auth/login', {
         username,
         password,
         latitude,
@@ -49,7 +46,7 @@ export const AuthAPI: AuthAPIType = {
   // 注册API
   register: async (username: string, email: string, password: string) => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/auth/register`, { username, email, password });
+      const response = await axios.post('/auth/register', { username, email, password });
       return response.data;
     } catch (error) {
       throw error;
@@ -59,7 +56,7 @@ export const AuthAPI: AuthAPIType = {
   // 登出API
   logout: async () => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/auth/logout`);
+      const response = await axios.post('/auth/logout');
       return response.data;
     } catch (error) {
       throw error;
@@ -69,7 +66,7 @@ export const AuthAPI: AuthAPIType = {
   // 获取当前用户信息
   getCurrentUser: async () => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/auth/me`);
+      const response = await axios.get('/auth/me');
       return response.data;
     } catch (error) {
       throw error;
@@ -79,7 +76,7 @@ export const AuthAPI: AuthAPIType = {
   // 更新用户信息
   updateUser: async (userId: number, userData: Partial<User>) => {
     try {
-      const response = await axios.put(`${API_BASE_URL}/auth/users/${userId}`, userData);
+      const response = await axios.put(`/auth/users/${userId}`, userData);
       return response.data;
     } catch (error) {
       throw error;
@@ -89,7 +86,7 @@ export const AuthAPI: AuthAPIType = {
   // 修改密码
   changePassword: async (oldPassword: string, newPassword: string) => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/auth/change-password`, { oldPassword, newPassword });
+      const response = await axios.post('/auth/change-password', { oldPassword, newPassword });
       return response.data;
     } catch (error) {
       throw error;
@@ -99,7 +96,7 @@ export const AuthAPI: AuthAPIType = {
   // 请求重置密码
   requestPasswordReset: async (email: string) => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/auth/request-password-reset`, { email });
+      const response = await axios.post('/auth/request-password-reset', { email });
       return response.data;
     } catch (error) {
       throw error;
@@ -109,7 +106,7 @@ export const AuthAPI: AuthAPIType = {
   // 重置密码
   resetPassword: async (token: string, newPassword: string) => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/auth/reset-password`, { token, newPassword });
+      const response = await axios.post('/auth/reset-password', { token, newPassword });
       return response.data;
     } catch (error) {
       throw error;

+ 4 - 6
client/mobile/api/chart.ts

@@ -1,7 +1,5 @@
 import axios from 'axios';
 
-const API_BASE_URL = '/api';
-
 // 图表数据API接口类型
 interface ChartDataResponse<T> {
   message: string;
@@ -35,7 +33,7 @@ export const ChartAPI = {
   // 获取用户活跃度数据
   getUserActivity: async (): Promise<ChartDataResponse<UserActivityData[]>> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/charts/user-activity`);
+      const response = await axios.get('/charts/user-activity');
       return response.data;
     } catch (error) {
       throw error;
@@ -45,7 +43,7 @@ export const ChartAPI = {
   // 获取文件上传统计数据
   getFileUploads: async (): Promise<ChartDataResponse<FileUploadsData[]>> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/charts/file-uploads`);
+      const response = await axios.get('/charts/file-uploads');
       return response.data;
     } catch (error) {
       throw error;
@@ -55,7 +53,7 @@ export const ChartAPI = {
   // 获取文件类型分布数据
   getFileTypes: async (): Promise<ChartDataResponse<FileTypesData[]>> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/charts/file-types`);
+      const response = await axios.get('/charts/file-types');
       return response.data;
     } catch (error) {
       throw error;
@@ -65,7 +63,7 @@ export const ChartAPI = {
   // 获取仪表盘概览数据
   getDashboardOverview: async (): Promise<ChartDataResponse<DashboardOverviewData>> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/charts/dashboard-overview`);
+      const response = await axios.get('/charts/dashboard-overview');
       return response.data;
     } catch (error) {
       throw error;

+ 11 - 13
client/mobile/api/file.ts

@@ -1,11 +1,9 @@
 import axios from 'axios';
 import type { MinioUploadPolicy, OSSUploadPolicy } from '@d8d-appcontainer/types';
-import type { 
+import type {
   FileLibrary, FileCategory
 } from '../../share/types.ts';
 
-const API_BASE_URL = '/api';
-
 interface FileUploadPolicyResponse {
   message: string;
   data: MinioUploadPolicy | OSSUploadPolicy;
@@ -64,7 +62,7 @@ export const FileAPI = {
   // 获取文件上传策略
   getUploadPolicy: async (filename: string, prefix: string = 'uploads/', maxSize: number = 10 * 1024 * 1024): Promise<FileUploadPolicyResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/upload/policy`, { 
+      const response = await axios.get('/upload/policy', {
         params: { filename, prefix, maxSize } 
       });
       return response.data;
@@ -76,7 +74,7 @@ export const FileAPI = {
   // 保存文件信息
   saveFileInfo: async (fileData: Partial<FileLibrary>): Promise<FileSaveResponse> => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/upload/save`, fileData);
+      const response = await axios.post('/upload/save', fileData);
       return response.data;
     } catch (error) {
       throw error;
@@ -92,7 +90,7 @@ export const FileAPI = {
     keyword?: string
   }): Promise<FileListResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/upload/list`, { params });
+      const response = await axios.get('/upload/list', { params });
       return response.data;
     } catch (error) {
       throw error;
@@ -102,7 +100,7 @@ export const FileAPI = {
   // 获取单个文件信息
   getFileInfo: async (id: number): Promise<FileInfoResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/upload/${id}`);
+      const response = await axios.get(`/upload/${id}`);
       return response.data;
     } catch (error) {
       throw error;
@@ -112,7 +110,7 @@ export const FileAPI = {
   // 更新文件下载计数
   updateDownloadCount: async (id: number): Promise<FileDeleteResponse> => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/upload/${id}/download`);
+      const response = await axios.post(`/upload/${id}/download`);
       return response.data;
     } catch (error) {
       throw error;
@@ -122,7 +120,7 @@ export const FileAPI = {
   // 删除文件
   deleteFile: async (id: number): Promise<FileDeleteResponse> => {
     try {
-      const response = await axios.delete(`${API_BASE_URL}/upload/${id}`);
+      const response = await axios.delete(`/upload/${id}`);
       return response.data;
     } catch (error) {
       throw error;
@@ -136,7 +134,7 @@ export const FileAPI = {
     search?: string
   }): Promise<FileCategoryListResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/file-categories`, { params });
+      const response = await axios.get('/file-categories', { params });
       return response.data;
     } catch (error) {
       throw error;
@@ -146,7 +144,7 @@ export const FileAPI = {
   // 创建文件分类
   createCategory: async (data: Partial<FileCategory>): Promise<FileCategoryCreateResponse> => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/file-categories`, data);
+      const response = await axios.post('/file-categories', data);
       return response.data;
     } catch (error) {
       throw error;
@@ -156,7 +154,7 @@ export const FileAPI = {
   // 更新文件分类
   updateCategory: async (id: number, data: Partial<FileCategory>): Promise<FileCategoryUpdateResponse> => {
     try {
-      const response = await axios.put(`${API_BASE_URL}/file-categories/${id}`, data);
+      const response = await axios.put(`/file-categories/${id}`, data);
       return response.data;
     } catch (error) {
       throw error;
@@ -166,7 +164,7 @@ export const FileAPI = {
   // 删除文件分类
   deleteCategory: async (id: number): Promise<FileCategoryDeleteResponse> => {
     try {
-      const response = await axios.delete(`${API_BASE_URL}/file-categories/${id}`);
+      const response = await axios.delete(`/file-categories/${id}`);
       return response.data;
     } catch (error) {
       throw error;

+ 4 - 6
client/mobile/api/home.ts

@@ -1,10 +1,8 @@
 import axios from 'axios';
-import type { 
+import type {
   KnowInfo
 } from '../../share/types.ts';
 
-const API_BASE_URL = '/api';
-
 // 首页数据相关类型定义
 interface HomeBannersResponse {
   message: string;
@@ -43,7 +41,7 @@ export const HomeAPI = {
   // 获取轮播图
   getBanners: async (): Promise<HomeBannersResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/home/banners`);
+      const response = await axios.get('/home/banners');
       return response.data;
     } catch (error) {
       throw error;
@@ -57,7 +55,7 @@ export const HomeAPI = {
     category?: string
   }): Promise<HomeNewsResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/home/news`, { params });
+      const response = await axios.get('/home/news', { params });
       return response.data;
     } catch (error) {
       throw error;
@@ -70,7 +68,7 @@ export const HomeAPI = {
     pageSize?: number
   }): Promise<HomeNoticesResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/home/notices`, { params });
+      const response = await axios.get('/home/notices', { params });
       return response.data;
     } catch (error) {
       throw error;

+ 17 - 1
client/mobile/api/index.ts

@@ -1,3 +1,10 @@
+import axios from 'axios';
+
+// 基础配置
+const API_BASE_URL = '/api';
+// 全局axios配置
+axios.defaults.baseURL = API_BASE_URL;
+
 export * from './auth.ts';
 export * from './user.ts';
 export * from './file.ts';
@@ -6,4 +13,13 @@ export * from './chart.ts';
 export * from './home.ts';
 export * from './map.ts';
 export * from './system.ts';
-export * from './message.ts';
+export * from './message.ts';
+
+// 获取OSS完整URL
+export const getOssUrl = (path: string): string => {
+  // 获取全局配置中的OSS_HOST,如果不存在使用默认值
+  const ossHost = (window.CONFIG?.OSS_BASE_URL) || '';
+  // 确保path不以/开头
+  const ossPath = path.startsWith('/') ? path.substring(1) : path;
+  return `${ossHost}/${ossPath}`;
+};

+ 4 - 6
client/mobile/api/map.ts

@@ -1,10 +1,8 @@
 import axios from 'axios';
-import type { 
+import type {
   LoginLocation, LoginLocationDetail,
 } from '../../share/types.ts';
 
-const API_BASE_URL = '/api';
-
 
 // 地图相关API的接口类型定义
 export interface LoginLocationResponse {
@@ -31,7 +29,7 @@ export const MapAPI = {
     userId?: number 
   }): Promise<LoginLocationResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/map/markers`, { params });
+      const response = await axios.get('/map/markers', { params });
       return response.data;
     } catch (error) {
       throw error;
@@ -41,7 +39,7 @@ export const MapAPI = {
   // 获取登录位置详情
   getLocationDetail: async (locationId: number): Promise<LoginLocationDetailResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/map/location/${locationId}`);
+      const response = await axios.get(`/map/location/${locationId}`);
       return response.data;
     } catch (error) {
       throw error;
@@ -55,7 +53,7 @@ export const MapAPI = {
     location_name?: string; 
   }): Promise<LoginLocationUpdateResponse> => {
     try {
-      const response = await axios.put(`${API_BASE_URL}/map/location/${locationId}`, data);
+      const response = await axios.put(`/map/location/${locationId}`, data);
       return response.data;
     } catch (error) {
       throw error;

+ 7 - 9
client/mobile/api/message.ts

@@ -1,10 +1,8 @@
 import axios from 'axios';
-import type { 
+import type {
   MessageType, MessageStatus, UserMessage
 } from '../../share/types.ts';
 
-const API_BASE_URL = '/api';
-
 // 消息API响应类型
 export interface MessageResponse {
   message: string;
@@ -35,7 +33,7 @@ export const MessageAPI = {
     status?: MessageStatus
   }): Promise<MessagesResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/messages`, { params });
+      const response = await axios.get('/messages', { params });
       return response.data;
     } catch (error) {
       throw error;
@@ -45,7 +43,7 @@ export const MessageAPI = {
   // 获取消息详情
   getMessage: async (id: number): Promise<MessageResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/messages/${id}`);
+      const response = await axios.get(`/messages/${id}`);
       return response.data;
     } catch (error) {
       throw error;
@@ -60,7 +58,7 @@ export const MessageAPI = {
     receiver_ids: number[]
   }): Promise<MessageResponse> => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/messages`, data);
+      const response = await axios.post('/messages', data);
       return response.data;
     } catch (error) {
       throw error;
@@ -70,7 +68,7 @@ export const MessageAPI = {
   // 删除消息(软删除)
   deleteMessage: async (id: number): Promise<MessageResponse> => {
     try {
-      const response = await axios.delete(`${API_BASE_URL}/messages/${id}`);
+      const response = await axios.delete(`/messages/${id}`);
       return response.data;
     } catch (error) {
       throw error;
@@ -80,7 +78,7 @@ export const MessageAPI = {
   // 获取未读消息数量
   getUnreadCount: async (): Promise<UnreadCountResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/messages/count/unread`);
+      const response = await axios.get('/messages/count/unread');
       return response.data;
     } catch (error) {
       throw error;
@@ -90,7 +88,7 @@ export const MessageAPI = {
   // 标记消息为已读
   markAsRead: async (id: number): Promise<MessageResponse> => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/messages/${id}/read`);
+      const response = await axios.post(`/messages/${id}/read`);
       return response.data;
     } catch (error) {
       throw error;

+ 6 - 8
client/mobile/api/system.ts

@@ -1,16 +1,14 @@
 import axios from 'axios';
-import type { 
-  SystemSetting, SystemSettingGroupData, 
+import type {
+  SystemSetting, SystemSettingGroupData,
 } from '../../share/types.ts';
 
-const API_BASE_URL = '/api';
-
 // 系统设置API
 export const SystemAPI = {
   // 获取所有系统设置
   getSettings: async (): Promise<SystemSettingGroupData[]> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/settings`);
+      const response = await axios.get('/settings');
       return response.data.data;
     } catch (error) {
       throw error;
@@ -20,7 +18,7 @@ export const SystemAPI = {
   // 获取指定分组的系统设置
   getSettingsByGroup: async (group: string): Promise<SystemSetting[]> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/settings/group/${group}`);
+      const response = await axios.get(`/settings/group/${group}`);
       return response.data.data;
     } catch (error) {
       throw error;
@@ -31,7 +29,7 @@ export const SystemAPI = {
   // 更新系统设置
   updateSettings: async (settings: Partial<SystemSetting>[]): Promise<SystemSetting[]> => {
     try {
-      const response = await axios.put(`${API_BASE_URL}/settings`, settings);
+      const response = await axios.put('/settings', settings);
       return response.data.data;
     } catch (error) {
       throw error;
@@ -41,7 +39,7 @@ export const SystemAPI = {
   // 重置系统设置
   resetSettings: async (): Promise<SystemSetting[]> => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/settings/reset`);
+      const response = await axios.post('/settings/reset');
       return response.data.data;
     } catch (error) {
       throw error;

+ 4 - 6
client/mobile/api/theme.ts

@@ -1,16 +1,14 @@
 import axios from 'axios';
-import type { 
+import type {
   ThemeSettings
 } from '../../share/types.ts';
 
-const API_BASE_URL = '/api';
-
 // Theme API 定义
 export const ThemeAPI = {
   // 获取主题设置
   getThemeSettings: async (): Promise<ThemeSettings> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/theme`);
+      const response = await axios.get('/theme');
       return response.data.data;
     } catch (error) {
       throw error;
@@ -20,7 +18,7 @@ export const ThemeAPI = {
   // 更新主题设置
   updateThemeSettings: async (themeData: Partial<ThemeSettings>): Promise<ThemeSettings> => {
     try {
-      const response = await axios.put(`${API_BASE_URL}/theme`, themeData);
+      const response = await axios.put('/theme', themeData);
       return response.data.data;
     } catch (error) {
       throw error;
@@ -30,7 +28,7 @@ export const ThemeAPI = {
   // 重置主题设置
   resetThemeSettings: async (): Promise<ThemeSettings> => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/theme/reset`);
+      const response = await axios.post('/theme/reset');
       return response.data.data;
     } catch (error) {
       throw error;

+ 7 - 9
client/mobile/api/user.ts

@@ -1,8 +1,6 @@
 import axios from 'axios';
 import type { User } from '../../share/types.ts';
 
-const API_BASE_URL = '/api';
-
 // 为UserAPI添加的接口响应类型
 interface UsersResponse {
   data: User[];
@@ -39,7 +37,7 @@ export const UserAPI = {
   // 获取用户列表
   getUsers: async (params?: { page?: number, limit?: number, search?: string }): Promise<UsersResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/users`, { params });
+      const response = await axios.get('/users', { params });
       return response.data;
     } catch (error) {
       throw error;
@@ -49,7 +47,7 @@ export const UserAPI = {
   // 获取单个用户详情
   getUser: async (userId: number): Promise<UserResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/users/${userId}`);
+      const response = await axios.get(`/users/${userId}`);
       return response.data;
     } catch (error) {
       throw error;
@@ -59,7 +57,7 @@ export const UserAPI = {
   // 创建用户
   createUser: async (userData: Partial<User>): Promise<UserCreateResponse> => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/users`, userData);
+      const response = await axios.post('/users', userData);
       return response.data;
     } catch (error) {
       throw error;
@@ -69,7 +67,7 @@ export const UserAPI = {
   // 更新用户信息
   updateUser: async (userId: number, userData: Partial<User>): Promise<UserUpdateResponse> => {
     try {
-      const response = await axios.put(`${API_BASE_URL}/users/${userId}`, userData);
+      const response = await axios.put(`/users/${userId}`, userData);
       return response.data;
     } catch (error) {
       throw error;
@@ -79,7 +77,7 @@ export const UserAPI = {
   // 删除用户
   deleteUser: async (userId: number): Promise<UserDeleteResponse> => {
     try {
-      const response = await axios.delete(`${API_BASE_URL}/users/${userId}`);
+      const response = await axios.delete(`/users/${userId}`);
       return response.data;
     } catch (error) {
       throw error;
@@ -89,7 +87,7 @@ export const UserAPI = {
   // 获取当前用户信息
   getCurrentUser: async (): Promise<UserResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/users/me/profile`);
+      const response = await axios.get('/users/me/profile');
       return response.data;
     } catch (error) {
       throw error;
@@ -99,7 +97,7 @@ export const UserAPI = {
   // 更新当前用户信息
   updateCurrentUser: async (userData: Partial<User>): Promise<UserUpdateResponse> => {
     try {
-      const response = await axios.put(`${API_BASE_URL}/users/me/profile`, userData);
+      const response = await axios.put('/users/me/profile', userData);
       return response.data;
     } catch (error) {
       throw error;