Skip to content

update:源列表增加自定义排序 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
update:源列表增加自定义排序
  • Loading branch information
yutons committed Dec 19, 2024
commit 970bd4de9bc099d6f8e44bd6a6513e8b26436ebd
27 changes: 26 additions & 1 deletion controllers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ async function generateSiteJSON(jsDir, requestHost, sub) {
};
sites.push(site);
}

// 自定义排序
sortParseJSON(sites, sub);

return {sites};
}

Expand Down Expand Up @@ -99,6 +103,27 @@ function getSubs(subFilePath) {
return subs
}

function sortParseJSON(sites, sub) {
let sorts = sub.sorts||'';
if (sorts) {
let sortKeys = sorts.split(',');
let sortMap = {};
sortKeys.forEach((key, index) => {
sortMap[key.trim()] = index;
});

sites.sort((a, b) => {
let aKey = a.key.split('_')[1].trim();
let bKey = b.key.split('_')[1].trim();

let aIndex = sortMap[aKey] !== undefined ? sortMap[aKey] : sortKeys.length + aKey.localeCompare(bKey);
let bIndex = sortMap[bKey] !== undefined ? sortMap[bKey] : sortKeys.length + bKey.localeCompare(aKey); // 为了确保稳定性,这里也使用 aKey 进行比较

return aIndex - bIndex;
});
}
}

export default (fastify, options, done) => {

fastify.get('/index', async (request, reply) => {
Expand All @@ -115,7 +140,7 @@ export default (fastify, options, done) => {
fastify.get('/config*', async (request, reply) => {
let t1 = (new Date()).getTime();
const query = request.query; // 获取 query 参数
const sub_code = query.sub || '';
const sub_code = query.sub || 'green';
const cfg_path = request.params['*']; // 捕获整个路径
try {
// 获取主机名,协议及端口
Expand Down
1 change: 1 addition & 0 deletions sub.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"name": "不含29",
"code": "green",
"reg": "密",
"sorts":"番茄小说[书],大象影视,腾云驾雾[官],央视大全[官]",
"mode": 1,
"status": 1
},
Expand Down