mirror of
https://github.com/YuleBest/FontMM.git
synced 2026-12-04 03:11:48 +08:00
fix(webui): 修正小米字体搜索的响应解析, 搜索此前一直无结果
原解析读 data.data.items, 与接口实际结构不符 —— 真实响应为 apiData.cards[].items[].schema.clicks[], 字段是 title/link/pic。 实测同一响应下原代码解析出 0 条, 界面恒显示「未找到相关字体」。 - 抽出 parseSearchResponse() 按真实结构解析, 并跳过无 title/link 的 占位项 (无结果时接口返回 type=noResultMessage 的 item) - 分页改用接口的 hasMore: 接口每页固定 12 条, 原按「>=20 条才有下一页」 估算总数会误判总页数 (第一页即显示 100 页) - 移除 it.time / it.certified 字段引用 (接口不返回) - dev mock 同步为真实结构, 并让 page=1 返回空页以便验证下一页按钮 已用真实接口响应 (12 条) 验证解析结果正确。
This commit is contained in:
1 parent
403d9f1391
commit
f7b9893d12
2 files changed
+64
-14
No files matched your search
+20
-3
@@ -100,19 +100,36 @@ async function mockExec(command: string): Promise<ExecResult> {
|
||||
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',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
|
||||
+44
-11
@@ -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, '>').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 = `
|
||||
<div class="mi-item-info">
|
||||
<div class="mi-item-title"></div>
|
||||
<div class="mi-item-sub">${escapeHtml(it.certified ? '官方认证 · ' : '')}${time}</div>
|
||||
</div>
|
||||
${pic ? `<img class="mi-item-pic" src="${escapeHtml(pic)}" alt="" loading="lazy" onerror="this.remove()">` : ''}
|
||||
<md-icon-button class="mi-item-dl" aria-label="下载">
|
||||
<md-icon>download</md-icon>
|
||||
</md-icon-button>
|
||||
`;
|
||||
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 = `
|
||||
<md-text-button id="mi-prev" ${pageNo === 0 ? 'disabled' : ''}>上一页</md-text-button>
|
||||
<span class="mi-page">第 ${pageNo + 1} / ${pages} 页</span>
|
||||
<md-text-button id="mi-next" ${pageNo + 1 >= pages ? 'disabled' : ''}>下一页</md-text-button>
|
||||
<span class="mi-page">第 ${pageNo + 1} 页</span>
|
||||
<md-text-button id="mi-next" ${hasMore ? '' : 'disabled'}>下一页</md-text-button>
|
||||
`;
|
||||
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 {
|
||||
|
||||
Reference in new issue
Block a user