Pārlūkot izejas kodu

已从10个API模块文件中移除重复的API_BASE_URL定义
所有API调用现在统一使用client/admin/api/index.ts中的全局axios配置
保持原有功能不变的同时简化了代码结构

yourname 7 mēneši atpakaļ
vecāks
revīzija
e4f45ed952

+ 8 - 10
client/admin/api/auth.ts

@@ -1,8 +1,6 @@
 import axios from 'axios';
 import type { User } from '../../share/types.ts';
 
-const API_BASE_URL = '/api';
-
 interface AuthLoginResponse {
   message: string;
   token: string;
@@ -29,7 +27,7 @@ interface AuthAPIType {
 export const AuthAPI: AuthAPIType = {
   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,
@@ -43,7 +41,7 @@ export const AuthAPI: AuthAPIType = {
   
   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;
@@ -52,7 +50,7 @@ export const AuthAPI: AuthAPIType = {
   
   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;
@@ -61,7 +59,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;
@@ -70,7 +68,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;
@@ -79,7 +77,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;
@@ -88,7 +86,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;
@@ -97,7 +95,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/admin/api/charts.ts

@@ -1,7 +1,5 @@
 import axios from 'axios';
 
-const API_BASE_URL = '/api';
-
 interface ChartDataResponse<T> {
   message: string;
   data: T;
@@ -32,7 +30,7 @@ interface DashboardOverviewData {
 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;
@@ -41,7 +39,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;
@@ -50,7 +48,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;
@@ -59,7 +57,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;

+ 10 - 12
client/admin/api/files.ts

@@ -2,8 +2,6 @@ import axios from 'axios';
 import type { FileLibrary, FileCategory } from '../../share/types.ts';
 import type { MinioUploadPolicy, OSSUploadPolicy } from '@d8d-appcontainer/types';
 
-const API_BASE_URL = '/api';
-
 interface FileUploadPolicyResponse {
   message: string;
   data: MinioUploadPolicy | OSSUploadPolicy;
@@ -59,7 +57,7 @@ interface FileCategoryDeleteResponse {
 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;
@@ -70,7 +68,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;
@@ -85,7 +83,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;
@@ -94,7 +92,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;
@@ -103,7 +101,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;
@@ -112,7 +110,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;
@@ -125,7 +123,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;
@@ -134,7 +132,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;
@@ -143,7 +141,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;
@@ -152,7 +150,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;

+ 16 - 0
client/admin/api/index.ts

@@ -1,3 +1,19 @@
+import axios from 'axios';
+
+// 基础配置
+export const API_BASE_URL = '/api';
+// 全局axios配置
+axios.defaults.baseURL = API_BASE_URL;
+
+// 获取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}`;
+};
+
 export * from './auth.ts';
 export * from './users.ts';
 export * from './files.ts';

+ 5 - 8
client/admin/api/know_info.ts

@@ -32,9 +32,6 @@ interface KnowInfoDeleteResponse {
 }
 
 
-const API_BASE_URL = '/api';
-
-
 // 知识库API
 export const KnowInfoAPI = {
   // 获取知识库列表
@@ -46,7 +43,7 @@ export const KnowInfoAPI = {
     tags?: string;
   }): Promise<KnowInfoListResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/know-infos`, { params });
+      const response = await axios.get('/know-infos', { params });
       return response.data;
     } catch (error) {
       throw error;
@@ -56,7 +53,7 @@ export const KnowInfoAPI = {
   // 获取单个知识详情
   getKnowInfo: async (id: number): Promise<KnowInfoResponse> => {
     try {
-      const response = await axios.get(`${API_BASE_URL}/know-infos/${id}`);
+      const response = await axios.get(`/know-infos/${id}`);
       return response.data;
     } catch (error) {
       throw error;
@@ -66,7 +63,7 @@ export const KnowInfoAPI = {
   // 创建知识
   createKnowInfo: async (data: Partial<KnowInfo>): Promise<KnowInfoCreateResponse> => {
     try {
-      const response = await axios.post(`${API_BASE_URL}/know-infos`, data);
+      const response = await axios.post('/know-infos', data);
       return response.data;
     } catch (error) {
       throw error;
@@ -76,7 +73,7 @@ export const KnowInfoAPI = {
   // 更新知识
   updateKnowInfo: async (id: number, data: Partial<KnowInfo>): Promise<KnowInfoUpdateResponse> => {
     try {
-      const response = await axios.put(`${API_BASE_URL}/know-infos/${id}`, data);
+      const response = await axios.put(`/know-infos/${id}`, data);
       return response.data;
     } catch (error) {
       throw error;
@@ -86,7 +83,7 @@ export const KnowInfoAPI = {
   // 删除知识
   deleteKnowInfo: async (id: number): Promise<KnowInfoDeleteResponse> => {
     try {
-      const response = await axios.delete(`${API_BASE_URL}/know-infos/${id}`);
+      const response = await axios.delete(`/know-infos/${id}`);
       return response.data;
     } catch (error) {
       throw error;

+ 2 - 3
client/admin/api/maps.ts

@@ -1,10 +1,9 @@
 import axios from 'axios';
-import type { 
+import { API_BASE_URL } from './index.ts';
+import type {
  LoginLocation, LoginLocationDetail,
 } from '../../share/types.ts';
 
-const API_BASE_URL = '/api';
-
 
 // 地图相关API的接口类型定义
 export interface LoginLocationResponse {

+ 5 - 7
client/admin/api/messages.ts

@@ -1,8 +1,6 @@
 import axios from 'axios';
 import type { UserMessage, Message } from '../../share/types.ts';
 
-const API_BASE_URL = '/api';
-
 interface MessagesResponse {
   data: UserMessage[];
   pagination: {
@@ -31,7 +29,7 @@ export const MessageAPI = {
     search?: string
   }): 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 = {
     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;
@@ -54,7 +52,7 @@ export const MessageAPI = {
 
   getUnreadCount: async (): Promise<MessageCountResponse> => {
     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;
@@ -63,7 +61,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;
@@ -72,7 +70,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;

+ 6 - 8
client/admin/api/sys.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';
-
 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;
@@ -30,7 +28,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;
@@ -40,7 +38,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;

+ 3 - 5
client/admin/api/theme.ts

@@ -1,8 +1,6 @@
 import axios from 'axios';
 import type { ThemeSettings } from '../../share/types.ts';
 
-const API_BASE_URL = '/api';
-
 export interface ThemeSettingsResponse {
   message: string;
   data: ThemeSettings;
@@ -11,7 +9,7 @@ export interface ThemeSettingsResponse {
 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;
@@ -29,7 +27,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;

+ 5 - 7
client/admin/api/users.ts

@@ -1,8 +1,6 @@
 import axios from 'axios';
 import type { User } from '../../share/types.ts';
 
-const API_BASE_URL = '/api';
-
 interface UsersResponse {
   data: User[];
   pagination: {
@@ -36,7 +34,7 @@ interface UserDeleteResponse {
 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;
@@ -45,7 +43,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;
@@ -54,7 +52,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;
@@ -63,7 +61,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;
@@ -72,7 +70,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;