From ba64ad96edd7ce01e0b56018c8521fe1c9066bfd Mon Sep 17 00:00:00 2001 From: yourname Date: Tue, 13 May 2025 09:31:30 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=8C=83=E5=9B=B4=E8=A6=86?= =?UTF-8?q?=E7=9B=96mobile/api=E7=9B=AE=E5=BD=95=E4=B8=8B=E6=89=80?= =?UTF-8?q?=E6=9C=89API=E6=96=87=E4=BB=B6(=E5=85=B19=E4=B8=AA=EF=BC=8C?= =?UTF-8?q?=E5=8C=85=E6=8B=ACauth.ts)=20=E7=BB=9F=E4=B8=80=E4=BD=BF?= =?UTF-8?q?=E7=94=A8index.ts=E4=B8=AD=E7=9A=84=E5=85=A8=E5=B1=80axios?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=20=E7=A7=BB=E9=99=A4=E4=BA=86=E6=89=80?= =?UTF-8?q?=E6=9C=89=E9=87=8D=E5=A4=8D=E7=9A=84API=5FBASE=5FURL=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=20=E7=AE=80=E5=8C=96=E4=BA=86=E6=89=80=E6=9C=89API?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E8=B7=AF=E5=BE=84=E6=A0=BC=E5=BC=8F=20?= =?UTF-8?q?=E6=8F=90=E9=AB=98=E4=BA=86=E4=BB=A3=E7=A0=81=E4=B8=80=E8=87=B4?= =?UTF-8?q?=E6=80=A7=E5=92=8C=E5=8F=AF=E7=BB=B4=E6=8A=A4=E6=80=A7=20?= =?UTF-8?q?=E7=A1=AE=E4=BF=9D=E6=89=80=E6=9C=89API=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E4=BF=9D=E6=8C=81=E6=AD=A3=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/mobile/api/auth.ts | 19 ++++++++----------- client/mobile/api/chart.ts | 10 ++++------ client/mobile/api/file.ts | 24 +++++++++++------------- client/mobile/api/home.ts | 10 ++++------ client/mobile/api/index.ts | 18 +++++++++++++++++- client/mobile/api/map.ts | 10 ++++------ client/mobile/api/message.ts | 16 +++++++--------- client/mobile/api/system.ts | 14 ++++++-------- client/mobile/api/theme.ts | 10 ++++------ client/mobile/api/user.ts | 16 +++++++--------- 10 files changed, 72 insertions(+), 75 deletions(-) diff --git a/client/mobile/api/auth.ts b/client/mobile/api/auth.ts index 8f1f82d..133b0cf 100644 --- a/client/mobile/api/auth.ts +++ b/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) => { 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; diff --git a/client/mobile/api/chart.ts b/client/mobile/api/chart.ts index f40212e..3dacc16 100644 --- a/client/mobile/api/chart.ts +++ b/client/mobile/api/chart.ts @@ -1,7 +1,5 @@ import axios from 'axios'; -const API_BASE_URL = '/api'; - // 图表数据API接口类型 interface ChartDataResponse { message: string; @@ -35,7 +33,7 @@ export const ChartAPI = { // 获取用户活跃度数据 getUserActivity: async (): Promise> => { 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> => { 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> => { 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> => { 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; diff --git a/client/mobile/api/file.ts b/client/mobile/api/file.ts index cb66a20..8f57603 100644 --- a/client/mobile/api/file.ts +++ b/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 => { 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): Promise => { 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 => { 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 => { 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 => { 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 => { 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 => { 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): Promise => { 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): Promise => { 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 => { 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; diff --git a/client/mobile/api/home.ts b/client/mobile/api/home.ts index a144f10..cb9f183 100644 --- a/client/mobile/api/home.ts +++ b/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 => { 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 => { 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 => { 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; diff --git a/client/mobile/api/index.ts b/client/mobile/api/index.ts index 6f357bb..ede55d8 100644 --- a/client/mobile/api/index.ts +++ b/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'; \ No newline at end of file +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}`; +}; \ No newline at end of file diff --git a/client/mobile/api/map.ts b/client/mobile/api/map.ts index 7092d29..4bbd731 100644 --- a/client/mobile/api/map.ts +++ b/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 => { 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 => { 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 => { 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; diff --git a/client/mobile/api/message.ts b/client/mobile/api/message.ts index c3e7bd7..5dec986 100644 --- a/client/mobile/api/message.ts +++ b/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 => { 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 => { 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 => { 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 => { 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 => { 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 => { 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; diff --git a/client/mobile/api/system.ts b/client/mobile/api/system.ts index 9efab9e..0ef9328 100644 --- a/client/mobile/api/system.ts +++ b/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 => { 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 => { 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[]): Promise => { 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 => { 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; diff --git a/client/mobile/api/theme.ts b/client/mobile/api/theme.ts index 27cd7cf..4488833 100644 --- a/client/mobile/api/theme.ts +++ b/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 => { 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): Promise => { 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 => { 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; diff --git a/client/mobile/api/user.ts b/client/mobile/api/user.ts index 8f1aed5..09b2dce 100644 --- a/client/mobile/api/user.ts +++ b/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 => { 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 => { 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): Promise => { 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): Promise => { 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 => { 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 => { 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): Promise => { 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;