2024-09-03 16:46:42 +08:00
|
|
|
import type { GrantType } from '@vben/common-ui';
|
2025-05-18 14:28:57 +08:00
|
|
|
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';
|
|
|
|
|
2025-05-18 14:28:57 +08:00
|
|
|
import { Modal } from 'ant-design-vue';
|
|
|
|
|
2024-07-28 14:29:05 +08:00
|
|
|
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
|
|
|
|
2024-07-28 14:29:05 +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-07-28 14:29:05 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
// }
|
|
|
|
|
2024-07-28 14:29:05 +08:00
|
|
|
/** 登录接口返回值 */
|
|
|
|
export interface LoginResult {
|
2024-08-07 08:57:56 +08:00
|
|
|
access_token: string;
|
|
|
|
client_id: string;
|
|
|
|
expire_in: number;
|
2024-07-28 14:29:05 +08:00
|
|
|
}
|
2024-08-19 22:59:42 +08:00
|
|
|
|
|
|
|
export interface RefreshTokenResult {
|
|
|
|
data: string;
|
|
|
|
status: number;
|
|
|
|
}
|
2024-07-28 14:29:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 登录
|
|
|
|
*/
|
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
|
|
|
|
*/
|
2025-05-18 14:28:57 +08:00
|
|
|
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-07-28 14:29:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-08-07 08:57:56 +08:00
|
|
|
* 获取租户列表 下拉框使用
|
2024-07-28 14:29:05 +08:00
|
|
|
*/
|
2024-08-07 08:57:56 +08:00
|
|
|
export function tenantList() {
|
|
|
|
return requestClient.get<TenantResp>('/auth/tenant/list');
|
2024-07-28 14:29:05 +08:00
|
|
|
}
|
2024-08-08 08:03:04 +08:00
|
|
|
|
2024-09-03 16:46:42 +08:00
|
|
|
/**
|
|
|
|
* vben的 先不删除
|
|
|
|
* @returns string[]
|
|
|
|
*/
|
2024-08-07 13:42:33 +08:00
|
|
|
export async function getAccessCodesApi() {
|
2024-07-28 14:29:05 +08:00
|
|
|
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);
|
|
|
|
}
|