feat: 完成全部接口替换为新后端接口

This commit is contained in:
R524809
2026-10-19 13:19:07 +08:00
parent 8de06ce66b
commit 997042789d
40 changed files with 1946 additions and 785 deletions
@@ -1,3 +1,7 @@
import { compareCountDescending } from '../../pages/category/category.model';
import { apiRequest } from '../../services/api-request';
import { managementRequest } from '../../services/admin-api';
import { resolvePreviewUrl } from '../../config/assets';
import { CATEGORY_LIST } from '../../core/data/categories';
type SimpleWorksheet = {
@@ -13,8 +17,8 @@ type SimpleWorksheet = {
isHot: boolean;
path: string;
ageBand: string;
downloads: number;
likes: number;
downloads: string;
likes: string;
};
type SectionConfig = {
@@ -43,12 +47,6 @@ type PageConfig = {
[key: string]: unknown;
};
type CloudFunctionResult<T> = {
success?: boolean;
message?: string;
data?: T;
};
function formatSimple(raw: Record<string, unknown>): SimpleWorksheet {
const ageMin = Number(raw.ageMin) || 0;
const ageMax = Number(raw.ageMax) || 0;
@@ -57,7 +55,7 @@ function formatSimple(raw: Record<string, unknown>): SimpleWorksheet {
title: String(raw.title || ''),
subtitle: String(raw.subtitle || ''),
category: String(raw.category || ''),
previewImg: String(raw.previewImg || ''),
previewImg: resolvePreviewUrl(raw.previewImg),
ageMin,
ageMax,
difficulty: Number(raw.difficulty) || 2,
@@ -65,22 +63,11 @@ function formatSimple(raw: Record<string, unknown>): SimpleWorksheet {
isHot: !!raw.isHot,
path: String(raw.path || ''),
ageBand: `${ageMin}-${ageMax}岁`,
downloads: Number(raw.downloads) || 0,
likes: Number(raw.likes) || 0,
downloads: String(raw.downloads || '0'),
likes: String(raw.likes || '0'),
};
}
async function callCloudFunction<T>(
name: string,
data?: Record<string, unknown>,
): Promise<CloudFunctionResult<T>> {
const response = (await wx.cloud.callFunction({
name,
data: data || {},
})) as { result?: CloudFunctionResult<T> };
return response.result || {};
}
Page({
data: {
featured: [] as SimpleWorksheet[],
@@ -122,15 +109,15 @@ Page({
async loadTimerStatus() {
try {
const db = wx.cloud.database();
const { data } = await db
.collection('settings')
.doc('homeAutoRefresh')
.get();
this.setData({ timerEnabled: (data as { enabled?: boolean }).enabled !== false });
const data = await apiRequest<{ enabled: boolean }>(
'/admin/site/timer',
);
this.setData({
timerEnabled: (data as { enabled?: boolean }).enabled !== false,
});
} catch {
// Document doesn't exist — default enabled
this.setData({ timerEnabled: true });
// 权限或网络失败不能被当成已启用。
this.setData({ timerEnabled: false });
}
},
@@ -139,13 +126,14 @@ Page({
try {
// Fetch in parallel: active worksheets + saved page config
const [wsResult, configResult] = await Promise.all([
callCloudFunction<SimpleWorksheet[]>('worksheetsQuery', {
managementRequest<SimpleWorksheet[]>('worksheets.list', {
status: 'active',
}),
callCloudFunction<PageConfig>('pageConfigFetch'),
managementRequest<PageConfig>('site.config'),
]);
if (!wsResult.success) throw new Error(wsResult.message || '查询失败');
if (!wsResult.success)
throw new Error(wsResult.message || '查询失败');
const allActive = (wsResult.data || []).map((raw) =>
formatSimple(raw as unknown as Record<string, unknown>),
@@ -156,16 +144,21 @@ Page({
// featured: always top-5 by downloads; hot: always top-5 by likes
const featured: SimpleWorksheet[] = [...allActive]
.sort((a, b) => (b.downloads ?? 0) - (a.downloads ?? 0))
.sort((a, b) =>
compareCountDescending(a.downloads, b.downloads),
)
.slice(0, 5);
const hot: SimpleWorksheet[] = [...allActive]
.sort((a, b) => (b.likes ?? 0) - (a.likes ?? 0))
.sort((a, b) => compareCountDescending(a.likes, b.likes))
.slice(0, 5);
// Restore saved sections from config
const savedHome = (configResult.data as PageConfig | undefined)?.home;
const savedHome = (configResult.data as PageConfig | undefined)
?.home;
const savedSections: HomeConfigItem[] = savedHome?.sections || [];
const savedSectionMap = new Map(savedSections.map((s) => [s.id, s]));
const savedSectionMap = new Map(
savedSections.map((s) => [s.id, s]),
);
const sections: SectionConfig[] = CATEGORY_LIST.map((c) => {
const saved = savedSectionMap.get(c.id);
@@ -184,7 +177,13 @@ Page({
};
});
this.setData({ loading: false, allActive, featured, hot, sections });
this.setData({
loading: false,
allActive,
featured,
hot,
sections,
});
} catch (error) {
this.setData({ loading: false });
wx.showToast({
@@ -202,7 +201,7 @@ Page({
this.setData({ timerLoading: true });
try {
const result = await callCloudFunction('homeTimerControl', {
const result = await managementRequest('site.timer', {
enabled: newEnabled,
});
if (!result.success) {
@@ -335,9 +334,9 @@ Page({
this.setData({ building: true });
try {
const result = await callCloudFunction<{
const result = await managementRequest<{
version: number;
}>('pageContentBuild', {
}>('site.rebuild', {
page: 'home',
featuredIds: this.data.featured.map((w) => w._id),
hotIds: this.data.hot.map((w) => w._id),