refactor(submitter): 优化分类加载逻辑,支持按架构获取CDN分类

1. 提取公共的CDN分类获取函数,增加 fallback 兜底逻辑
2. 新增支持amd64、arm64等更多架构类型
3. 为每个架构单独加载对应的分类列表,适配不同架构的分类差异
4. 更新日志打印参数,更清晰展示各架构的分类数量
This commit is contained in:
2026-07-16 14:27:26 +08:00
parent b06966c0a7
commit a8ea7bec34
+74 -10
View File
@@ -641,15 +641,7 @@ export function registerSubmitterHandlers(
} }
}); });
ipcMain.handle( const FALLBACK_CATEGORIES = [
"search-history-app",
async (_event, pkgname: string, useMirror = false) => {
try {
const baseUrl = useMirror
? "https://mirrors.sdu.edu.cn/spark-store"
: "https://spk-json.spark-app.store";
const storeArchs = ["store", "aarch64-store", "loong64-store"];
const categories = [
"chat", "chat",
"development", "development",
"games", "games",
@@ -663,19 +655,91 @@ export function registerSubmitterHandlers(
"tools", "tools",
"video", "video",
]; ];
async function fetchCategoriesFromCdn(
baseUrl: string,
arch: string,
): Promise<string[]> {
const url = `${baseUrl}/${arch}/categories.json`;
logger.info({ url }, "[Submitter] Fetching categories from CDN");
try {
const response = await fetch(url, {
headers: { "User-Agent": getUserAgent() },
});
if (!response.ok) {
logger.warn(
{ status: response.status, arch },
"[Submitter] Failed to fetch categories.json, using fallback",
);
return FALLBACK_CATEGORIES;
}
const json = await response.json();
if (json && typeof json === "object" && !Array.isArray(json)) {
const keys = Object.keys(json);
logger.info(
{ arch, categories: keys, count: keys.length },
"[Submitter] Categories loaded from CDN",
);
return keys.length > 0 ? keys : FALLBACK_CATEGORIES;
}
logger.warn(
{ arch },
"[Submitter] Unexpected categories.json format, using fallback",
);
return FALLBACK_CATEGORIES;
} catch (err) {
logger.warn(
{ err, arch },
"[Submitter] Error fetching categories.json, using fallback",
);
return FALLBACK_CATEGORIES;
}
}
ipcMain.handle(
"search-history-app",
async (_event, pkgname: string, useMirror = false) => {
try {
const baseUrl = useMirror
? "https://mirrors.sdu.edu.cn/spark-store"
: "https://spk-json.spark-app.store";
const storeArchs = [
"amd64-store",
"arm64-store",
"loong64-store",
"amd64-apm",
"arm64-apm",
"loong64-apm",
];
const results: HistoryAppInfo[] = []; const results: HistoryAppInfo[] = [];
// 为每个架构目录获取各自的分类列表(不同目录可能有不同分类,如 apm-extensions 只在 *-apm 下有)
const archCategoriesMap = new Map<string, string[]>();
for (const arch of storeArchs) {
const cats = await fetchCategoriesFromCdn(baseUrl, arch);
archCategoriesMap.set(arch, cats);
}
logger.info( logger.info(
"[Submitter] ============== SEARCH HISTORY APP START ==============", "[Submitter] ============== SEARCH HISTORY APP START ==============",
); );
logger.info( logger.info(
{ pkgname, useMirror, baseUrl, storeArchs, categories }, {
pkgname,
useMirror,
baseUrl,
storeArchs,
archCategoryCounts: Array.from(archCategoriesMap.entries()).map(
([a, c]) => ({ arch: a, categoryCount: c.length }),
),
},
"[Submitter] Search parameters", "[Submitter] Search parameters",
); );
const allPromises: Promise<void>[] = []; const allPromises: Promise<void>[] = [];
for (const arch of storeArchs) { for (const arch of storeArchs) {
const categories = archCategoriesMap.get(arch) || FALLBACK_CATEGORIES;
for (const category of categories) { for (const category of categories) {
const url = `${baseUrl}/${arch}/${category}/${pkgname}/app.json`; const url = `${baseUrl}/${arch}/${category}/${pkgname}/app.json`;
logger.info( logger.info(