定时任务增加置顶功能

This commit is contained in:
hanhh 2021-09-19 22:25:57 +08:00
parent 1cc54b5da6
commit aa08f489d7
3 changed files with 152 additions and 2 deletions

View File

@ -191,6 +191,42 @@ export default (app: Router) => {
},
);
route.put(
'/crons/pin',
celebrate({
body: Joi.array().items(Joi.string().required()),
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const cronService = Container.get(CronService);
const data = await cronService.pin(req.body);
return res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.put(
'/crons/unpin',
celebrate({
body: Joi.array().items(Joi.string().required()),
}),
async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const cronService = Container.get(CronService);
const data = await cronService.unPin(req.body);
return res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.get(
'/crons/import',
async (req: Request, res: Response, next: NextFunction) => {

View File

@ -115,6 +115,32 @@ export default class CronService {
});
}
public async pin(ids: string[]) {
return new Promise((resolve: any) => {
this.cronDb.update(
{ _id: { $in: ids } },
{ $set: { isPinned: 1 } },
{ multi: true },
async (err) => {
resolve();
},
);
});
}
public async unPin(ids: string[]) {
return new Promise((resolve: any) => {
this.cronDb.update(
{ _id: { $in: ids } },
{ $set: { isPinned: 0 } },
{ multi: true },
async (err) => {
resolve();
},
);
});
}
public async crontabs(searchText?: string): Promise<Crontab[]> {
let query = {};
if (searchText) {

View File

@ -25,6 +25,7 @@ import {
DeleteOutlined,
PauseCircleOutlined,
FieldTimeOutlined,
PushpinOutlined,
} from '@ant-design/icons';
import config from '@/utils/config';
import { PageContainer } from '@ant-design/pro-layout';
@ -49,6 +50,8 @@ enum OperationName {
'禁用',
'运行',
'停止',
'置顶',
'取消置顶',
}
enum OperationPath {
@ -56,6 +59,8 @@ enum OperationPath {
'disable',
'run',
'stop',
'pin',
'unpin',
}
const Crontab = ({ headerStyle, isPhone }: any) => {
@ -66,7 +71,16 @@ const Crontab = ({ headerStyle, isPhone }: any) => {
key: 'name',
align: 'center' as const,
render: (text: string, record: any) => (
<span>{record.name || record._id}</span>
<span>
{record.name || record._id}{' '}
{record.isPinned ? (
<span>
<PushpinOutlined />
</span>
) : (
''
)}
</span>
),
sorter: {
compare: (a: any, b: any) => a.name.localeCompare(b.name),
@ -237,7 +251,12 @@ const Crontab = ({ headerStyle, isPhone }: any) => {
data.data.sort((a: any, b: any) => {
const sortA = a.isDisabled ? 4 : a.status;
const sortB = b.isDisabled ? 4 : b.status;
return CrontabSort[sortA] - CrontabSort[sortB];
a.isPinned = a.isPinned ? a.isPinned : 0;
b.isPinned = b.isPinned ? b.isPinned : 0;
if (a.isPinned === b.isPinned) {
return CrontabSort[sortA] - CrontabSort[sortB];
}
return b.isPinned - a.isPinned;
}),
);
setCurrentPage(1);
@ -403,6 +422,50 @@ const Crontab = ({ headerStyle, isPhone }: any) => {
});
};
const pinOrunPinCron = (record: any, index: number) => {
Modal.confirm({
title: `确认${record.isPinned === 1 ? '取消置顶' : '置顶'}`,
content: (
<>
{record.isPinned === 1 ? '取消置顶' : '置顶'}
{' '}
<Text style={{ wordBreak: 'break-all' }} type="warning">
{record.name}
</Text>{' '}
</>
),
onOk() {
request
.put(
`${config.apiPrefix}crons/${
record.isPinned === 1 ? 'unpin' : 'pin'
}`,
{
data: [record._id],
},
)
.then((data: any) => {
if (data.code === 200) {
const newStatus = record.isPinned === 1 ? 0 : 1;
const result = [...value];
const i = result.findIndex((x) => x._id === record._id);
result.splice(i, 1, {
...record,
isPinned: newStatus,
});
setValue(result);
} else {
message.error(data);
}
});
},
onCancel() {
console.log('Cancel');
},
});
};
const MoreBtn: React.FC<{
record: any;
index: number;
@ -432,6 +495,14 @@ const Crontab = ({ headerStyle, isPhone }: any) => {
</Menu.Item>
)}
<Menu.Item
key="pinOrunPin"
icon={
record.isPinned === 1 ? <StopOutlined /> : <PushpinOutlined />
}
>
{record.isPinned === 1 ? '取消置顶' : '置顶'}
</Menu.Item>
</Menu>
}
>
@ -452,6 +523,9 @@ const Crontab = ({ headerStyle, isPhone }: any) => {
case 'delete':
delCron(record, index);
break;
case 'pinOrunPin':
pinOrunPinCron(record, index);
break;
default:
break;
}
@ -626,6 +700,20 @@ const Crontab = ({ headerStyle, isPhone }: any) => {
<Button type="primary" onClick={() => operateCrons(3)}>
</Button>
<Button
type="primary"
onClick={() => operateCrons(4)}
style={{ marginLeft: 8, marginRight: 8 }}
>
</Button>
<Button
type="primary"
onClick={() => operateCrons(5)}
style={{ marginLeft: 8, marginRight: 8 }}
>
</Button>
<span style={{ marginLeft: 8 }}>
<a>{selectedRowIds?.length}</a>