Some checks failed
CI / Test (ubuntu-latest) (push) Has been cancelled
CI / Test (windows-latest) (push) Has been cancelled
CI / Lint (ubuntu-latest) (push) Has been cancelled
CI / Lint (windows-latest) (push) Has been cancelled
CI / Check (ubuntu-latest) (push) Has been cancelled
CI / Check (windows-latest) (push) Has been cancelled
CodeQL / Analyze (javascript-typescript) (push) Has been cancelled
Deploy Website on push / Deploy Push Playground Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Docs Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Antd Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Element Ftp (push) Has been cancelled
Deploy Website on push / Deploy Push Naive Ftp (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
CI / CI OK (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Issue Close Require / close-issues (push) Has been cancelled
Close stale issues / stale (push) Has been cancelled
161 lines
3.7 KiB
TypeScript
161 lines
3.7 KiB
TypeScript
import { baseRequestClient, requestClient } from '#/api/request';
|
|
|
|
export namespace AuthApi {
|
|
/** 登录接口参数 */
|
|
export interface LoginParams {
|
|
account: string;
|
|
password?: string;
|
|
code?: string;
|
|
admin_id?: number;
|
|
/** @deprecated 兼容旧客户端 */
|
|
login_method?: 'phone' | 'login_account' | 'job_number';
|
|
username?: string;
|
|
phone?: string;
|
|
login_account?: string;
|
|
job_number?: string;
|
|
}
|
|
|
|
/** 登录接口返回值 */
|
|
export interface AccountOption {
|
|
account_type?: string;
|
|
id: number;
|
|
nick_name: string;
|
|
role_name: string;
|
|
store_name: string;
|
|
login_account?: string;
|
|
is_current?: boolean;
|
|
password_matched?: boolean;
|
|
}
|
|
|
|
export interface LoginResult {
|
|
token?: string;
|
|
need_select?: boolean;
|
|
accounts?: AccountOption[];
|
|
default_account_id?: number;
|
|
default_account_type?: string;
|
|
}
|
|
|
|
export interface RefreshTokenResult {
|
|
data: string;
|
|
status: number;
|
|
}
|
|
|
|
/** 发送验证码返回结果 */
|
|
export interface SendCodeResult {
|
|
code?: string;
|
|
need_select?: boolean;
|
|
accounts?: AccountOption[];
|
|
default_account_id?: number;
|
|
default_account_type?: string;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 登录
|
|
*/
|
|
export async function loginApi(data: AuthApi.LoginParams) {
|
|
if (data.account) {
|
|
return requestClient.post<AuthApi.LoginResult>('login', {
|
|
account: data.account,
|
|
password: data.password,
|
|
code: data.code,
|
|
admin_id: data.admin_id,
|
|
});
|
|
}
|
|
|
|
const params: Record<string, unknown> = {
|
|
login_method: data.login_method || 'phone',
|
|
password: data.password,
|
|
code: data.code,
|
|
};
|
|
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* 发送验证码
|
|
*/
|
|
export async function sendVerificationCode(params: {
|
|
account?: string;
|
|
password?: string;
|
|
admin_id?: number;
|
|
login_method?: 'phone' | 'login_account' | 'job_number';
|
|
username?: string;
|
|
phone?: string;
|
|
login_account?: string;
|
|
job_number?: string;
|
|
}) {
|
|
if (params.account) {
|
|
return requestClient.post<AuthApi.SendCodeResult>('send-verification-code', {
|
|
account: params.account,
|
|
admin_id: params.admin_id,
|
|
});
|
|
}
|
|
|
|
const requestParams: Record<string, unknown> = {
|
|
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() {
|
|
return baseRequestClient.post('logout', {
|
|
withCredentials: true,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 获取用户权限码
|
|
*/
|
|
export async function getAccessCodesApi() {
|
|
return requestClient.get<string[]>('auth/codes');
|
|
}
|
|
|
|
/**
|
|
* 同手机号可切换的管理员账号
|
|
*/
|
|
export async function getSiblingAccountsApi() {
|
|
return requestClient.get<AuthApi.AccountOption[]>('auth/sibling-accounts');
|
|
}
|
|
|
|
/**
|
|
* 切换管理员账号
|
|
*/
|
|
export async function switchAccountApi(data: {
|
|
admin_id: number;
|
|
}) {
|
|
return requestClient.post<AuthApi.LoginResult>('auth/switch-account', data);
|
|
}
|