ruoyi-plus-vben5/apps/web-antd/src/api/core/auth.ts

205 lines
4.5 KiB
TypeScript
Raw Normal View History

2024-09-03 16:46:42 +08:00
import type { GrantType } from '@vben/common-ui';
import type { HttpResponse } from '@vben/request';
import { h } from 'vue';
2024-09-03 16:46:42 +08:00
2024-08-07 08:57:56 +08:00
import { useAppConfig } from '@vben/hooks';
import { Modal } from 'ant-design-vue';
import { requestClient } from '#/api/request';
2024-12-19 11:27:23 +08:00
const { clientId, sseEnable } = useAppConfig(
import.meta.env,
import.meta.env.PROD,
);
2024-08-07 08:57:56 +08:00
export namespace AuthApi {
2024-09-03 16:46:42 +08:00
/**
* @description:
* @param clientId ID loginApi内部处理了
* @param grantType /
* @param tenantId id
*/
export interface BaseLoginParams {
clientId?: string;
grantType: GrantType;
2024-08-07 08:57:56 +08:00
tenantId: string;
2024-09-03 16:46:42 +08:00
}
/**
* @description: oauth登录需要用到的参数
* @param socialCode
* @param socialState
* @param source justauth.type.xxx的回调地址的source对应
*/
export interface OAuthLoginParams extends BaseLoginParams {
socialCode: string;
socialState: string;
source: string;
}
/**
* @description:
* @param code ()
* @param uuid ID ()
* @param username
* @param password
*/
export interface SimpleLoginParams extends BaseLoginParams {
code?: string;
2024-08-07 08:57:56 +08:00
uuid?: string;
2024-09-03 16:46:42 +08:00
username: string;
password: string;
}
2024-09-03 16:46:42 +08:00
export type LoginParams = OAuthLoginParams | SimpleLoginParams;
// /** 登录接口参数 */
// export interface LoginParams {
// code?: string;
// grantType: string;
// password: string;
// tenantId: string;
// username: string;
// uuid?: string;
// }
/** 登录接口返回值 */
export interface LoginResult {
2024-08-07 08:57:56 +08:00
access_token: string;
client_id: string;
expire_in: number;
}
export interface RefreshTokenResult {
data: string;
status: number;
}
}
/**
*
*/
2024-08-07 13:42:33 +08:00
export async function loginApi(data: AuthApi.LoginParams) {
2024-08-07 08:57:56 +08:00
return requestClient.post<AuthApi.LoginResult>(
'/auth/login',
{ ...data, clientId },
{
encrypt: true,
},
);
}
/**
*
* @returns void
*/
export async function doLogout() {
const resp = await requestClient.post<HttpResponse<void>>(
'/auth/logout',
null,
{
isTransformResponse: false,
},
);
// 无奈之举 对错误用法的提示
if (resp.code === 401 && import.meta.env.DEV) {
Modal.destroyAll();
Modal.warn({
title: '后端配置出现错误',
centered: true,
content: h('div', { class: 'flex flex-col gap-2' }, [
`检测到你的logout接口返回了401, 导致前端一直进入循环逻辑???`,
...Array.from({ length: 3 }, () =>
h(
'span',
{ class: 'font-bold text-red-500 text-[18px]' },
'去检查你的后端配置!别盯着前端找问题了!这不是前端问题!',
),
),
]),
});
}
2024-08-07 08:57:56 +08:00
}
2024-09-12 16:45:12 +08:00
/**
* sse连接
* @returns void
*/
export function seeConnectionClose() {
2024-12-19 11:27:23 +08:00
/**
* sse
*/
if (!sseEnable) {
return;
}
2024-09-12 16:45:12 +08:00
return requestClient.get<void>('/resource/sse/close');
}
2024-08-07 08:57:56 +08:00
/**
* @param companyName /
* @param domain (http(s)://) 可选
* @param tenantId id
*/
export interface TenantOption {
companyName: string;
domain?: string;
tenantId: string;
}
/**
* @param tenantEnabled
* @param voList
*/
export interface TenantResp {
tenantEnabled: boolean;
voList: TenantOption[];
}
/**
2024-08-07 08:57:56 +08:00
* 使
*/
2024-08-07 08:57:56 +08:00
export function tenantList() {
return requestClient.get<TenantResp>('/auth/tenant/list');
}
2024-09-03 16:46:42 +08:00
/**
* vben的
* @returns string[]
*/
2024-08-07 13:42:33 +08:00
export async function getAccessCodesApi() {
return requestClient.get<string[]>('/auth/codes');
}
2024-09-03 16:46:42 +08:00
/**
*
* @param source
* @returns url
*/
export function authBinding(source: string, tenantId: string) {
return requestClient.get<string>(`/auth/binding/${source}`, {
params: {
domain: window.location.host,
tenantId,
},
});
}
/**
*
* @param id id
*/
export function authUnbinding(id: string) {
return requestClient.deleteWithMsg<void>(`/auth/unlock/${id}`);
}
/**
* oauth授权回调
* @param data oauth授权
* @returns void
*/
export function authCallback(data: AuthApi.OAuthLoginParams) {
return requestClient.post<void>('/auth/social/callback', data);
}