修改定时任务分页数据

This commit is contained in:
whyour 2022-08-20 20:05:35 +08:00
parent f3de8435f1
commit 102e447f78
3 changed files with 70 additions and 32 deletions

View File

@ -9,17 +9,28 @@ const route = Router();
export default (app: Router) => { export default (app: Router) => {
app.use('/crons', route); app.use('/crons', route);
route.get('/', async (req: Request, res: Response, next: NextFunction) => { route.get(
const logger: Logger = Container.get('logger'); '/',
try { celebrate({
const cronService = Container.get(CronService); query: Joi.object({
const data = await cronService.crontabs(req.query.searchValue as string); searchText: Joi.string().required().allow(''),
return res.send({ code: 200, data }); page: Joi.string().required(),
} catch (e) { size: Joi.string().required(),
logger.error('🔥 error: %o', e); t: Joi.string().required(),
return next(e); }),
} }),
}); async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger');
try {
const cronService = Container.get(CronService);
const data = await cronService.crontabs(req.query as any);
return res.send({ code: 200, data });
} catch (e) {
logger.error('🔥 error: %o', e);
return next(e);
}
},
);
route.post( route.post(
'/', '/',

View File

@ -113,7 +113,15 @@ export default class CronService {
} }
} }
public async crontabs(searchText?: string): Promise<Crontab[]> { public async crontabs(params?: {
searchText: string;
page: string;
size: string;
}): Promise<{ data: Crontab[]; total: number }> {
const searchText = params?.searchText;
const page = Number(params?.page || '0');
const size = Number(params?.size || '0');
let query = {}; let query = {};
if (searchText) { if (searchText) {
const textArray = searchText.split(':'); const textArray = searchText.split(':');
@ -158,12 +166,18 @@ export default class CronService {
break; break;
} }
} }
let condition: any = {
where: query,
order: [['createdAt', 'DESC']],
};
if (page && size) {
condition.offset = (page - 1) * size;
condition.limit = size;
}
try { try {
const result = await CrontabModel.findAll({ const result = await CrontabModel.findAll(condition);
where: query, const count = await CrontabModel.count();
order: [['createdAt', 'DESC']], return { data: result, total: count };
});
return result as any;
} catch (error) { } catch (error) {
throw error; throw error;
} }
@ -441,7 +455,7 @@ export default class CronService {
private async set_crontab(needReloadSchedule: boolean = false) { private async set_crontab(needReloadSchedule: boolean = false) {
const tabs = await this.crontabs(); const tabs = await this.crontabs();
var crontab_string = ''; var crontab_string = '';
tabs.forEach((tab) => { tabs.data.forEach((tab) => {
const _schedule = tab.schedule && tab.schedule.split(/ +/); const _schedule = tab.schedule && tab.schedule.split(/ +/);
if (tab.isDisabled === 1 || _schedule!.length !== 5) { if (tab.isDisabled === 1 || _schedule!.length !== 5) {
crontab_string += '# '; crontab_string += '# ';

View File

@ -346,12 +346,14 @@ const Crontab = ({ headerStyle, isPhone, theme }: any) => {
const [isLogModalVisible, setIsLogModalVisible] = useState(false); const [isLogModalVisible, setIsLogModalVisible] = useState(false);
const [logCron, setLogCron] = useState<any>(); const [logCron, setLogCron] = useState<any>();
const [selectedRowIds, setSelectedRowIds] = useState<string[]>([]); const [selectedRowIds, setSelectedRowIds] = useState<string[]>([]);
const [currentPage, setCurrentPage] = useState(1); const [pageConf, setPageConf] = useState<{ page: number; size: number }>(
const [pageSize, setPageSize] = useState(20); {} as any,
);
const [tableScrollHeight, setTableScrollHeight] = useState<number>(); const [tableScrollHeight, setTableScrollHeight] = useState<number>();
const [isDetailModalVisible, setIsDetailModalVisible] = useState(false); const [isDetailModalVisible, setIsDetailModalVisible] = useState(false);
const [detailCron, setDetailCron] = useState<any>(); const [detailCron, setDetailCron] = useState<any>();
const [searchValue, setSearchValue] = useState(''); const [searchValue, setSearchValue] = useState('');
const [total, setTotal] = useState<number>();
const goToScriptManager = (record: any) => { const goToScriptManager = (record: any) => {
const cmd = record.command.split(' ') as string[]; const cmd = record.command.split(' ') as string[];
@ -374,10 +376,13 @@ const Crontab = ({ headerStyle, isPhone, theme }: any) => {
const getCrons = () => { const getCrons = () => {
setLoading(true); setLoading(true);
request request
.get(`${config.apiPrefix}crons?searchValue=${searchText}`) .get(
.then((data: any) => { `${config.apiPrefix}crons?searchText=${searchText}&page=${pageConf.page}&size=${pageConf.size}`,
)
.then((_data: any) => {
const { data, total } = _data.data;
setValue( setValue(
data.data data
.sort((a: any, b: any) => { .sort((a: any, b: any) => {
const sortA = const sortA =
a.isPinned && a.status !== 0 a.isPinned && a.status !== 0
@ -403,7 +408,7 @@ const Crontab = ({ headerStyle, isPhone, theme }: any) => {
}; };
}), }),
); );
setCurrentPage(1); setTotal(total);
}) })
.finally(() => setLoading(false)); .finally(() => setLoading(false));
}; };
@ -797,9 +802,8 @@ const Crontab = ({ headerStyle, isPhone, theme }: any) => {
}; };
const onPageChange = (page: number, pageSize: number | undefined) => { const onPageChange = (page: number, pageSize: number | undefined) => {
setCurrentPage(page); setPageConf({ page, size: pageSize as number });
setPageSize(pageSize as number); localStorage.setItem('pageSize', String(pageSize));
localStorage.setItem('pageSize', pageSize + '');
}; };
const getRowClassName = (record: any, index: number) => { const getRowClassName = (record: any, index: number) => {
@ -814,11 +818,20 @@ const Crontab = ({ headerStyle, isPhone, theme }: any) => {
}, [logCron]); }, [logCron]);
useEffect(() => { useEffect(() => {
getCrons(); setPageConf({ ...pageConf, page: 1 });
}, [searchText]); }, [searchText]);
useEffect(() => { useEffect(() => {
setPageSize(parseInt(localStorage.getItem('pageSize') || '20')); if (pageConf.page && pageConf.size) {
getCrons();
}
}, [pageConf]);
useEffect(() => {
setPageConf({
page: 1,
size: parseInt(localStorage.getItem('pageSize') || '2'),
});
setTimeout(() => { setTimeout(() => {
setTableScrollHeight(getTableScroll()); setTableScrollHeight(getTableScroll());
}); });
@ -906,12 +919,12 @@ const Crontab = ({ headerStyle, isPhone, theme }: any) => {
<Table <Table
columns={columns} columns={columns}
pagination={{ pagination={{
current: currentPage, current: pageConf.page,
onChange: onPageChange, onChange: onPageChange,
pageSize: pageSize, pageSize: pageConf.size,
showSizeChanger: true, showSizeChanger: true,
simple: isPhone, simple: isPhone,
defaultPageSize: 20, total,
showTotal: (total: number, range: number[]) => showTotal: (total: number, range: number[]) =>
`${range[0]}-${range[1]} 条/总共 ${total}`, `${range[0]}-${range[1]} 条/总共 ${total}`,
pageSizeOptions: [20, 100, 500, 1000] as any, pageSizeOptions: [20, 100, 500, 1000] as any,