diff --git a/docs/prompt.txt b/docs/prompt.txt index b72152f..c866c38 100644 --- a/docs/prompt.txt +++ b/docs/prompt.txt @@ -5,3 +5,4 @@ docs/openapi.json文件是后端提供的标准OpenAPI接口文档,你需要 3. 按模块拆分接口请求函数; 4. 严格遵循文档字段,不新增、不删减参数,区分必填/可选; 5. 按现有src/api/modules中的格式书写 +6. 定义的TS类型名称,interface以I开头命名,type以T开头命名 diff --git a/src/api/modules/account/index.ts b/src/api/modules/account/index.ts index 870f4a4..e0bb750 100644 --- a/src/api/modules/account/index.ts +++ b/src/api/modules/account/index.ts @@ -1,2 +1 @@ export * from './login.ts' -export * from './user.ts' diff --git a/src/api/modules/account/login.ts b/src/api/modules/account/login.ts index 6a84cb6..a81ed0d 100644 --- a/src/api/modules/account/login.ts +++ b/src/api/modules/account/login.ts @@ -1,17 +1,62 @@ import { requestClient } from '@/api/request' -/** 登录响应数据类型 */ -export interface ILoginResponse { - token: string -} - -/** 登录请求数据类型 */ -export interface ILoginRequest { +/** + * 登录请求参数 + * + * 对应 OpenAPI: LoginRequest + */ +export interface LoginRequest { + /** 用户名,不能为空 */ username: string + /** 密码,不能为空 */ password: string } -/** 登录 */ -function login(req: ILoginRequest) { - return requestClient.post('/account', req) +/** + * 注册请求参数 + * + * 对应 OpenAPI: RegisterRequest + */ +export interface RegisterRequest { + /** 用户名,长度限制 3-32 位 */ + username: string + /** 密码,长度限制 6-64 位 */ + password: string +} + +/** + * 登录/注册响应 + * + * 对应 OpenAPI: LoginResponse + */ +export interface LoginResponse { + /** Sa-Token 认证令牌(UUID 格式) */ + token: string +} + +/** + * 用户登录 + * + * 使用用户名和密码进行登录认证,成功返回 Sa-Token 令牌。 + * 该接口无需登录即可调用。 + * + * @param req - 登录请求参数 + * @returns 包含认证令牌的响应 + */ +export function login(req: LoginRequest) { + return requestClient.post('/account/login', req) +} + +/** + * 用户注册 + * + * 注册新账号。用户名长度 3-32 位,密码长度 6-64 位。 + * 注册成功后自动登录并返回 token。 + * 该接口无需登录即可调用。 + * + * @param req - 注册请求参数 + * @returns 包含认证令牌的响应 + */ +export function register(req: RegisterRequest) { + return requestClient.post('/account/register', req) } diff --git a/src/api/modules/account/user.ts b/src/api/modules/account/user.ts deleted file mode 100644 index 67f4987..0000000 --- a/src/api/modules/account/user.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { requestClient } from '@/api/request' - -/** 账户信息 */ -export interface IUserInfo { - id: number - username: string -} - -/** 获取账户信息 */ -function getAccountInfo() { - return requestClient.get('/user/info') -} diff --git a/src/api/modules/index.ts b/src/api/modules/index.ts new file mode 100644 index 0000000..d381706 --- /dev/null +++ b/src/api/modules/index.ts @@ -0,0 +1,2 @@ +export * from './account/index.ts' +export * from './user/index.ts' diff --git a/src/api/modules/user/index.ts b/src/api/modules/user/index.ts new file mode 100644 index 0000000..b783058 --- /dev/null +++ b/src/api/modules/user/index.ts @@ -0,0 +1 @@ +export * from './info.ts' diff --git a/src/api/modules/user/info.ts b/src/api/modules/user/info.ts new file mode 100644 index 0000000..8b14aa3 --- /dev/null +++ b/src/api/modules/user/info.ts @@ -0,0 +1,51 @@ +import { requestClient } from '@/api/request' + +/** + * 性别枚举 + * + * 对应 OpenAPI: Sex (x-enums) + * 1 = 男,2 = 女 + */ +export const Sex = { + /** 男 */ + MALE: 1, + /** 女 */ + FEMALE: 2 +} as const + +/** 性别类型 */ +export type SexType = (typeof Sex)[keyof typeof Sex] + +/** + * 用户信息响应 + * + * 对应 OpenAPI: UserInfoResponse + * 所有字段均为可选,部分字段可能为 null + */ +export interface UserInfoResponse { + /** 用户昵称,最长 16 位 */ + nickName?: string + /** 头像图片 URL 地址,可能为 null */ + avatar?: string | null + /** 个性签名,最长 64 位,可能为 null */ + signature?: string | null + /** 年龄,可能为 null */ + age?: number | null + /** 性别:1=男,2=女,可能为 null */ + sex?: SexType | null + /** 生日(ISO 8601 日期格式),可能为 null */ + birthday?: string | null +} + +/** + * 获取当前登录用户信息 + * + * 根据请求头中的 saToken 获取当前登录用户的详细信息, + * 包括昵称、头像、个性签名、年龄、性别、生日。 + * 需登录认证。 + * + * @returns 当前登录用户的详细信息 + */ +export function getUserInfo() { + return requestClient.get('/user/info') +} diff --git a/src/api/request/requestClient.ts b/src/api/request/requestClient.ts index ae69032..77dc528 100644 --- a/src/api/request/requestClient.ts +++ b/src/api/request/requestClient.ts @@ -63,11 +63,11 @@ class RequestClient { config.url = resolvePathParams(config.url, reqConfig.pathParams) } - // 跳过鉴权时不注入 token + // 跳过鉴权时不注入 saToken if (!reqConfig?.skipAuth) { const accountStore = useAccountStore() if (accountStore.token) { - config.headers.Authorization = `Bearer ${accountStore.token}` + config.headers.saToken = accountStore.token } } diff --git a/src/api/request/types.ts b/src/api/request/types.ts index 62cedee..69679eb 100644 --- a/src/api/request/types.ts +++ b/src/api/request/types.ts @@ -14,7 +14,7 @@ export interface ApiResponse { export interface InternalRequestConfig extends AxiosRequestConfig { /** 路径参数,替换 URL 中的 :param 占位符(如 /user/:id → /user/1) */ pathParams?: Record - /** 跳过鉴权注入(不携带 Authorization 头) */ + /** 跳过鉴权注入(不携带 saToken 头) */ skipAuth?: boolean }