reactor:【infra 基础设施】job 进一步统一代码风格

pull/210/head
YunaiV 2025-09-09 13:18:27 +08:00
parent 96158f22b9
commit b3a65f2492
4 changed files with 179 additions and 170 deletions

View File

@ -133,14 +133,17 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
{ {
field: 'id', field: 'id',
title: '任务编号', title: '任务编号',
minWidth: 80,
}, },
{ {
field: 'name', field: 'name',
title: '任务名称', title: '任务名称',
minWidth: 120,
}, },
{ {
field: 'status', field: 'status',
title: '任务状态', title: '任务状态',
minWidth: 100,
cellRender: { cellRender: {
name: 'CellDict', name: 'CellDict',
props: { type: DICT_TYPE.INFRA_JOB_STATUS }, props: { type: DICT_TYPE.INFRA_JOB_STATUS },
@ -149,14 +152,17 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
{ {
field: 'handlerName', field: 'handlerName',
title: '处理器的名字', title: '处理器的名字',
minWidth: 180,
}, },
{ {
field: 'handlerParam', field: 'handlerParam',
title: '处理器的参数', title: '处理器的参数',
minWidth: 140,
}, },
{ {
field: 'cronExpression', field: 'cronExpression',
title: 'CRON 表达式', title: 'CRON 表达式',
minWidth: 120,
}, },
{ {
title: '操作', title: '操作',

View File

@ -39,7 +39,7 @@ const [DetailModal, detailModalApi] = useVbenModal({
}); });
/** 刷新表格 */ /** 刷新表格 */
function onRefresh() { function handleRefresh() {
gridApi.query(); gridApi.query();
} }
@ -72,24 +72,33 @@ async function handleUpdateStatus(row: InfraJobApi.Job) {
: InfraJobStatusEnum.STOP; : InfraJobStatusEnum.STOP;
const statusText = status === InfraJobStatusEnum.NORMAL ? '启用' : '停用'; const statusText = status === InfraJobStatusEnum.NORMAL ? '启用' : '停用';
confirm({ await confirm(`确定${statusText} ${row.name} 吗?`);
content: `确定${statusText} ${row.name} 吗?`, const hideLoading = message.loading({
}).then(async () => { content: `正在${statusText}中...`,
await updateJobStatus(row.id as number, status); duration: 0,
//
message.success($t('ui.actionMessage.operationSuccess'));
onRefresh();
}); });
try {
await updateJobStatus(row.id!, status);
message.success($t('ui.actionMessage.operationSuccess'));
handleRefresh();
} finally {
hideLoading();
}
} }
/** 执行一次任务 */ /** 执行一次任务 */
async function handleTrigger(row: InfraJobApi.Job) { async function handleTrigger(row: InfraJobApi.Job) {
confirm({ await confirm(`确定执行一次 ${row.name} 吗?`);
content: `确定执行一次 ${row.name} 吗?`, const hideLoading = message.loading({
}).then(async () => { content: '正在执行中...',
await runJob(row.id as number); duration: 0,
message.success($t('ui.actionMessage.operationSuccess'));
}); });
try {
await runJob(row.id!);
message.success($t('ui.actionMessage.operationSuccess'));
} finally {
hideLoading();
}
} }
/** 跳转到任务日志 */ /** 跳转到任务日志 */
@ -105,12 +114,28 @@ async function handleDelete(row: InfraJobApi.Job) {
const hideLoading = message.loading({ const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.name]), content: $t('ui.actionMessage.deleting', [row.name]),
duration: 0, duration: 0,
key: 'action_process_msg',
}); });
try { try {
await deleteJob(row.id as number); await deleteJob(row.id!);
message.success($t('ui.actionMessage.deleteSuccess', [row.name])); message.success($t('ui.actionMessage.deleteSuccess', [row.name]));
onRefresh(); handleRefresh();
} finally {
hideLoading();
}
}
/** 批量删除任务 */
async function handleDeleteBatch() {
await confirm($t('ui.actionMessage.deleteBatchConfirm'));
const hideLoading = message.loading({
content: $t('ui.actionMessage.deletingBatch'),
duration: 0,
});
try {
await deleteJobList(checkedIds.value);
checkedIds.value = [];
message.success($t('ui.actionMessage.deleteSuccess'));
handleRefresh();
} finally { } finally {
hideLoading(); hideLoading();
} }
@ -121,23 +146,6 @@ function handleRowCheckboxChange({ records }: { records: InfraJobApi.Job[] }) {
checkedIds.value = records.map((item) => item.id!); checkedIds.value = records.map((item) => item.id!);
} }
/** 批量删除任务 */
async function handleDeleteBatch() {
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting'),
duration: 0,
key: 'action_process_msg',
});
try {
await deleteJobList(checkedIds.value);
checkedIds.value = [];
message.success($t('ui.actionMessage.deleteSuccess'));
onRefresh();
} finally {
hideLoading();
}
}
const [Grid, gridApi] = useVbenVxeGrid({ const [Grid, gridApi] = useVbenVxeGrid({
formOptions: { formOptions: {
schema: useGridFormSchema(), schema: useGridFormSchema(),
@ -181,7 +189,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
<DocAlert title="消息队列" url="https://doc.iocoder.cn/message-queue/" /> <DocAlert title="消息队列" url="https://doc.iocoder.cn/message-queue/" />
</template> </template>
<FormModal @success="onRefresh" /> <FormModal @success="handleRefresh" />
<DetailModal /> <DetailModal />
<Grid table-title=""> <Grid table-title="">
<template #toolbar-tools> <template #toolbar-tools>
@ -205,15 +213,15 @@ const [Grid, gridApi] = useVbenVxeGrid({
label: '执行日志', label: '执行日志',
type: 'primary', type: 'primary',
icon: 'lucide:history', icon: 'lucide:history',
auth: ['infra:job:export'], auth: ['infra:job:query'],
onClick: handleLog, onClick: () => handleLog(undefined),
}, },
{ {
label: '批量删除', label: $t('ui.actionTitle.deleteBatch'),
type: 'primary', type: 'primary',
danger: true, danger: true,
disabled: isEmpty(checkedIds),
icon: ACTION_ICON.DELETE, icon: ACTION_ICON.DELETE,
disabled: isEmpty(checkedIds),
auth: ['infra:job:delete'], auth: ['infra:job:delete'],
onClick: handleDeleteBatch, onClick: handleDeleteBatch,
}, },

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 { InfraJobApi } from '#/api/infra/job';
import { useAccess } from '@vben/access'; import { DICT_TYPE } from '@vben/constants';
import { DICT_TYPE, InfraJobStatusEnum } from '@vben/constants';
import { getDictOptions } from '@vben/hooks'; import { getDictOptions } from '@vben/hooks';
const { hasAccessByCodes } = useAccess();
/** 新增/修改的表单 */ /** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] { export function useFormSchema(): VbenFormSchema[] {
return [ return [
@ -34,10 +30,12 @@ export function useFormSchema(): VbenFormSchema[] {
component: 'Input', component: 'Input',
componentProps: { componentProps: {
placeholder: '请输入处理器的名字', placeholder: '请输入处理器的名字',
// readonly: ({ values }) => !!values.id, },
dependencies: {
triggerFields: ['id'],
disabled: (values) => !!values.id,
}, },
rules: 'required', rules: 'required',
// TODO @芋艿:在修改场景下,禁止调整
}, },
{ {
fieldName: 'handlerParam', fieldName: 'handlerParam',
@ -124,9 +122,7 @@ export function useGridFormSchema(): VbenFormSchema[] {
} }
/** 表格列配置 */ /** 表格列配置 */
export function useGridColumns<T = InfraJobApi.Job>( export function useGridColumns(): VxeTableGridOptions['columns'] {
onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
return [ return [
{ type: 'checkbox', width: 40 }, { type: 'checkbox', width: 40 },
{ {
@ -164,59 +160,10 @@ export function useGridColumns<T = InfraJobApi.Job>(
minWidth: 120, minWidth: 120,
}, },
{ {
field: 'operation',
title: '操作', title: '操作',
width: 280, width: 240,
fixed: 'right', fixed: 'right',
align: 'center', slots: { default: 'actions' },
cellRender: {
attrs: {
nameField: 'name',
nameTitle: '任务',
onClick: onActionClick,
},
name: 'CellOperation',
options: [
{
code: 'edit',
show: hasAccessByCodes(['infra:job:update']),
},
{
code: 'update-status',
text: '开启',
show: (row: any) =>
hasAccessByCodes(['infra:job:update']) &&
row.status === InfraJobStatusEnum.STOP,
},
{
code: 'update-status',
text: '暂停',
show: (row: any) =>
hasAccessByCodes(['infra:job:update']) &&
row.status === InfraJobStatusEnum.NORMAL,
},
{
code: 'trigger',
text: '执行',
show: hasAccessByCodes(['infra:job:trigger']),
},
// TODO @芋艿:增加一个“更多”选项
{
code: 'detail',
text: '详细',
show: hasAccessByCodes(['infra:job:query']),
},
{
code: 'log',
text: '日志',
show: hasAccessByCodes(['infra:job:query']),
},
{
code: 'delete',
show: hasAccessByCodes(['infra:job:delete']),
},
],
},
}, },
]; ];
} }

View File

@ -1,8 +1,5 @@
<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 { InfraJobApi } from '#/api/infra/job'; import type { InfraJobApi } from '#/api/infra/job';
import { ref } from 'vue'; import { ref } from 'vue';
@ -42,61 +39,68 @@ const [DetailModal, detailModalApi] = useVbenModal({
}); });
/** 刷新表格 */ /** 刷新表格 */
function onRefresh() { function handleRefresh() {
gridApi.query(); gridApi.query();
} }
/** 导出表格 */ /** 导出表格 */
async function onExport() { async function handleExport() {
const data = await exportJob(await gridApi.formApi.getValues()); const data = await exportJob(await gridApi.formApi.getValues());
downloadFileFromBlobPart({ fileName: '定时任务.xls', source: data }); downloadFileFromBlobPart({ fileName: '定时任务.xls', source: data });
} }
/** 创建任务 */ /** 创建任务 */
function onCreate() { function handleCreate() {
formModalApi.setData(null).open(); formModalApi.setData(null).open();
} }
/** 编辑任务 */ /** 编辑任务 */
function onEdit(row: InfraJobApi.Job) { function handleEdit(row: InfraJobApi.Job) {
formModalApi.setData(row).open(); formModalApi.setData(row).open();
} }
/** 查看任务详情 */ /** 查看任务详情 */
function onDetail(row: InfraJobApi.Job) { function handleDetail(row: InfraJobApi.Job) {
detailModalApi.setData({ id: row.id }).open(); detailModalApi.setData({ id: row.id }).open();
} }
/** 更新任务状态 */ /** 更新任务状态 */
async function onUpdateStatus(row: InfraJobApi.Job) { async function handleUpdateStatus(row: InfraJobApi.Job) {
const status = const status =
row.status === InfraJobStatusEnum.STOP row.status === InfraJobStatusEnum.STOP
? InfraJobStatusEnum.NORMAL ? InfraJobStatusEnum.NORMAL
: InfraJobStatusEnum.STOP; : InfraJobStatusEnum.STOP;
const statusText = status === InfraJobStatusEnum.NORMAL ? '启用' : '停用'; const statusText = status === InfraJobStatusEnum.NORMAL ? '启用' : '停用';
confirm({ await confirm(`确定${statusText} ${row.name} 吗?`);
content: `确定${statusText} ${row.name} 吗?`, const loadingInstance = ElLoading.service({
}).then(async () => { text: `正在${statusText}中...`,
await updateJobStatus(row.id as number, status);
//
ElMessage.success($t('ui.actionMessage.operationSuccess'));
onRefresh();
}); });
try {
await updateJobStatus(row.id!, status);
ElMessage.success($t('ui.actionMessage.operationSuccess'));
handleRefresh();
} finally {
loadingInstance.close();
}
} }
/** 执行一次任务 */ /** 执行一次任务 */
async function onTrigger(row: InfraJobApi.Job) { async function handleTrigger(row: InfraJobApi.Job) {
confirm({ await confirm(`确定执行一次 ${row.name} 吗?`);
content: `确定执行一次 ${row.name} 吗?`, const loadingInstance = ElLoading.service({
}).then(async () => { text: '正在执行中...',
await runJob(row.id as number);
ElMessage.success($t('ui.actionMessage.operationSuccess'));
}); });
try {
await runJob(row.id!);
ElMessage.success($t('ui.actionMessage.operationSuccess'));
} finally {
loadingInstance.close();
}
} }
/** 跳转到任务日志 */ /** 跳转到任务日志 */
function onLog(row?: InfraJobApi.Job) { function handleLog(row?: InfraJobApi.Job) {
push({ push({
name: 'InfraJobLog', name: 'InfraJobLog',
query: row?.id ? { id: row.id } : {}, query: row?.id ? { id: row.id } : {},
@ -104,26 +108,33 @@ function onLog(row?: InfraJobApi.Job) {
} }
/** 删除任务 */ /** 删除任务 */
async function onDelete(row: InfraJobApi.Job) { async function handleDelete(row: InfraJobApi.Job) {
const loadingInstance = ElLoading.service({ const loadingInstance = ElLoading.service({
text: $t('ui.actionMessage.deleting', [row.name]), text: $t('ui.actionMessage.deleting', [row.name]),
}); });
try { try {
await deleteJob(row.id as number); await deleteJob(row.id!);
ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.name])); ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.name]));
onRefresh(); handleRefresh();
} finally { } finally {
loadingInstance.close(); loadingInstance.close();
} }
} }
/** 批量删除任务 */ /** 批量删除任务 */
async function onDeleteBatch() { async function handleDeleteBatch() {
await confirm('确定要批量删除该任务吗?'); await confirm($t('ui.actionMessage.deleteBatchConfirm'));
await deleteJobList(checkedIds.value); const loadingInstance = ElLoading.service({
checkedIds.value = []; text: $t('ui.actionMessage.deletingBatch'),
ElMessage.success($t('ui.actionMessage.deleteSuccess')); });
onRefresh(); try {
await deleteJobList(checkedIds.value);
checkedIds.value = [];
ElMessage.success($t('ui.actionMessage.deleteSuccess'));
handleRefresh();
} finally {
loadingInstance.close();
}
} }
const checkedIds = ref<number[]>([]); const checkedIds = ref<number[]>([]);
@ -131,42 +142,12 @@ function handleRowCheckboxChange({ records }: { records: InfraJobApi.Job[] }) {
checkedIds.value = records.map((item) => item.id!); checkedIds.value = records.map((item) => item.id!);
} }
/** 表格操作按钮的回调函数 */
function onActionClick({ code, row }: OnActionClickParams<InfraJobApi.Job>) {
switch (code) {
case 'delete': {
onDelete(row);
break;
}
case 'detail': {
onDetail(row);
break;
}
case 'edit': {
onEdit(row);
break;
}
case 'log': {
onLog(row);
break;
}
case 'trigger': {
onTrigger(row);
break;
}
case 'update-status': {
onUpdateStatus(row);
break;
}
}
}
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: {
@ -182,6 +163,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
}, },
rowConfig: { rowConfig: {
keyField: 'id', keyField: 'id',
isHover: true,
}, },
toolbarConfig: { toolbarConfig: {
refresh: true, refresh: true,
@ -203,7 +185,7 @@ const [Grid, gridApi] = useVbenVxeGrid({
<DocAlert title="消息队列" url="https://doc.iocoder.cn/message-queue/" /> <DocAlert title="消息队列" url="https://doc.iocoder.cn/message-queue/" />
</template> </template>
<FormModal @success="onRefresh" /> <FormModal @success="handleRefresh" />
<DetailModal /> <DetailModal />
<Grid table-title=""> <Grid table-title="">
<template #toolbar-tools> <template #toolbar-tools>
@ -214,21 +196,21 @@ const [Grid, gridApi] = useVbenVxeGrid({
type: 'primary', type: 'primary',
icon: ACTION_ICON.ADD, icon: ACTION_ICON.ADD,
auth: ['infra:job:create'], auth: ['infra:job:create'],
onClick: onCreate, onClick: handleCreate,
}, },
{ {
label: $t('ui.actionTitle.export'), label: $t('ui.actionTitle.export'),
type: 'primary', type: 'primary',
icon: ACTION_ICON.DOWNLOAD, icon: ACTION_ICON.DOWNLOAD,
auth: ['infra:job:export'], auth: ['infra:job:export'],
onClick: onExport, onClick: handleExport,
}, },
{ {
label: '执行日志', label: '执行日志',
type: 'primary', type: 'primary',
icon: ACTION_ICON.MORE, icon: 'lucide:history',
auth: ['infra:job:query'], auth: ['infra:job:query'],
onClick: () => onLog(undefined), onClick: () => handleLog(undefined),
}, },
{ {
label: $t('ui.actionTitle.deleteBatch'), label: $t('ui.actionTitle.deleteBatch'),
@ -236,7 +218,73 @@ const [Grid, gridApi] = useVbenVxeGrid({
icon: ACTION_ICON.DELETE, icon: ACTION_ICON.DELETE,
disabled: isEmpty(checkedIds), disabled: isEmpty(checkedIds),
auth: ['infra:job:delete'], auth: ['infra:job:delete'],
onClick: onDeleteBatch, onClick: handleDeleteBatch,
},
]"
/>
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'primary',
link: true,
icon: ACTION_ICON.EDIT,
auth: ['infra:job:update'],
onClick: handleEdit.bind(null, row),
},
{
label: '开启',
type: 'primary',
link: true,
icon: 'lucide:circle-play',
auth: ['infra:job:update'],
ifShow: () => row.status === InfraJobStatusEnum.STOP,
onClick: handleUpdateStatus.bind(null, row),
},
{
label: '暂停',
type: 'primary',
link: true,
icon: 'lucide:circle-pause',
auth: ['infra:job:update'],
ifShow: () => row.status === InfraJobStatusEnum.NORMAL,
onClick: handleUpdateStatus.bind(null, row),
},
{
label: '执行',
type: 'primary',
link: true,
icon: 'lucide:clock-plus',
auth: ['infra:job:trigger'],
onClick: handleTrigger.bind(null, row),
},
]"
:drop-down-actions="[
{
label: $t('common.detail'),
type: 'primary',
link: true,
auth: ['infra:job:query'],
onClick: handleDetail.bind(null, row),
},
{
label: '日志',
type: 'primary',
link: true,
auth: ['infra:job:query'],
onClick: handleLog.bind(null, row),
},
{
label: $t('common.delete'),
type: 'danger',
link: true,
auth: ['infra:job:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
confirm: handleDelete.bind(null, row),
},
}, },
]" ]"
/> />