import service from './request' import { ResponseData } from './request' // 上传响应数据 export interface UploadResponse { url: string // 文件访问地址 fileName: string // 文件名 fileSize: number // 文件大小(字节) fileType: string // 文件类型 } /** * 上传文件到阿里云 OSS * @param file 文件对象 * @param onProgress 上传进度回调 * @returns Promise> */ export function uploadFile( file: File, onProgress?: (progressEvent: any) => void ): Promise> { const formData = new FormData() formData.append('file', file) return service.post('/oss/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' }, onUploadProgress: onProgress }) } /** * 批量上传文件 * @param files 文件数组 * @param onProgress 上传进度回调 * @returns Promise> */ export function uploadFiles( files: File[], onProgress?: (progressEvent: any) => void ): Promise> { const formData = new FormData() files.forEach((file) => { formData.append('files', file) }) return service.post('/oss/upload/batch', formData, { headers: { 'Content-Type': 'multipart/form-data' }, onUploadProgress: onProgress }) } /** * 上传图片(带图片压缩和格式验证) * @param file 图片文件 * @param options 配置选项 * @returns Promise> */ export interface UploadImageOptions { maxSize?: number // 最大文件大小(MB),默认 5MB allowedTypes?: string[] // 允许的文件类型,默认 ['image/jpeg', 'image/png', 'image/gif', 'image/webp'] onProgress?: (progressEvent: any) => void } export function uploadImage( file: File, options: UploadImageOptions = {} ): Promise> { const { maxSize = 5, allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'], onProgress } = options // 验证文件类型 if (!allowedTypes.includes(file.type)) { return Promise.reject(new Error(`只支持上传 ${allowedTypes.join(', ')} 格式的图片`)) } // 验证文件大小 const maxSizeBytes = maxSize * 1024 * 1024 if (file.size > maxSizeBytes) { return Promise.reject(new Error(`图片大小不能超过 ${maxSize}MB`)) } return uploadFile(file, onProgress) } /** * 上传文档(Word、Excel、PDF 等) * @param file 文档文件 * @param options 配置选项 * @returns Promise> */ export interface UploadDocumentOptions { maxSize?: number // 最大文件大小(MB),默认 10MB allowedTypes?: string[] // 允许的文件类型 onProgress?: (progressEvent: any) => void } export function uploadDocument( file: File, options: UploadDocumentOptions = {} ): Promise> { const { maxSize = 10, allowedTypes = [ 'application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation' ], onProgress } = options // 验证文件类型 if (!allowedTypes.includes(file.type)) { return Promise.reject(new Error('不支持的文档格式')) } // 验证文件大小 const maxSizeBytes = maxSize * 1024 * 1024 if (file.size > maxSizeBytes) { return Promise.reject(new Error(`文档大小不能超过 ${maxSize}MB`)) } return uploadFile(file, onProgress) } /** * 删除文件 * @param url 文件地址 * @returns Promise> */ export function deleteFile(url: string): Promise> { return service.post('/oss/delete', { url }) } /** * 获取文件临时访问地址(用于私有文件) * @param url 文件地址 * @param expires 过期时间(秒),默认 3600 * @returns Promise> */ export function getFileUrl(url: string, expires = 3600): Promise> { return service.post('/oss/getUrl', { url, expires }) }