feat:增加 oauth2.0 授权

This commit is contained in:
YunaiV
2025-04-06 19:49:21 +08:00
parent c754307f11
commit 32be95debd
7 changed files with 761 additions and 0 deletions

View File

@@ -0,0 +1,110 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemOAuth2TokenApi } from '#/api/system/oauth2/token';
import { DICT_TYPE, getDictOptions } from '#/utils/dict';
import { useAccess } from '@vben/access';
const { hasAccessByCodes } = useAccess();
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'userId',
label: '用户编号',
component: 'Input',
componentProps: {
placeholder: '请输入用户编号',
}
},
{
fieldName: 'userType',
label: '用户类型',
component: 'Select',
componentProps: {
options: getDictOptions(DICT_TYPE.USER_TYPE, 'number'),
placeholder: '请选择用户类型',
},
},
{
fieldName: 'clientId',
label: '客户端编号',
component: 'Input',
componentProps: {
placeholder: '请输入客户端编号',
}
},
];
}
/** 列表的字段 */
export function useGridColumns<T = SystemOAuth2TokenApi.SystemOAuth2Token>(
onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
return [
{
field: 'accessToken',
title: '访问令牌',
minWidth: 300,
},
{
field: 'refreshToken',
title: '刷新令牌',
minWidth: 300,
},
{
field: 'userId',
title: '用户编号',
minWidth: 100,
},
{
field: 'userType',
title: '用户类型',
minWidth: 100,
cellRender: {
name: 'CellDict',
props: { type: DICT_TYPE.USER_TYPE },
},
},
{
field: 'clientId',
title: '客户端编号',
minWidth: 120,
},
{
field: 'expiresTime',
title: '过期时间',
minWidth: 180,
formatter: 'formatDateTime',
},
{
field: 'createTime',
title: '创建时间',
minWidth: 180,
formatter: 'formatDateTime',
},
{
field: 'operation',
title: '操作',
minWidth: 100,
align: 'center',
fixed: 'right',
cellRender: {
attrs: {
nameField: 'accessToken',
nameTitle: 'OAuth2 令牌',
onClick: onActionClick,
},
name: 'CellOperation',
options: [
{
code: 'delete',
text: '强退',
show: hasAccessByCodes(['system:oauth2-token:delete']),
},
],
},
},
];
}

View File

@@ -0,0 +1,87 @@
<script lang="ts" setup>
import type { OnActionClickParams, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemOAuth2TokenApi } from '#/api/system/oauth2/token';
import { Page } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { DocAlert } from '#/components/doc-alert';
import { $t } from '#/locales';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getOAuth2TokenPage, deleteOAuth2Token } from '#/api/system/oauth2/token';
import { useGridColumns, useGridFormSchema } from './data';
/** 刷新表格 */
function onRefresh() {
gridApi.query();
}
/** 删除 OAuth2 令牌 */
async function onDelete(row: SystemOAuth2TokenApi.SystemOAuth2Token) {
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', ['令牌']),
duration: 0,
key: 'action_process_msg',
});
try {
await deleteOAuth2Token(row.accessToken);
message.success({
content: $t('ui.actionMessage.operationSuccess'),
key: 'action_process_msg',
});
onRefresh();
} catch (error) {
hideLoading();
}
}
/** 表格操作按钮的回调函数 */
function onActionClick({
code,
row,
}: OnActionClickParams<SystemOAuth2TokenApi.SystemOAuth2Token>) {
switch (code) {
case 'delete': {
onDelete(row);
break;
}
}
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema()
},
gridOptions: {
columns: useGridColumns(onActionClick),
height: 'auto',
keepSource: true,
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getOAuth2TokenPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
rowConfig: {
keyField: 'id',
},
toolbarConfig: {
refresh: { code: 'query' },
search: true,
},
} as VxeTableGridOptions<SystemOAuth2TokenApi.SystemOAuth2Token>,
});
</script>
<template>
<Page auto-content-height>
<DocAlert title="OAuth 2.0SSO 单点登录)" url="https://doc.iocoder.cn/oauth2/" />
<Grid table-title="令牌列表" />
</Page>
</template>