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

View File

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

View File

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

View File

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