/** * API 服务基类 * 提供统一的 HTTP 请求方法 */ import request from '@/utils/http' import type { BaseResponse, PaginationResponse, ListResponse } from '@/types/api' export class BaseService { /** * GET 请求 * @param url 请求URL * @param params 请求参数 * @param config 额外配置 */ protected static get( url: string, params?: Record, config?: Record ): Promise { return request.get({ url, params, ...config }) } /** * POST 请求 * @param url 请求URL * @param data 请求数据 * @param config 额外配置 */ protected static post( url: string, data?: Record, config?: Record ): Promise { return request.post({ url, data, ...config }) } /** * PUT 请求 * @param url 请求URL * @param data 请求数据 * @param config 额外配置 */ protected static put( url: string, data?: Record, config?: Record ): Promise { return request.put({ url, data, ...config }) } /** * DELETE 请求 * @param url 请求URL * @param params 请求参数 * @param config 额外配置 */ protected static delete( url: string, params?: Record, config?: Record ): Promise { return request.del({ url, params, ...config }) } /** * 获取单个资源 * @param url 请求URL * @param params 请求参数 */ protected static getOne(url: string, params?: Record): Promise> { return this.get>(url, params) } /** * 获取列表(不分页) * @param url 请求URL * @param params 请求参数 */ protected static getList(url: string, params?: Record): Promise> { return this.get>(url, params) } /** * 获取分页列表 * @param url 请求URL * @param params 请求参数 */ protected static getPage( url: string, params?: Record ): Promise> { return this.get>(url, params) } /** * 创建资源 * @param url 请求URL * @param data 请求数据 */ protected static create( url: string, data: Record ): Promise> { return this.post>(url, data) } /** * 更新资源 * @param url 请求URL * @param data 请求数据 */ protected static update( url: string, data: Record ): Promise> { return this.put>(url, data) } /** * 删除资源 * @param url 请求URL * @param params 请求参数 */ protected static remove( url: string, params?: Record ): Promise> { return this.delete>(url, params) } /** * 批量删除 * @param url 请求URL * @param ids ID列表 */ protected static batchDelete(url: string, ids: (string | number)[]): Promise { return this.delete(url, { ids }) } /** * 上传文件 * @param url 请求URL * @param file 文件 * @param params 额外参数 */ protected static upload( url: string, file: File, params?: Record ): Promise> { const formData = new FormData() formData.append('file', file) if (params) { Object.keys(params).forEach((key) => { formData.append(key, params[key]) }) } return request.post>({ url, data: formData, headers: { 'Content-Type': 'multipart/form-data' } }) } /** * 下载文件 * @param url 请求URL * @param params 请求参数 * @param fileName 文件名 */ protected static download( url: string, params?: Record, fileName?: string ): Promise { return request .get({ url, params, responseType: 'blob' }) .then((blob: any) => { const downloadUrl = window.URL.createObjectURL(blob) const link = document.createElement('a') link.href = downloadUrl link.download = fileName || 'download' document.body.appendChild(link) link.click() document.body.removeChild(link) window.URL.revokeObjectURL(downloadUrl) }) } }