diff --git a/api/codeApi.uts b/api/codeApi.uts new file mode 100644 index 0000000..d10147c --- /dev/null +++ b/api/codeApi.uts @@ -0,0 +1,28 @@ +import { RequestConfig } from "@/types/HttpClientType.uts" +import { HttpMethod } from "@/enum/HttpMethod.uts" +import { VerifyCodeResult } from "@/api/types.uts" +import { HttpClient } from "@/utils/HttpClient" + +export class CodeApi { + + private static http = HttpClient.getInstance() + + static async getImgCode() : Promise { + const config = new RequestConfig() + config.url = "/auth/code" + config.method = HttpMethod.GET + + const res = await this.http.request(config) + const data = res.data + if (data == null) { + throw new Error("验证码数据异常") + } + // 手动转换 UTSJSONObject 到 VerifyCodeResult + const result: VerifyCodeResult = { + captchaEnabled: data["captchaEnabled"] as boolean ?? false, + uuid: data["uuid"] as string ?? "", + img: data["img"] as string ?? "" + } + return result + } +} \ No newline at end of file diff --git a/api/loginApi.uts b/api/loginApi.uts new file mode 100644 index 0000000..8b10f2b --- /dev/null +++ b/api/loginApi.uts @@ -0,0 +1,43 @@ +import { Config } from "../config" +import { RequestConfig } from "@/types/HttpClientType.uts" +import { LoginData, LoginResult } from "./types" +import { HttpMethod } from "@/enum/HttpMethod" +import { HttpClient } from "@/utils/HttpClient" +export class LoginApi { + private static http = HttpClient.getInstance() + static async login(config : LoginData) : Promise { + const configData : any = { + username: config.username, + password: config.password, + rememberMe: config.rememberMe, + socialCode: config.socialCode, + socialState: config.socialState, + source: config.source, + code: config.code, + uuid: config.uuid, + clientId: Config.VITE_APP_CLIENT_ID, + grantType: 'password' + } + const Requestconfig = new RequestConfig() + Requestconfig.url = "/auth/login" + Requestconfig.headers!.isToken = false + Requestconfig.headers!.isEncrypt = true + Requestconfig.headers!.repeatSubmit = false + Requestconfig.method = HttpMethod.POST + Requestconfig.data = configData + + const res = await this.http.request(Requestconfig) + const data = res.data + console.log(data); + if (data == null) { + throw new Error("登陆失败") + } + // 手动转换 UTSJSONObject 到 VerifyCodeResult + const result: LoginResult = { + access_token: data["access_token"] as string + } + console.log(result.access_token); + + return result + } +} diff --git a/api/types.uts b/api/types.uts new file mode 100644 index 0000000..30f6538 --- /dev/null +++ b/api/types.uts @@ -0,0 +1,51 @@ +/** + * 注册 + */ +export type RegisterForm = { + username : string; + password : string; + confirmPassword ?: string; + code ?: string; + uuid ?: string; + userType ?: string; +}; + +/** + * 登录请求 + */ +export class LoginData { + username ?: string; + password ?: string; + rememberMe ?: boolean; + socialCode ?: string; + socialState ?: string; + source ?: string; + code ?: string; + uuid ?: string; + clientId : string = ""; + grantType : string = ""; +} + +/** + * 登录响应 + */ +export type LoginResult { + access_token : string; +} + +/** + * 验证码返回 + */ +export type VerifyCodeResult = { + captchaEnabled : boolean + uuid : string + img : string +} + +/** + * 分页返回结果 + */ +export interface PageResult { + total : number; + rows : T[]; +} diff --git a/api/请求案例.txt b/api/请求案例.txt new file mode 100644 index 0000000..98e1423 --- /dev/null +++ b/api/请求案例.txt @@ -0,0 +1,44 @@ +import { Config } from "../config" +import { RequestConfig } from "@/types/HttpClientType.uts" +import { LoginData, LoginResult } from "./types" +import { HttpMethod } from "@/enum/HttpMethod" +import { HttpClient } from "@/utils/HttpClient" +export class LoginApi { + private static http = HttpClient.getInstance() + static async login(config : LoginData) : Promise { + const configData : any = { + username: config.username, + password: config.password, + rememberMe: config.rememberMe, + socialCode: config.socialCode, + socialState: config.socialState, + source: config.source, + code: config.code, + uuid: config.uuid, + clientId: Config.VITE_APP_CLIENT_ID, + grantType: 'password' + } + // 请求参数处理 + const Requestconfig = new RequestConfig() + Requestconfig.url = "/auth/login" + Requestconfig.headers!.isToken = false + Requestconfig.headers!.isEncrypt = true + Requestconfig.headers!.repeatSubmit = false + Requestconfig.method = HttpMethod.POST + Requestconfig.data = configData + //发起请求 + const res = await this.http.request(Requestconfig) + const data = res.data + console.log(data); + if (data == null) { + throw new Error("登陆失败") + } + // 格式化返回数据 手动转换 UTSJSONObject 到 VerifyCodeResult + const result : LoginResult = { + access_token: data["access_token"] as string + } + console.log(result.access_token); + + return result + } +} \ No newline at end of file