创建请求验证码和登录接口

This commit is contained in:
2026-06-07 11:26:28 +08:00
parent caf9038dfa
commit 78192a2f61
4 changed files with 166 additions and 0 deletions
+28
View File
@@ -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<VerifyCodeResult> {
const config = new RequestConfig()
config.url = "/auth/code"
config.method = HttpMethod.GET
const res = await this.http.request<UTSJSONObject>(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
}
}
+43
View File
@@ -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<LoginResult> {
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<UTSJSONObject>(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
}
}
+51
View File
@@ -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<T = any> {
total : number;
rows : T[];
}
+44
View File
@@ -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<LoginResult> {
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<UTSJSONObject>(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
}
}