import axios from 'axios'; import type { MinioUploadPolicy, OSSUploadPolicy } from '@d8d-appcontainer/types'; import type { FileLibrary, FileCategory } from '../../share/types.ts'; const API_BASE_URL = '/api'; 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 => { 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): Promise => { 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 => { try { const response = await axios.get(`${API_BASE_URL}/upload/list`, { params }); return response.data; } catch (error) { throw error; } }, // 获取单个文件信息 getFileInfo: async (id: number): Promise => { try { const response = await axios.get(`${API_BASE_URL}/upload/${id}`); return response.data; } catch (error) { throw error; } }, // 更新文件下载计数 updateDownloadCount: async (id: number): Promise => { try { const response = await axios.post(`${API_BASE_URL}/upload/${id}/download`); return response.data; } catch (error) { throw error; } }, // 删除文件 deleteFile: async (id: number): Promise => { 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 => { try { const response = await axios.get(`${API_BASE_URL}/file-categories`, { params }); return response.data; } catch (error) { throw error; } }, // 创建文件分类 createCategory: async (data: Partial): Promise => { 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): Promise => { 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 => { try { const response = await axios.delete(`${API_BASE_URL}/file-categories/${id}`); return response.data; } catch (error) { throw error; } } };