增加排序和过滤功能

pull/51/head
qiaoronggui 2023-12-09 20:29:15 +08:00
parent bb67f7f2a7
commit a9d8d539f1
3 changed files with 48 additions and 1 deletions

View File

@ -33,4 +33,9 @@ window.Config = {
url: 'https://abo.xyz/'
},
],
// 根据导航栏菜单排序
SortNavi: false,
// 根据导航栏菜单过滤没有的站点
FilterNavi: false,
};

40
src/common/sort.js Normal file
View File

@ -0,0 +1,40 @@
const { Navi, SortNavi, FilterNavi } = window.Config;
export function Sort (monitors = []) {
if (SortNavi && FilterNavi) {
return sortAndFilter(monitors);
}
// 如果启用了排序
if (SortNavi) {
return sort(monitors);
}
// 如果启用了过滤
if (FilterNavi) {
return filter(monitors);
}
// 如果都没有启用,则直接返回 monitors
return monitors;
}
const sort = (monitors = []) => {
const urlIndexMap = new Map(Navi.map((navItem, index) => [navItem.url, index]));
monitors.sort((a, b) => {
const indexA = urlIndexMap.get(a.url);
const indexB = urlIndexMap.get(b.url);
return (indexA !== undefined && indexB !== undefined) ? indexA - indexB : (indexA !== undefined) ? -1 : (indexB !== undefined) ? 1 : 0;
});
return monitors;
}
const filter = (monitors = []) => {
return monitors.filter(item => Navi.some(navItem => navItem.url === item.url));
};
const sortAndFilter = (monitors = []) => {
return sort(filter(monitors));
};

View File

@ -1,6 +1,7 @@
import axios from 'axios';
import dayjs from 'dayjs';
import { formatNumber } from './helper';
import { Sort } from './sort';
export async function GetMonitors(apikey, days) {
@ -27,7 +28,8 @@ export async function GetMonitors(apikey, days) {
const response = await axios.post('https://cors.status.org.cn/uptimerobot/v2/getMonitors', postdata, { timeout: 10000 });
if (response.data.stat !== 'ok') throw response.data.error;
return response.data.monitors.map((monitor) => {
const processed = Sort(response.data.monitors);
return processed.map((monitor) => {
const ranges = monitor.custom_uptime_ranges.split('-');
const average = formatNumber(ranges.pop());