feat: 分类接口替换为api 接口

This commit is contained in:
R524809
2026-10-16 17:33:24 +08:00
parent eddd7d75e2
commit 8de06ce66b
12 changed files with 1086 additions and 283 deletions
+160 -271
View File
@@ -1,44 +1,19 @@
import { appSharePageMethods } from '../../base/pageMixin';
import { CATEGORY_LIST_WITH_ALL } from '../../core/data/categories';
import { parseMiniProgramUrl, takePendingCategoryTabId } from '../../utils/index';
import { takePendingCategoryTabId } from '../../utils/index';
import tracker from '../../utils/tracker';
import { CATEGORY_DATA, type CategoryItem } from './category.data';
type CategoryTab = {
id: string;
name: string;
icon: string;
};
type CategoryDataset = {
searchPlaceholder: string;
categories: Array<{
id: string;
name: string;
icon: string;
items: CategoryItem[];
}>;
};
const DIFFICULTY_LABELS: Record<number, string> = {
1: '入门',
2: '基础',
3: '进阶',
4: '挑战',
};
const DYNAMIC_TABS: CategoryTab[] = [
{ id: 'hot', name: '热门推荐', icon: '🔥' },
{ id: 'top-downloads', name: '下载最多', icon: '📥' },
];
const SIDEBAR_TABS: CategoryTab[] = [
{ id: 'all', name: '全部', icon: '📋' },
...DYNAMIC_TABS,
...CATEGORY_LIST_WITH_ALL.filter((c) => c.id !== 'all').map(
({ id, name, icon }) => ({ id, name, icon }),
),
];
import { ApiRequestError } from '../../services/api-request';
import {
getWorksheet,
listAllWorksheets,
listCategories,
} from '../../services/content-api';
import {
BASE_TABS,
buildCategoryDataset,
getCategoryItems,
type CategoryDataset,
type CategoryItem,
} from './category.model';
const TAB_BAR_PATHS = new Set([
'/pages/home/home',
@@ -47,182 +22,42 @@ const TAB_BAR_PATHS = new Set([
'/pages/favorites/favorites',
'/pages/profile/profile',
]);
// Page-level mutable state (shared across methods)
let _categoryData: CategoryDataset | null = null;
// --- 以下仅为本地调试:拉取分类数据后查看各 worksheet 的 downloads / likes,非业务逻辑 ---
function debugLogWorksheetStatsAfterCategoryLoad(
source:
| { kind: 'cloud'; rows: Record<string, any>[] }
| { kind: 'static'; data: CategoryDataset },
) {
let map: Record<string, { downloads: number; likes: number }> = {};
if (source.kind === 'cloud') {
for (const raw of source.rows) {
const id = String(raw._id || '').trim();
if (!id) continue;
map[id] = {
downloads: Number(raw.downloads) || 0,
likes: Number(raw.likes) || 0,
};
}
} else {
for (const group of source.data.categories) {
for (const item of group.items) {
map[item.id] = {
downloads: Number(item.downloads) || 0,
likes: Number(item.likes) || 0,
};
}
}
}
console.log('worksheetStatsById', JSON.stringify(map, null, 2));
}
// --- 调试块结束 ---
function toContentDate(raw: Record<string, any>): string {
const ts = raw.contentUpdatedAt || raw.createdAt;
return ts
? new Date(ts).toISOString().slice(0, 10)
: new Date().toISOString().slice(0, 10);
}
/** 将云端 worksheet 原始数据转换为 CategoryItem */
function toDisplayItem(raw: Record<string, any>): CategoryItem {
const difficulty = (Number(raw.difficulty) || 2) as 1 | 2 | 3 | 4;
const ageMin = Number(raw.ageMin) || 0;
const ageMax = Number(raw.ageMax) || 0;
return {
id: String(raw._id || ''),
title: String(raw.title || ''),
subtitle: String(raw.subtitle || ''),
previewImg: String(raw.previewImg || ''),
ageBand: `${ageMin}-${ageMax}岁`,
ageMin,
ageMax,
difficulty,
difficultyLabel: DIFFICULTY_LABELS[difficulty] || '基础',
path: String(raw.path || ''),
available: true,
likes: Number(raw.likes) || 0,
downloads: Number(raw.downloads) || 0,
date: toContentDate(raw),
updatedAt: raw.updatedAt ? String(raw.updatedAt) : '',
};
}
/** 排序:updatedAt 最新的 2 个置顶,其余按 downloads 降序 */
function sortWorksheets(items: CategoryItem[]): CategoryItem[] {
const byTime = [...items].sort(
(a, b) =>
new Date(b.updatedAt || '').getTime() -
new Date(a.updatedAt || '').getTime(),
);
const recent = byTime.slice(0, 2);
const recentIds = new Set(recent.map((w) => w.id));
const rest = byTime
.filter((w) => !recentIds.has(w.id))
.sort((a, b) => (b.downloads || 0) - (a.downloads || 0));
return [...recent, ...rest];
}
/** 将云端 worksheet 数组按分类分组,构建 CategoryDataset */
function buildCategoryDataFromCloud(
rawList: Record<string, any>[],
): CategoryDataset {
const itemsByCategory: Record<string, CategoryItem[]> = {};
for (const raw of rawList) {
const item = toDisplayItem(raw);
const cat = String(raw.category || '');
if (!itemsByCategory[cat]) itemsByCategory[cat] = [];
itemsByCategory[cat].push(item);
}
const categories = CATEGORY_LIST_WITH_ALL.filter((c) => c.id !== 'all').map(
(cat) => ({
id: cat.id,
name: cat.name,
icon: cat.icon,
items: sortWorksheets(itemsByCategory[cat.id] || []),
}),
);
return { searchPlaceholder: '搜索练习纸...', categories };
}
function buildAllItems(): CategoryItem[] {
if (!_categoryData) return [];
const items: CategoryItem[] = [];
for (const group of _categoryData.categories) {
for (const item of group.items) {
items.push({ ...item });
}
}
return sortWorksheets(items);
}
const DYNAMIC_LIMIT = 12;
function buildHotItems(): CategoryItem[] {
const all = buildAllItems();
return [...all]
.sort((a, b) => (b.likes || 0) - (a.likes || 0))
.slice(0, DYNAMIC_LIMIT);
}
function buildTopDownloadsItems(): CategoryItem[] {
const all = buildAllItems();
return [...all]
.sort((a, b) => (b.downloads || 0) - (a.downloads || 0))
.slice(0, DYNAMIC_LIMIT);
}
function getItemsByCategory(categoryId: string): CategoryItem[] {
if (!_categoryData) return [];
if (categoryId === 'all') return buildAllItems();
if (categoryId === 'hot') return buildHotItems();
if (categoryId === 'top-downloads') return buildTopDownloadsItems();
const group = _categoryData.categories.find((c) => c.id === categoryId);
if (!group) return [];
return group.items.map((item) => ({ ...item }));
}
function filterByKeyword(
items: CategoryItem[],
keyword: string,
): CategoryItem[] {
if (!keyword) return items;
const kw = keyword.toLowerCase();
return items.filter(
(item) =>
item.title.toLowerCase().includes(kw) ||
item.subtitle.toLowerCase().includes(kw),
);
}
// Skeleton placeholder items for loading state
const SKELETON_ITEMS = Array.from({ length: 6 }, (_, i) => ({
id: `skeleton-${i}`,
title: '\u00A0',
subtitle: '\u00A0',
skeleton: true,
}));
const SKELETON_ITEMS: CategoryItem[] = Array.from(
{ length: 6 },
(_, index) => ({
id: `skeleton-${index}`,
title: '\u00a0',
subtitle: '\u00a0',
icon: '',
previewImg: '',
ageBand: '',
difficultyLabel: '',
path: '',
available: false,
likes: '',
downloads: '',
date: '',
updatedAt: '',
skeleton: true,
}),
);
Page({
data: {
searchPlaceholder: '搜索练习纸...',
searchKeyword: '',
sidebarTabs: SIDEBAR_TABS,
sidebarTabs: BASE_TABS,
activeCategoryId: 'all',
displayItems: SKELETON_ITEMS as any[],
displayItems: SKELETON_ITEMS,
scrollIntoViewId: '',
loading: true,
errorMessage: '',
},
_defaultCategoryId: 'all',
_categoryData: null as CategoryDataset | null,
_loadVersion: 0,
_opening: false,
onLoad(options: Record<string, string>) {
tracker.reportPageView('pages/category/category');
@@ -233,58 +68,92 @@ Page({
},
onShow() {
_categoryData = null;
this.setData({ displayItems: SKELETON_ITEMS, loading: true });
void this.loadWorksheets(takePendingCategoryTabId() ?? undefined);
},
async loadWorksheets(overrideCategoryId?: string | null) {
try {
const res = await wx.cloud.callFunction({
name: 'worksheetsQuery',
data: { status: 'active' },
});
const result = (res?.result as any) || {};
if (!result.success) throw new Error(result.message || '查询失败');
onHide() {
this._loadVersion++;
},
const rows = result.data || [];
console.log('rows', rows);
_categoryData = buildCategoryDataFromCloud(rows);
debugLogWorksheetStatsAfterCategoryLoad({ kind: 'cloud', rows });
} catch {
_categoryData = CATEGORY_DATA;
debugLogWorksheetStatsAfterCategoryLoad({
kind: 'static',
data: CATEGORY_DATA,
onUnload() {
this._loadVersion++;
},
/** 每次进入重新读取公开目录与完整分页;失败清空内容,不回退静态数据或旧云服务。 */
async loadWorksheets(overrideCategoryId?: string) {
const version = ++this._loadVersion;
this._categoryData = null;
this.setData({
displayItems: SKELETON_ITEMS,
sidebarTabs: BASE_TABS,
loading: true,
errorMessage: '',
});
try {
const [categories, worksheets] = await Promise.all([
listCategories(),
listAllWorksheets(),
]);
if (version !== this._loadVersion) return;
const dataset = buildCategoryDataset(categories, worksheets);
const desiredId =
overrideCategoryId ||
this.data.activeCategoryId ||
this._defaultCategoryId;
const activeId = dataset.sidebarTabs.some(
(tab) => tab.id === desiredId,
)
? desiredId
: 'all';
this._categoryData = dataset;
this.setData({
loading: false,
sidebarTabs: dataset.sidebarTabs,
activeCategoryId: activeId,
displayItems: getCategoryItems(
dataset,
activeId,
this.data.searchKeyword,
),
scrollIntoViewId: '',
errorMessage: '',
});
} catch (error) {
if (version !== this._loadVersion) return;
this._categoryData = null;
this.setData({
loading: false,
displayItems: [],
sidebarTabs: BASE_TABS,
errorMessage:
error instanceof Error
? error.message
: '加载失败,请稍后重试',
});
}
},
const activeId =
overrideCategoryId != null &&
String(overrideCategoryId).trim() !== ''
? String(overrideCategoryId).trim()
: this.data.activeCategoryId || this._defaultCategoryId;
const items = getItemsByCategory(activeId);
const filtered = filterByKeyword(items, this.data.searchKeyword);
this.setData({
loading: false,
searchPlaceholder: _categoryData.searchPlaceholder,
activeCategoryId: activeId,
displayItems: filtered,
});
onRetry() {
void this.loadWorksheets();
},
onTapCategory(e: WechatMiniprogram.TouchEvent) {
const id = e.currentTarget.dataset.id as string;
if (!id || id === this.data.activeCategoryId) return;
const items = getItemsByCategory(id);
const filtered = filterByKeyword(items, this.data.searchKeyword);
if (
!this._categoryData ||
this.data.loading ||
!id ||
id === this.data.activeCategoryId ||
!this._categoryData.sidebarTabs.some((tab) => tab.id === id)
)
return;
this.setData({
activeCategoryId: id,
displayItems: filtered,
displayItems: getCategoryItems(
this._categoryData,
id,
this.data.searchKeyword,
),
scrollIntoViewId: '',
});
tracker.report('category_select', 'pages/category/category', {
@@ -294,49 +163,69 @@ Page({
onSearchInput(e: WechatMiniprogram.Input) {
const keyword = (e.detail.value ?? '').trim();
const items = getItemsByCategory(this.data.activeCategoryId);
const filtered = filterByKeyword(items, keyword);
this.setData({
searchKeyword: keyword,
displayItems: filtered,
displayItems: getCategoryItems(
this._categoryData,
this.data.activeCategoryId,
keyword,
),
});
},
onClearSearch() {
const items = getItemsByCategory(this.data.activeCategoryId);
this.setData({
searchKeyword: '',
displayItems: items,
displayItems: getCategoryItems(
this._categoryData,
this.data.activeCategoryId,
),
});
},
onTapItem(e: WechatMiniprogram.TouchEvent) {
const path = e.currentTarget.dataset.path as string;
const title = e.currentTarget.dataset.title as string;
const available = e.currentTarget.dataset.available;
if (!available || !path) {
/** 点击时重新检查详情,已下架或网络不可用时不进入内置生成页。 */
async onTapItem(e: WechatMiniprogram.TouchEvent) {
const id = e.currentTarget.dataset.id as string;
if (
this._opening ||
this.data.loading ||
!id ||
!this.data.displayItems.some(
(item) => item.id === id && item.available,
)
)
return;
this._opening = true;
const version = this._loadVersion;
try {
const row = await getWorksheet(id);
if (version !== this._loadVersion) return;
if (!row.path) throw new Error('内容暂不可用');
tracker.reportWorksheetClick('pages/category/category', {
worksheet_id: row.id,
worksheet_title: row.title,
category_id: this.data.activeCategoryId,
source: 'category_list',
});
const fail = () =>
wx.showToast({ title: '暂时无法打开该内容', icon: 'none' });
if (TAB_BAR_PATHS.has(row.path))
wx.switchTab({ url: row.path, fail });
else wx.navigateTo({ url: row.path, fail });
} catch (error) {
if (version !== this._loadVersion) return;
wx.showToast({
title: title ? `${title} 即将上线` : '即将上线',
title:
error instanceof Error
? error.message
: '暂时无法打开,请稍后重试',
icon: 'none',
});
return;
if (error instanceof ApiRequestError && error.statusCode === 404)
void this.loadWorksheets();
} finally {
this._opening = false;
}
tracker.reportWorksheetClick('pages/category/category', {
worksheet_id: String(parseMiniProgramUrl(path).query.id || ''),
worksheet_title: title || '',
category_id: this.data.activeCategoryId,
source: 'category_list',
});
if (TAB_BAR_PATHS.has(path)) {
wx.switchTab({ url: path });
return;
}
wx.navigateTo({ url: path });
},
...appSharePageMethods,