2024-08-19 22:59:42 +08:00
|
|
|
|
import { baseRequestClient, requestClient } from '#/api/request';
|
2024-07-28 14:29:05 +08:00
|
|
|
|
|
|
|
|
|
|
export namespace AuthApi {
|
|
|
|
|
|
/** 登录接口参数 */
|
|
|
|
|
|
export interface LoginParams {
|
2026-05-27 09:34:08 +08:00
|
|
|
|
account: string;
|
2024-10-05 17:09:42 +08:00
|
|
|
|
password?: string;
|
2025-03-06 19:12:26 +08:00
|
|
|
|
code?: string;
|
2026-05-30 16:58:05 +08:00
|
|
|
|
admin_id?: number;
|
2026-05-27 09:34:08 +08:00
|
|
|
|
/** @deprecated 兼容旧客户端 */
|
|
|
|
|
|
login_method?: 'phone' | 'login_account' | 'job_number';
|
|
|
|
|
|
username?: string;
|
|
|
|
|
|
phone?: string;
|
|
|
|
|
|
login_account?: string;
|
|
|
|
|
|
job_number?: string;
|
2024-07-28 14:29:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 登录接口返回值 */
|
2026-05-30 16:58:05 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-28 14:29:05 +08:00
|
|
|
|
export interface LoginResult {
|
2026-05-30 16:58:05 +08:00
|
|
|
|
token?: string;
|
|
|
|
|
|
need_select?: boolean;
|
|
|
|
|
|
accounts?: AccountOption[];
|
|
|
|
|
|
default_account_id?: number;
|
|
|
|
|
|
default_account_type?: string;
|
2024-07-28 14:29:05 +08:00
|
|
|
|
}
|
2024-08-19 22:59:42 +08:00
|
|
|
|
|
|
|
|
|
|
export interface RefreshTokenResult {
|
|
|
|
|
|
data: string;
|
|
|
|
|
|
status: number;
|
|
|
|
|
|
}
|
2026-01-22 13:46:35 +08:00
|
|
|
|
|
|
|
|
|
|
/** 发送验证码返回结果 */
|
|
|
|
|
|
export interface SendCodeResult {
|
2026-05-27 09:34:08 +08:00
|
|
|
|
code?: string;
|
|
|
|
|
|
need_select?: boolean;
|
2026-05-30 16:58:05 +08:00
|
|
|
|
accounts?: AccountOption[];
|
|
|
|
|
|
default_account_id?: number;
|
|
|
|
|
|
default_account_type?: string;
|
2026-01-22 13:46:35 +08:00
|
|
|
|
}
|
2024-07-28 14:29:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 登录
|
|
|
|
|
|
*/
|
2024-08-07 13:42:33 +08:00
|
|
|
|
export async function loginApi(data: AuthApi.LoginParams) {
|
2026-08-01 08:04:31 +08:00
|
|
|
|
// PC 管理端固定 web,供单机登录按端区分会话
|
|
|
|
|
|
const clientType = 'web';
|
2026-05-27 09:34:08 +08:00
|
|
|
|
if (data.account) {
|
|
|
|
|
|
return requestClient.post<AuthApi.LoginResult>('login', {
|
|
|
|
|
|
account: data.account,
|
|
|
|
|
|
password: data.password,
|
|
|
|
|
|
code: data.code,
|
2026-05-30 16:58:05 +08:00
|
|
|
|
admin_id: data.admin_id,
|
2026-08-01 08:04:31 +08:00
|
|
|
|
client_type: clientType,
|
2026-05-27 09:34:08 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const params: Record<string, unknown> = {
|
|
|
|
|
|
login_method: data.login_method || 'phone',
|
2025-03-06 19:12:26 +08:00
|
|
|
|
password: data.password,
|
|
|
|
|
|
code: data.code,
|
2026-08-01 08:04:31 +08:00
|
|
|
|
client_type: clientType,
|
2026-01-22 13:46:35 +08:00
|
|
|
|
};
|
2026-05-27 09:34:08 +08:00
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
2026-05-27 09:34:08 +08:00
|
|
|
|
|
2026-01-22 13:46:35 +08:00
|
|
|
|
return requestClient.post<AuthApi.LoginResult>('login', params);
|
2025-03-06 19:12:26 +08:00
|
|
|
|
}
|
2026-05-27 09:34:08 +08:00
|
|
|
|
|
2025-03-06 19:12:26 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 发送验证码
|
|
|
|
|
|
*/
|
2026-01-22 13:46:35 +08:00
|
|
|
|
export async function sendVerificationCode(params: {
|
2026-05-27 09:34:08 +08:00
|
|
|
|
account?: string;
|
|
|
|
|
|
password?: string;
|
2026-05-30 16:58:05 +08:00
|
|
|
|
admin_id?: number;
|
2026-01-22 13:46:35 +08:00
|
|
|
|
login_method?: 'phone' | 'login_account' | 'job_number';
|
|
|
|
|
|
username?: string;
|
|
|
|
|
|
phone?: string;
|
|
|
|
|
|
login_account?: string;
|
|
|
|
|
|
job_number?: string;
|
|
|
|
|
|
}) {
|
2026-05-27 09:34:08 +08:00
|
|
|
|
if (params.account) {
|
|
|
|
|
|
return requestClient.post<AuthApi.SendCodeResult>('send-verification-code', {
|
|
|
|
|
|
account: params.account,
|
2026-06-18 13:24:58 +08:00
|
|
|
|
password: params.password,
|
2026-05-30 16:58:05 +08:00
|
|
|
|
admin_id: params.admin_id,
|
2026-05-27 09:34:08 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const requestParams: Record<string, unknown> = {
|
2026-01-22 13:46:35 +08:00
|
|
|
|
login_method: params.login_method || 'phone',
|
|
|
|
|
|
};
|
2026-05-27 09:34:08 +08:00
|
|
|
|
|
2026-01-22 13:46:35 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2026-05-27 09:34:08 +08:00
|
|
|
|
|
|
|
|
|
|
return requestClient.post<AuthApi.SendCodeResult>(
|
|
|
|
|
|
'send-verification-code',
|
|
|
|
|
|
requestParams,
|
|
|
|
|
|
);
|
2024-07-28 14:29:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-19 22:59:42 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 刷新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', {
|
2024-08-28 22:37:47 +08:00
|
|
|
|
withCredentials: true,
|
|
|
|
|
|
});
|
2024-08-19 22:59:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-28 14:29:05 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取用户权限码
|
|
|
|
|
|
*/
|
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');
|
2024-07-28 14:29:05 +08:00
|
|
|
|
}
|
2026-05-30 16:58:05 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 同手机号可切换的管理员账号
|
|
|
|
|
|
*/
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|