reactor:【system 系统管理】oauth2/token 进一步统一代码风格

pull/210/head
YunaiV 2025-09-06 19:47:13 +08:00
parent c9e782fefe
commit 1d11ebad3a
4 changed files with 148 additions and 61 deletions

View File

@ -13,6 +13,7 @@ export function useGridFormSchema(): VbenFormSchema[] {
component: 'Input',
componentProps: {
placeholder: '请输入用户编号',
allowClear: true,
},
},
{
@ -22,6 +23,7 @@ export function useGridFormSchema(): VbenFormSchema[] {
componentProps: {
options: getDictOptions(DICT_TYPE.USER_TYPE, 'number'),
placeholder: '请选择用户类型',
allowClear: true,
},
},
{
@ -30,6 +32,7 @@ export function useGridFormSchema(): VbenFormSchema[] {
component: 'Input',
componentProps: {
placeholder: '请输入客户端编号',
allowClear: true,
},
},
];
@ -42,18 +45,22 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
{
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 },
@ -62,15 +69,18 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
{
field: 'clientId',
title: '客户端编号',
minWidth: 120,
},
{
field: 'expiresTime',
title: '过期时间',
minWidth: 180,
formatter: 'formatDateTime',
},
{
field: 'createTime',
title: '创建时间',
minWidth: 180,
formatter: 'formatDateTime',
},
{

View File

@ -2,7 +2,10 @@
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemOAuth2TokenApi } from '#/api/system/oauth2/token';
import { DocAlert, Page } from '@vben/common-ui';
import { ref } from 'vue';
import { confirm, DocAlert, Page } from '@vben/common-ui';
import { isEmpty } from '@vben/utils';
import { message } from 'ant-design-vue';
@ -16,7 +19,7 @@ import { $t } from '#/locales';
import { useGridColumns, useGridFormSchema } from './data';
/** 刷新表格 */
function onRefresh() {
function handleRefresh() {
gridApi.query();
}
@ -24,20 +27,43 @@ function onRefresh() {
async function handleDelete(row: SystemOAuth2TokenApi.OAuth2Token) {
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', ['令牌']),
key: 'action_key_msg',
duration: 0,
});
try {
await deleteOAuth2Token(row.accessToken);
message.success({
content: $t('ui.actionMessage.deleteSuccess', ['令牌']),
key: 'action_key_msg',
});
onRefresh();
message.success($t('ui.actionMessage.deleteSuccess', ['令牌']));
handleRefresh();
} finally {
hideLoading();
}
}
/** 批量删除 OAuth2 令牌 */
async function handleDeleteBatch() {
await confirm($t('ui.actionMessage.deleteBatchConfirm'));
const hideLoading = message.loading({
content: $t('ui.actionMessage.deletingBatch'),
duration: 0,
});
try {
await deleteOAuth2Token(checkedIds.value.join(','));
checkedIds.value = [];
message.success($t('ui.actionMessage.deleteSuccess'));
handleRefresh();
} finally {
hideLoading();
}
}
const checkedIds = ref<string[]>([]);
function handleRowCheckboxChange({
records,
}: {
records: SystemOAuth2TokenApi.OAuth2Token[];
}) {
checkedIds.value = records.map((item) => item.accessToken);
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
@ -58,13 +84,18 @@ const [Grid, gridApi] = useVbenVxeGrid({
},
},
rowConfig: {
keyField: 'id',
keyField: 'accessToken',
isHover: true,
},
toolbarConfig: {
refresh: true,
search: true,
},
} as VxeTableGridOptions<SystemOAuth2TokenApi.OAuth2Token>,
gridEvents: {
checkboxAll: handleRowCheckboxChange,
checkboxChange: handleRowCheckboxChange,
},
});
</script>
@ -78,17 +109,32 @@ const [Grid, gridApi] = useVbenVxeGrid({
</template>
<Grid table-title="">
<template #toolbar-tools>
<TableAction
:actions="[
{
label: $t('ui.actionTitle.deleteBatch'),
type: 'primary',
danger: true,
icon: ACTION_ICON.DELETE,
auth: ['system:oauth2-token:delete'],
disabled: isEmpty(checkedIds),
onClick: handleDeleteBatch,
},
]"
/>
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: '强退',
label: $t('common.delete'),
type: 'link',
danger: true,
icon: ACTION_ICON.DELETE,
auth: ['system:oauth2-token:delete'],
popConfirm: {
title: `确定要强退令牌吗?`,
title: $t('ui.actionMessage.deleteConfirm', ['令牌']),
confirm: handleDelete.bind(null, row),
},
},

View File

