| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600 |
- import axios from 'axios';
- import type { MinioUploadPolicy, OSSUploadPolicy } from '@d8d-appcontainer/types';
- import 'dayjs/locale/zh-cn';
- import type {
- User, FileLibrary, FileCategory, KnowInfo,
- AuthContextType, ThemeContextType, Attachment, ThemeSettings,
- SystemSetting, SystemSettingGroupData
- } from '../share/types.ts';
- // 定义API基础URL
- const API_BASE_URL = '/api';
- // 获取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}`;
- };
- // ===================
- // Auth API 定义部分
- // ===================
- // 定义API返回数据类型
- interface AuthLoginResponse {
- message: string;
- token: string;
- refreshToken?: string;
- user: User;
- }
- interface AuthResponse {
- message: string;
- [key: string]: any;
- }
- // 定义Auth API接口类型
- interface AuthAPIType {
- login: (username: string, password: string, latitude?: number, longitude?: number) => Promise<AuthLoginResponse>;
- register: (username: string, email: string, password: string) => Promise<AuthResponse>;
- logout: () => Promise<AuthResponse>;
- getCurrentUser: () => Promise<User>;
- updateUser: (userId: number, userData: Partial<User>) => Promise<User>;
- changePassword: (oldPassword: string, newPassword: string) => Promise<AuthResponse>;
- requestPasswordReset: (email: string) => Promise<AuthResponse>;
- resetPassword: (token: string, newPassword: string) => Promise<AuthResponse>;
- }
- // Auth相关API
- 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`, {
- username,
- password,
- latitude,
- longitude
- });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- // 注册API
- register: async (username: string, email: string, password: string) => {
- try {
- const response = await axios.post(`${API_BASE_URL}/auth/register`, { username, email, password });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- // 登出API
- logout: async () => {
- try {
- const response = await axios.post(`${API_BASE_URL}/auth/logout`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- // 获取当前用户信息
- getCurrentUser: async () => {
- try {
- const response = await axios.get(`${API_BASE_URL}/auth/me`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- // 更新用户信息
- updateUser: async (userId: number, userData: Partial<User>) => {
- try {
- const response = await axios.put(`${API_BASE_URL}/auth/users/${userId}`, userData);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- // 修改密码
- changePassword: async (oldPassword: string, newPassword: string) => {
- try {
- const response = await axios.post(`${API_BASE_URL}/auth/change-password`, { oldPassword, newPassword });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- // 请求重置密码
- requestPasswordReset: async (email: string) => {
- try {
- const response = await axios.post(`${API_BASE_URL}/auth/request-password-reset`, { email });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- // 重置密码
- resetPassword: async (token: string, newPassword: string) => {
- try {
- const response = await axios.post(`${API_BASE_URL}/auth/reset-password`, { token, newPassword });
- return response.data;
- } catch (error) {
- throw error;
- }
- }
- };
- // 为UserAPI添加的接口响应类型
- interface UsersResponse {
- data: User[];
- pagination: {
- total: number;
- current: number;
- pageSize: number;
- totalPages: number;
- };
- }
- interface UserResponse {
- data: User;
- message?: string;
- }
- interface UserCreateResponse {
- message: string;
- data: User;
- }
- interface UserUpdateResponse {
- message: string;
- data: User;
- }
- interface UserDeleteResponse {
- message: string;
- id: number;
- }
- // 用户管理API
- 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 });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- // 获取单个用户详情
- getUser: async (userId: number): Promise<UserResponse> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/users/${userId}`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- // 创建用户
- createUser: async (userData: Partial<User>): Promise<UserCreateResponse> => {
- try {
- const response = await axios.post(`${API_BASE_URL}/users`, userData);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- // 更新用户信息
- updateUser: async (userId: number, userData: Partial<User>): Promise<UserUpdateResponse> => {
- try {
- const response = await axios.put(`${API_BASE_URL}/users/${userId}`, userData);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
-
- // 删除用户
- deleteUser: async (userId: number): Promise<UserDeleteResponse> => {
- try {
- const response = await axios.delete(`${API_BASE_URL}/users/${userId}`);
- return response.data;
- } catch (error) {
- throw error;
- }
- }
- };
- // 定义文件相关接口类型
- interface FileUploadPolicyResponse {
- message: string;
- data: MinioUploadPolicy | OSSUploadPolicy;
- }
- interface FileListResponse {
- message: string;
- data: {
- list: FileLibrary[];
- pagination: {
- current: number;
- pageSize: number;
- total: number;
- };
- };
- }
- interface FileSaveResponse {
- message: string;
- data: FileLibrary;
- }
- interface FileInfoResponse {
- message: string;
- data: FileLibrary;
- }
- interface FileDeleteResponse {
- message: string;
- }
- interface FileCategoryListResponse {
- data: FileCategory[];
- total: number;
- page: number;
- pageSize: number;
- }
- interface FileCategoryCreateResponse {
- message: string;
- data: FileCategory;
- }
- interface FileCategoryUpdateResponse {
- message: string;
- data: FileCategory;
- }
- interface FileCategoryDeleteResponse {
- message: string;
- }
- // 文件API接口定义
- 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`, {
- params: { filename, prefix, maxSize }
- });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 保存文件信息
- saveFileInfo: async (fileData: Partial<FileLibrary>): Promise<FileSaveResponse> => {
- try {
- const response = await axios.post(`${API_BASE_URL}/upload/save`, fileData);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 获取文件列表
- getFileList: async (params?: {
- page?: number,
- pageSize?: number,
- category_id?: number,
- fileType?: string,
- keyword?: string
- }): Promise<FileListResponse> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/upload/list`, { params });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 获取单个文件信息
- getFileInfo: async (id: number): Promise<FileInfoResponse> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/upload/${id}`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 更新文件下载计数
- updateDownloadCount: async (id: number): Promise<FileDeleteResponse> => {
- try {
- const response = await axios.post(`${API_BASE_URL}/upload/${id}/download`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 删除文件
- deleteFile: async (id: number): Promise<FileDeleteResponse> => {
- try {
- const response = await axios.delete(`${API_BASE_URL}/upload/${id}`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 获取文件分类列表
- getCategories: async (params?: {
- page?: number,
- pageSize?: number,
- search?: string
- }): Promise<FileCategoryListResponse> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/file-categories`, { params });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 创建文件分类
- createCategory: async (data: Partial<FileCategory>): Promise<FileCategoryCreateResponse> => {
- try {
- const response = await axios.post(`${API_BASE_URL}/file-categories`, data);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 更新文件分类
- updateCategory: async (id: number, data: Partial<FileCategory>): Promise<FileCategoryUpdateResponse> => {
- try {
- const response = await axios.put(`${API_BASE_URL}/file-categories/${id}`, data);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 删除文件分类
- deleteCategory: async (id: number): Promise<FileCategoryDeleteResponse> => {
- try {
- const response = await axios.delete(`${API_BASE_URL}/file-categories/${id}`);
- return response.data;
- } catch (error) {
- throw error;
- }
- }
- };
- // Theme API 定义
- export const ThemeAPI = {
- // 获取主题设置
- getThemeSettings: async (): Promise<ThemeSettings> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/theme`);
- return response.data.data;
- } catch (error) {
- throw error;
- }
- },
- // 更新主题设置
- updateThemeSettings: async (themeData: Partial<ThemeSettings>): Promise<ThemeSettings> => {
- try {
- const response = await axios.put(`${API_BASE_URL}/theme`, themeData);
- return response.data.data;
- } catch (error) {
- throw error;
- }
- },
- // 重置主题设置
- resetThemeSettings: async (): Promise<ThemeSettings> => {
- try {
- const response = await axios.post(`${API_BASE_URL}/theme/reset`);
- return response.data.data;
- } catch (error) {
- throw error;
- }
- }
- };
- // 图表数据API接口类型
- interface ChartDataResponse<T> {
- message: string;
- data: T;
- }
- interface UserActivityData {
- date: string;
- count: number;
- }
- interface FileUploadsData {
- month: string;
- count: number;
- }
- interface FileTypesData {
- type: string;
- value: number;
- }
- interface DashboardOverviewData {
- userCount: number;
- fileCount: number;
- articleCount: number;
- todayLoginCount: number;
- }
- // 图表数据API
- export const ChartAPI = {
- // 获取用户活跃度数据
- getUserActivity: async (): Promise<ChartDataResponse<UserActivityData[]>> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/charts/user-activity`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 获取文件上传统计数据
- getFileUploads: async (): Promise<ChartDataResponse<FileUploadsData[]>> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/charts/file-uploads`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 获取文件类型分布数据
- getFileTypes: async (): Promise<ChartDataResponse<FileTypesData[]>> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/charts/file-types`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 获取仪表盘概览数据
- getDashboardOverview: async (): Promise<ChartDataResponse<DashboardOverviewData>> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/charts/dashboard-overview`);
- return response.data;
- } catch (error) {
- throw error;
- }
- }
- };
- // 地图相关API的接口类型定义
- export interface LoginLocationResponse {
- message: string;
- data: LoginLocation[];
- }
- export interface LoginLocationDetailResponse {
- message: string;
- data: LoginLocationDetail;
- }
- export interface LoginLocationUpdateResponse {
- message: string;
- data: LoginLocationDetail;
- }
- // 地图相关API
- export const MapAPI = {
- // 获取地图标记点数据
- getMarkers: async (params?: {
- startTime?: string;
- endTime?: string;
- userId?: number
- }): Promise<LoginLocationResponse> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/map/markers`, { params });
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 获取登录位置详情
- getLocationDetail: async (locationId: number): Promise<LoginLocationDetailResponse> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/map/location/${locationId}`);
- return response.data;
- } catch (error) {
- throw error;
- }
- },
- // 更新登录位置信息
- updateLocation: async (locationId: number, data: {
- longitude: number;
- latitude: number;
- location_name?: string;
- }): Promise<LoginLocationUpdateResponse> => {
- try {
- const response = await axios.put(`${API_BASE_URL}/map/location/${locationId}`, data);
- return response.data;
- } catch (error) {
- throw error;
- }
- }
- };
- // 系统设置API
- export const SystemAPI = {
- // 获取所有系统设置
- getSettings: async (): Promise<SystemSettingGroupData[]> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/settings`);
- return response.data.data;
- } catch (error) {
- throw error;
- }
- },
- // 获取指定分组的系统设置
- getSettingsByGroup: async (group: string): Promise<SystemSetting[]> => {
- try {
- const response = await axios.get(`${API_BASE_URL}/settings/group/${group}`);
- return response.data.data;
- } catch (error) {
- throw error;
- }
- },
- // 更新系统设置
- updateSettings: async (settings: Partial<SystemSetting>[]): Promise<SystemSetting[]> => {
- try {
- const response = await axios.put(`${API_BASE_URL}/settings`, settings);
- return response.data.data;
- } catch (error) {
- throw error;
- }
- },
- // 重置系统设置
- resetSettings: async (): Promise<SystemSetting[]> => {
- try {
- const response = await axios.post(`${API_BASE_URL}/settings/reset`);
- return response.data.data;
- } catch (error) {
- throw error;
- }
- }
- };
|