Files
xk-admin/apps/web-antd/src/api/core/auth.ts

106 lines
2.8 KiB
TypeScript
Raw Normal View History

import { baseRequestClient, requestClient } from '#/api/request';
export namespace AuthApi {
/** 登录接口参数 */
export interface LoginParams {
2026-01-22 13:46:35 +08:00
login_method?: 'phone' | 'login_account' | 'job_number'; // 登录方式类型
password?: string;
username?: string;
2026-01-22 13:46:35 +08:00
phone?: string; // 手机号当login_method为phone时
2025-03-06 19:12:26 +08:00
code?: string;
2026-01-22 13:46:35 +08:00
login_account?: string; // 登录账号当login_method为login_account时
job_number?: string; // 工号当login_method为job_number时
}
/** 登录接口返回值 */
export interface LoginResult {
2025-03-06 19:12:26 +08:00
token: string;
}
export interface RefreshTokenResult {
data: string;
status: number;
}
2026-01-22 13:46:35 +08:00
/** 发送验证码返回结果 */
export interface SendCodeResult {
code?: string; // 开发环境返回验证码
accounts?: Array<{
id: number;
login_account: string;
job_number: string;
}>; // 如果手机号绑定多个账号,返回账号列表
need_select?: boolean; // 是否需要选择账号/工号
}
}
/**
*
*/
2024-08-07 13:42:33 +08:00
export async function loginApi(data: AuthApi.LoginParams) {
2026-01-22 13:46:35 +08:00
const params: any = {
login_method: data.login_method || 'phone', // 添加默认值
2025-03-06 19:12:26 +08:00
password: data.password,
code: data.code,
2026-01-22 13:46:35 +08:00
};
if (data.login_method === 'phone') {
params.phone = data.phone || data.username;
} else if (data.login_method === 'login_account') {
params.login_account = data.login_account;
} else if (data.login_method === 'job_number') {
params.job_number = data.job_number;
}
return requestClient.post<AuthApi.LoginResult>('login', params);
2025-03-06 19:12:26 +08:00
}
/**
*
*/
2026-01-22 13:46:35 +08:00
export async function sendVerificationCode(params: {
login_method?: 'phone' | 'login_account' | 'job_number';
username?: string;
phone?: string;
login_account?: string;
job_number?: string;
}) {
const requestParams: any = {
login_method: params.login_method || 'phone',
};
if (params.login_method === 'phone') {
requestParams.phone = params.phone || params.username;
} else if (params.login_method === 'login_account') {
requestParams.login_account = params.login_account;
} else if (params.login_method === 'job_number') {
requestParams.job_number = params.job_number;
}
return requestClient.post<AuthApi.SendCodeResult>('send-verification-code', requestParams);
}
/**
* accessToken
*/
export async function refreshTokenApi() {
return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
withCredentials: true,
});
}
/**
* 退
*/
export async function logoutApi() {
2025-03-06 19:12:26 +08:00
return baseRequestClient.post('logout', {
withCredentials: true,
});
}
/**
*
*/
2024-08-07 13:42:33 +08:00
export async function getAccessCodesApi() {
2025-03-06 19:12:26 +08:00
return requestClient.get<string[]>('auth/codes');
}