codernew-api-frontend/src/types/api.ts
gaoziman f312be1aa8 feat(类型系统): 新增项目类型定义和常量
- 新增 API 响应类型定义(src/types/api.ts)
- 新增用户认证相关类型(src/types/auth.ts)
- 新增用户信息类型(src/types/user.ts)
- 新增全局常量定义(src/constants/)
- 更新项目设置配置
2025-11-18 20:44:43 +08:00

112 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* API 通用类型定义
* @description 定义后端 API 的通用数据结构类型
*/
// ==================== 通用响应类型 ====================
/**
* 统一响应格式
* @template T - 响应数据类型
*/
export interface R<T = any> {
/** 响应码:'0'-成功,'1'-失败,其他-业务错误码(兼容后端返回字符串或数字) */
code: string | number;
/** 响应消息 */
msg: string;
/** 响应数据 */
data: T;
/** 时间戳 */
timestamp?: number;
}
/**
* 分页响应格式
* @template T - 列表数据项类型
*/
export interface PageResp<T = any> {
/** 数据列表 */
list: T[];
/** 总记录数 */
total: number;
/** 每页条数 */
size?: number;
/** 当前页码 */
current?: number;
/** 总页数 */
pages?: number;
}
// ==================== 分页查询参数 ====================
/**
* 分页查询参数
*/
export interface PageQuery {
/** 当前页码(从 1 开始)*/
current?: number;
/** 每页条数 */
size?: number;
/** 排序字段,格式:字段名,排序方向createTime,desc */
sort?: string;
}
// ==================== HTTP 错误响应 ====================
/**
* HTTP 错误响应
*/
export interface HttpError {
/** HTTP 状态码 */
status: number;
/** 错误消息 */
message: string;
/** 错误详情 */
error?: any;
}
// ==================== 请求配置扩展 ====================
/**
* 自定义请求配置
*/
export interface RequestConfig {
/** 是否显示加载提示 */
showLoading?: boolean;
/** 是否显示成功提示 */
showSuccessMsg?: boolean;
/** 是否显示错误提示 */
showErrorMsg?: boolean;
/** 自定义成功提示消息 */
successMsg?: string;
/** 自定义错误提示消息 */
errorMsg?: string;
/** 是否需要认证(默认 true */
requireAuth?: boolean;
}
// ==================== 类型守卫函数 ====================
/**
* 判断响应是否成功
* @param response - 响应对象
* @returns 是否成功
*/
export function isSuccessResponse<T>(response: R<T>): boolean {
return response.code == 0; // 使用宽松相等,兼容字符串和数字
}
/**
* 判断是否为分页响应
* @param data - 响应数据
* @returns 是否为分页响应
*/
export function isPageResponse<T>(data: any): data is PageResp<T> {
return (
data !== null &&
typeof data === 'object' &&
Array.isArray(data.list) &&
typeof data.total === 'number'
);
}