/** * 通用 API * * 包含文件上传、字典查询、验证码获取、系统配置等公共接口。 * * @module api/common */ import { post, get } from '@/utils/http' // ══════════════════════════════════════════════ // 请求参数类型 // ══════════════════════════════════════════════ /** 文件上传响应 */ export interface UploadResult { /** 文件在服务器上的存储名称 */ fileName: string /** 文件可访问的完整 URL */ url: string } /** 字典项 */ export interface DictItem { /** 字典标签(显示用) */ dictLabel: string /** 字典值(存储用) */ dictValue: string /** 字典类型 */ dictType?: string /** CSS 类名(用于表格回显样式) */ cssClass?: string /** 列表样式 */ listClass?: string /** 是否默认 */ isDefault?: string /** 排序号 */ dictSort?: number } /** 验证码图片响应 */ export interface CaptchaResult { /** 验证码唯一标识,登录时需要回传 */ uuid: string /** Base64 编码的验证码图片 */ img: string /** 是否开启验证码 */ captchaEnabled: boolean } /** 系统配置项 */ export interface ConfigItem { /** 参数键名 */ configKey: string /** 参数值 */ configValue: string } // ══════════════════════════════════════════════ // API 方法 // ══════════════════════════════════════════════ /** * 上传文件 * * 支持图片、文档等文件上传,使用 FormData 格式。 * * @param file - 要上传的文件对象 * @returns 上传结果,包含文件名和访问 URL */ export function uploadFile(file: File): Promise { const formData = new FormData() formData.append('file', file) return post('/common/upload', formData, { headers: { 'Content-Type': 'multipart/form-data' }, }) } /** * 批量上传文件 * @param files - 文件对象数组 * @returns 上传结果数组 */ export async function uploadFiles(files: File[]): Promise { const results: UploadResult[] = [] for (const file of files) { const result = await uploadFile(file) results.push(result) } return results } /** * 获取字典数据 * * 根据字典类型查询对应的字典项列表,用于下拉框、单选框等组件的数据源。 * * @param dictType - 字典类型标识,如 'sys_user_sex' * @returns 字典项数组 */ export function getDictData(dictType: string): Promise { return get('/system/dict/data/type', { dictType }) } /** * 根据字典类型获取字典数据(RESTful 风格) * @param dictType - 字典类型标识 * @returns 字典项数组 */ export function getDictDataByType(dictType: string): Promise { return get(`/system/dict/data/type/${dictType}`) } /** * 获取验证码图片 * @returns 验证码信息(uuid 和 base64 图片) */ export function getCaptcha(): Promise { return get('/captchaImage') } /** * 根据配置键获取系统配置值 * @param configKey - 配置键名 * @returns 配置项的值 */ export function getConfigValue(configKey: string): Promise { return get(`/system/config/configKey/${configKey}`) } /** * 获取系统配置项详情 * @param configKey - 配置键名 * @returns 配置项完整信息 */ export function getConfigItem(configKey: string): Promise { return get(`/system/config/configKey/${configKey}`) }