feat: Phase 3 - API layer + Pinia stores + app integration
- HTTP client (axios interceptors, token mgmt, typed APIs) - Pinia stores: token, user (login/logout), app (dark mode, sidebar) - globalConfig TS types + Window augmentation - Vue Router (hash history, auth guard) - Login/Home/Mine pages (Vant UI) - Vant integration + globalConfig dev script - Build passes (vue-tsc + vite)
This commit is contained in:
139
src/api/common.ts
Normal file
139
src/api/common.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
/**
|
||||
* 通用 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<UploadResult> {
|
||||
const formData = new FormData()
|
||||
formData.append('file', file)
|
||||
|
||||
return post<UploadResult>('/common/upload', formData, {
|
||||
headers: { 'Content-Type': 'multipart/form-data' },
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量上传文件
|
||||
* @param files - 文件对象数组
|
||||
* @returns 上传结果数组
|
||||
*/
|
||||
export async function uploadFiles(files: File[]): Promise<UploadResult[]> {
|
||||
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<DictItem[]> {
|
||||
return get<DictItem[]>('/system/dict/data/type', { dictType })
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字典类型获取字典数据(RESTful 风格)
|
||||
* @param dictType - 字典类型标识
|
||||
* @returns 字典项数组
|
||||
*/
|
||||
export function getDictDataByType(dictType: string): Promise<DictItem[]> {
|
||||
return get<DictItem[]>(`/system/dict/data/type/${dictType}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取验证码图片
|
||||
* @returns 验证码信息(uuid 和 base64 图片)
|
||||
*/
|
||||
export function getCaptcha(): Promise<CaptchaResult> {
|
||||
return get<CaptchaResult>('/captchaImage')
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据配置键获取系统配置值
|
||||
* @param configKey - 配置键名
|
||||
* @returns 配置项的值
|
||||
*/
|
||||
export function getConfigValue(configKey: string): Promise<string> {
|
||||
return get<string>(`/system/config/configKey/${configKey}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统配置项详情
|
||||
* @param configKey - 配置键名
|
||||
* @returns 配置项完整信息
|
||||
*/
|
||||
export function getConfigItem(configKey: string): Promise<ConfigItem> {
|
||||
return get<ConfigItem>(`/system/config/configKey/${configKey}`)
|
||||
}
|
||||
Reference in New Issue
Block a user