增加cron搜索

This commit is contained in:
whyour 2021-04-04 14:27:09 +08:00
parent f46e20fc81
commit a7d652c86e
3 changed files with 34 additions and 5 deletions

View File

@ -15,7 +15,9 @@ export default (app: Router) => {
const logger: Logger = Container.get('logger');
try {
const cookieService = Container.get(CronService);
const data = await cookieService.crontabs();
const data = await cookieService.crontabs(
req.query.searchValue as string,
);
return res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);

View File

@ -48,10 +48,24 @@ export default class CronService {
await this.set_crontab();
}
public async crontabs(): Promise<Crontab[]> {
public async crontabs(searchText?: string): Promise<Crontab[]> {
let query = {};
if (searchText) {
const reg = new RegExp(searchText);
query = {
$or: [
{
name: reg,
},
{
command: reg,
},
],
};
}
return new Promise((resolve) => {
this.cronDb
.find({})
.find(query)
.sort({ created: -1 })
.exec((err, docs) => {
resolve(docs);

View File

@ -10,6 +10,7 @@ import {
Dropdown,
Menu,
Typography,
Input,
} from 'antd';
import {
ClockCircleOutlined,
@ -29,6 +30,7 @@ import { request } from '@/utils/http';
import CronModal from './modal';
const { Text } = Typography;
const { Search } = Input;
enum CrontabStatus {
'running',
@ -122,10 +124,10 @@ const Crontab = () => {
const [isModalVisible, setIsModalVisible] = useState(false);
const [editedCron, setEditedCron] = useState();
const getCrons = () => {
const getCrons = (text: string = '') => {
setLoading(true);
request
.get(`${config.apiPrefix}crons`)
.get(`${config.apiPrefix}crons?searchValue=${text}`)
.then((data: any) => {
setValue(data.data.sort((a: any, b: any) => a.status - b.status));
})
@ -310,6 +312,10 @@ const Crontab = () => {
}
};
const onSearch = (value: string) => {
getCrons(value);
};
useEffect(() => {
if (document.body.clientWidth < 768) {
setWdith('auto');
@ -329,6 +335,13 @@ const Crontab = () => {
title="定时任务"
loading={loading}
extra={[
<Search
placeholder="请输入名称或者关键词"
style={{ width: 'auto' }}
enterButton
loading={loading}
onSearch={onSearch}
/>,
<Button key="2" type="primary" onClick={() => addCron()}>
</Button>,