// API 服务层 import api from '@/utils/api' import axios from 'axios' import type { ApiResponse, FileInfo, ConfigState, AdminUser, FileUploadResponse, TextSendResponse, DashboardData, FileListResponse, FileEditForm } from '@/types' import type { UploadProgress, PresignInitRequest, PresignInitResponse, PresignConfirmRequest, PresignUploadResult, PresignStatusResponse } from '@/types' // 系统配置服务 export class ConfigService { static async getConfig(): Promise> { return api.get('/admin/config/get') } static async getUserConfig(): Promise> { return api.post('/') } static async updateConfig(config: Partial): Promise { return api.patch('/admin/config/update', config) } } // 文件服务 export class FileService { static async uploadFile( file: File, onProgress?: (progress: UploadProgress) => void ): Promise> { const formData = new FormData() formData.append('file', file) return api.post('/share/file/', formData, { headers: { 'Content-Type': 'multipart/form-data' }, onUploadProgress: (progressEvent) => { if (onProgress && progressEvent.total) { const progress: UploadProgress = { loaded: progressEvent.loaded, total: progressEvent.total, percentage: Math.round((progressEvent.loaded * 100) / progressEvent.total) } onProgress(progress) } } }) } static async uploadText(text: string): Promise> { return api.post('/share/text/', { content: text }) } static async getFile(code: string): Promise> { return api.get(`/file/${code}`) } static async downloadFile(code: string): Promise { const response = await api.get(`/download/${code}`, { responseType: 'blob' }) return response.data } static async deleteFile(fileId: string): Promise { return api.post('/admin/file/delete', { id: fileId }) } static async getFileList(page = 1, limit = 10): Promise> { return api.get('/admin/file/list', { params: { page, size: limit } }) } // 文件管理相关方法 static async getAdminFileList(params: { page: number; size: number; keyword?: string }): Promise> { return api.get('/admin/file/list', { params }) } static async updateFile(data: FileEditForm): Promise { return api.patch('/admin/file/update', data) } static async deleteAdminFile(id: number): Promise { return api.delete('/admin/file/delete', { data: { id } }) } static async downloadAdminFile(id: number): Promise<{ data: Blob; headers: Record }> { return api.get('/admin/file/download', { params: { id }, responseType: 'blob' }) } } // 认证服务 export class AuthService { static async login(password: string): Promise> { return api.post('/admin/login', { password }) } static async logout(): Promise { return api.post('/admin/logout') } static async verifyToken(): Promise> { return api.get('/admin/verify') } } // 统计服务 export class StatsService { static async getDashboardStats(): Promise> { return api.get('/admin/dashboard') } static async getDashboard(): Promise> { return api.get('/admin/dashboard') } } // 预签名上传服务 export class PresignUploadService { /** * 初始化上传会话 */ static async initUpload(request: PresignInitRequest): Promise> { return api.post('/presign/upload/init', request) } /** * 代理模式上传 */ static async proxyUpload( uploadId: string, file: File, onProgress?: (progress: UploadProgress) => void ): Promise> { const formData = new FormData() formData.append('file', file) return api.put(`/presign/upload/proxy/${uploadId}`, formData, { headers: { 'Content-Type': 'multipart/form-data' }, onUploadProgress: (progressEvent) => { if (onProgress && progressEvent.total) { const progress: UploadProgress = { loaded: progressEvent.loaded, total: progressEvent.total, percentage: Math.round((progressEvent.loaded * 100) / progressEvent.total) } onProgress(progress) } } }) } /** * 确认直传上传 */ static async confirmUpload( uploadId: string, request?: PresignConfirmRequest ): Promise> { return api.post(`/presign/upload/confirm/${uploadId}`, request || {}) } /** * 查询上传状态 */ static async getUploadStatus(uploadId: string): Promise> { return api.get(`/presign/upload/status/${uploadId}`) } /** * 取消上传 */ static async cancelUpload(uploadId: string): Promise> { return api.delete(`/presign/upload/${uploadId}`) } /** * S3 直传(不经过后端) */ static async directUploadToS3( uploadUrl: string, file: File, onProgress?: (progress: UploadProgress) => void ): Promise { try { await axios.put(uploadUrl, file, { headers: { 'Content-Type': 'application/octet-stream' }, onUploadProgress: (progressEvent) => { if (onProgress && progressEvent.total) { const progress: UploadProgress = { loaded: progressEvent.loaded, total: progressEvent.total, percentage: Math.round((progressEvent.loaded * 100) / progressEvent.total) } onProgress(progress) } } }) return true } catch { return false } } } // 导出所有服务 export const services = { config: ConfigService, file: FileService, auth: AuthService, stats: StatsService, presignUpload: PresignUploadService } export default services