@ -1,13 +1,9 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemOAuth2TokenApi } from '#/api/system/oauth2/token';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { useAccess } from '@vben/access';
import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
const { hasAccessByCodes } = useAccess();
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
return [
@ -17,6 +13,7 @@ export function useGridFormSchema(): VbenFormSchema[] {
component: 'Input',
componentProps: {
placeholder: '请输入用户编号',
clearable: true,
},
},
{
@ -26,6 +23,7 @@ export function useGridFormSchema(): VbenFormSchema[] {
componentProps: {
options: getDictOptions(DICT_TYPE.USER_TYPE, 'number'),
placeholder: '请选择用户类型',
clearable: true,
},
},
{
@ -34,16 +32,16 @@ export function useGridFormSchema(): VbenFormSchema[] {
component: 'Input',
componentProps: {
placeholder: '请输入客户端编号',
clearable: true,
},
},
];
}
/** 列表的字段 */
export function useGridColumns<T = SystemOAuth2TokenApi.OAuth2Token>(
onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
export function useGridColumns(): VxeTableGridOptions['columns'] {
return [
{ type: 'checkbox', width: 40 },
{
field: 'accessToken',
title: '访问令牌',
@ -86,26 +84,10 @@ export function useGridColumns<T = SystemOAuth2TokenApi.OAuth2Token>(
formatter: 'formatDateTime',
},
{
field: 'operation',
title: '操作',
minWidth: 100,
align: 'center',
width: 80,
fixed: 'right',
cellRender: {
attrs: {
nameField: 'accessToken',
nameTitle: 'OAuth2 令牌',
onClick: onActionClick,
},
name: 'CellOperation',
options: [
{
code: 'delete',
text: '强退',
show: hasAccessByCodes(['system:oauth2-token:delete']),
},
],
},
slots: { default: 'actions' },
},
];
}

View File

@ -1,15 +1,15 @@
<script lang="ts" setup>
import type {
OnActionClickParams,
VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemOAuth2TokenApi } from '#/api/system/oauth2/token';
import { DocAlert, Page } from '@vben/common-ui';
import { ref } from 'vue';
import { confirm, DocAlert, Page } from '@vben/common-ui';
import { isEmpty } from '@vben/utils';
import { ElLoading, ElMessage } from 'element-plus';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import {
deleteOAuth2Token,
getOAuth2TokenPage,
@ -19,43 +19,55 @@ import { $t } from '#/locales';
import { useGridColumns, useGridFormSchema } from './data';
/** 刷新表格 */
function onRefresh() {
function handleRefresh() {
gridApi.query();
}
/** 删除 OAuth2 令牌 */
async function onDelete(row: SystemOAuth2TokenApi.OAuth2Token) {
async function handleDelete(row: SystemOAuth2TokenApi.OAuth2Token) {
const loadingInstance = ElLoading.service({
text: $t('ui.actionMessage.deleting', [row.accessToken]),
text: $t('ui.actionMessage.deleting', ['令牌']),
});
try {
await deleteOAuth2Token(row.accessToken);
ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.accessToken]));
onRefresh();
ElMessage.success($t('ui.actionMessage.deleteSuccess', ['令牌']));
handleRefresh();
} finally {
loadingInstance.close();
}
}
/** 表格操作按钮的回调函数 */
function onActionClick({
code,
row,
}: OnActionClickParams<SystemOAuth2TokenApi.OAuth2Token>) {
switch (code) {
case 'delete': {
onDelete(row);
break;
}
/** 批量删除 OAuth2 令牌 */
async function handleDeleteBatch() {
await confirm($t('ui.actionMessage.deleteBatchConfirm'));
const loadingInstance = ElLoading.service({
text: $t('ui.actionMessage.deletingBatch'),
});
try {
await deleteOAuth2Token(checkedIds.value.join(','));
checkedIds.value = [];
ElMessage.success($t('ui.actionMessage.deleteSuccess'));
handleRefresh();
} finally {
loadingInstance.close();
}
}
const checkedIds = ref<string[]>([]);
function handleRowCheckboxChange({
records,
}: {
records: SystemOAuth2TokenApi.OAuth2Token[];
}) {
checkedIds.value = records.map((item) => item.accessToken);
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
},
gridOptions: {
columns: useGridColumns(onActionClick),
columns: useGridColumns(),
height: 'auto',
keepSource: true,
proxyConfig: {
@ -70,13 +82,18 @@ const [Grid, gridApi] = useVbenVxeGrid({
},
},
rowConfig: {
keyField: 'id',
keyField: 'accessToken',
isHover: true,
},
toolbarConfig: {
refresh: true,
search: true,
},
} as VxeTableGridOptions<SystemOAuth2TokenApi.OAuth2Token>,
gridEvents: {
checkboxAll: handleRowCheckboxChange,
checkboxChange: handleRowCheckboxChange,
},
});
</script>
@ -89,6 +106,38 @@ const [Grid, gridApi] = useVbenVxeGrid({
/>
</template>
<Grid table-title="" />
<Grid table-title="">
<template #toolbar-tools>
<TableAction
:actions="[
{
label: $t('ui.actionTitle.deleteBatch'),
type: 'danger',
icon: ACTION_ICON.DELETE,
auth: ['system:oauth2-token:delete'],
disabled: isEmpty(checkedIds),
onClick: handleDeleteBatch,
},
]"
/>
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.delete'),
type: 'danger',
link: true,
icon: ACTION_ICON.DELETE,
auth: ['system:oauth2-token:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', ['令牌']),
confirm: handleDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid>
</Page>
</template>