diff --git a/web/src/ksu.ts b/web/src/ksu.ts index c314c6b..6714a20 100644 --- a/web/src/ksu.ts +++ b/web/src/ksu.ts @@ -100,19 +100,36 @@ async function mockExec(command: string): Promise { return slow('name=KernelSU MetaModule\nversion=v1.0\nauthor=KernelSU'); } - // 小米主题字体工具: 搜索/详情/下载 (模拟) + // 小米主题字体工具: 搜索 (模拟) + // 结构与真实接口一致: apiData.cards[].items[].schema.clicks[] 携带 title/link/pic, + // apiData.hasMore 标识是否还有下一页 if (command.includes('thm.market.intl.xiaomi.com')) { + // 第二页返回空, 用于验证「下一页」按钮状态 + if (/[?&]page=1\b/.test(command)) { + return slow(JSON.stringify({ apiData: { hasMore: false, cards: [] } })); + } return slow( JSON.stringify({ apiData: { + hasMore: true, cards: [ { items: [ { + type: 'endlessList', schema: { + type: 'Font', clicks: [ - { title: 'MiSans Global (模拟)', link: 'mock-font-1' }, - { title: 'OPPO Sans (模拟)', link: 'mock-font-2' }, + { + title: 'MiSans Global (模拟)', + link: 'mock-font-1', + pic: 'ThemeMarket/mock-pic-1', + }, + { + title: 'OPPO Sans (模拟)', + link: 'mock-font-2', + pic: 'ThemeMarket/mock-pic-2', + }, ], }, }, diff --git a/web/src/tools.ts b/web/src/tools.ts index a186794..a58b1fa 100644 --- a/web/src/tools.ts +++ b/web/src/tools.ts @@ -104,6 +104,36 @@ const SEARCH_API = 'https://thm.market.intl.xiaomi.com/thm/search/npage?category // 格式: thumbnail/{format}/{尺寸参数}/{pic}, 如 thumbnail/webp/w120q70/ThemeMarket/xxx const PIC_BASE = 'https://t17.market.mi-img.com/thumbnail/webp/w120q70/'; +/** 搜索结果条目 (来自 apiData.cards[].items[].schema.clicks[]) */ +interface MiFontItem { + title: string; + /** 主题 ID, 详情 API 用它换取下载地址 */ + link: string; + /** 相对图片路径 */ + pic: string; +} + +/** + * 解析搜索响应。 + * 真实结构: apiData.cards[].items[].schema.clicks[] —— 字段 title/link/pic; + * 找不到结果时返回 noResultMessage 类型的 item (clicks 为空)。 + */ +function parseSearchResponse(data: any): MiFontItem[] { + const cards: any[] = data?.apiData?.cards ?? []; + const items: MiFontItem[] = []; + for (const card of cards) { + for (const item of card?.items ?? []) { + for (const click of item?.schema?.clicks ?? []) { + const title = String(click?.title ?? '').trim(); + const link = String(click?.link ?? '').trim(); + if (!title || !link) continue; + items.push({ title, link, pic: String(click?.pic ?? '') }); + } + } + } + return items; +} + export const MiFontToolDef: ToolDef = { id: 'mi-font', title: '下载国际版小米主题字体', @@ -141,36 +171,38 @@ export const MiFontToolDef: ToolDef = { const escapeHtml = (s: string): string => s.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); - const renderItems = (items: any[]): void => { + const renderItems = (items: MiFontItem[]): void => { resultEl.textContent = ''; for (const it of items) { const card = document.createElement('div'); card.className = 'mi-item'; const pic = it.pic ? `${PIC_BASE}${it.pic}` : ''; - const time = it.time ? new Date(it.time).toLocaleDateString() : ''; card.innerHTML = `
-
${escapeHtml(it.certified ? '官方认证 · ' : '')}${time}
${pic ? `` : ''} download `; - card.querySelector('.mi-item-title')!.textContent = it.title ?? ''; + card.querySelector('.mi-item-title')!.textContent = it.title; resultEl.appendChild(card); } }; - const renderPager = (total: number): void => { - const pages = Math.max(1, Math.ceil(total / 20)); + // 分页: 接口每页固定返回 12 条并以 hasMore 标识是否还有下一页。 + // 之前按「返回条数 >= 20 才算有下一页」估算总数, 与真实分页规则不符, + // 会导致第一页就显示 100 页。改为记录 hasMore, 只区分「有/无下一页」。 + let hasMore = false; + + const renderPager = (): void => { const nav = document.createElement('div'); nav.className = 'mi-pager'; nav.innerHTML = ` 上一页 - 第 ${pageNo + 1} / ${pages} 页 - = pages ? 'disabled' : ''}>下一页 + 第 ${pageNo + 1} 页 + 下一页 `; resultEl.appendChild(nav); nav.querySelector('#mi-prev')?.addEventListener('click', () => { @@ -180,7 +212,7 @@ export const MiFontToolDef: ToolDef = { } }); nav.querySelector('#mi-next')?.addEventListener('click', () => { - if (pageNo + 1 < pages) { + if (hasMore) { pageNo++; void search(pageNo); } @@ -198,13 +230,14 @@ export const MiFontToolDef: ToolDef = { const { errno, stdout } = await exec(`curl -s ${shellQuote(url)}`); if (errno !== 0) throw new Error('curl 执行失败'); const data = JSON.parse(stdout); - const items = data?.data?.items ?? []; + const items = parseSearchResponse(data); if (items.length === 0) { resultEl.textContent = '未找到相关字体, 换个关键词试试'; return; } + hasMore = Boolean(data?.apiData?.hasMore); renderItems(items); - renderPager(items.length >= 20 ? 100 : items.length); + renderPager(); } catch (e) { resultEl.textContent = `搜索失败: ${(e as Error).message}`; } finally {