feat: 开发收藏和下载功能
This commit is contained in:
@@ -6,9 +6,9 @@ import {
|
||||
type MathTypeConfig,
|
||||
type MathTypeAction,
|
||||
} from './registry';
|
||||
import { getPublishMetaByMathState } from './mathDraw.config';
|
||||
import { getPublishMetaByMathState, MATH_WORKSHEET_DEFINITIONS } from './mathDraw.config';
|
||||
import type { DebugPublishMeta } from '../../utils/debugPublish';
|
||||
import { incrementWorksheetLikes } from '../../utils/worksheetStats';
|
||||
import { addFavorite, removeFavorite, batchCheckFavorited } from '../../utils/favorites';
|
||||
|
||||
const TYPE_LIST = MATH_TYPE_CONFIGS.map((t) => ({
|
||||
id: t.id,
|
||||
@@ -25,6 +25,7 @@ createMathPage({
|
||||
currentTypeConfig: null as MathTypeConfig | null,
|
||||
currentData: null as any,
|
||||
routeExtra: null as Record<string, any> | null,
|
||||
_favoritedMap: {} as Record<string, boolean>,
|
||||
|
||||
data: {
|
||||
pageTitle: '数学练习',
|
||||
@@ -82,6 +83,9 @@ createMathPage({
|
||||
});
|
||||
|
||||
this.initPageInfo(routeId, title);
|
||||
|
||||
// 加载收藏状态
|
||||
this.loadFavoritedMap();
|
||||
},
|
||||
|
||||
onCanvasReady(e: WechatMiniprogram.CustomEvent) {
|
||||
@@ -131,7 +135,9 @@ createMathPage({
|
||||
selectedTypeId: id,
|
||||
functionId: id,
|
||||
pageTitle: title,
|
||||
isPreviewFavorite: false,
|
||||
isPreviewFavorite: !!this._favoritedMap[
|
||||
getPublishMetaByMathState(id, id, mode)?.id || id
|
||||
],
|
||||
showActions: !!typeConfig.actions,
|
||||
currentActions: typeConfig.actions || [],
|
||||
actionsTitle: typeConfig.actionsTitle || '选择模式',
|
||||
@@ -172,7 +178,7 @@ createMathPage({
|
||||
this.setData({
|
||||
currentMode: value,
|
||||
pageTitle: title,
|
||||
isPreviewFavorite: false,
|
||||
isPreviewFavorite: !!this._favoritedMap[this.getWorksheetStatsIdFor(value)],
|
||||
});
|
||||
|
||||
this.initPageInfo(this.data.functionId, title);
|
||||
@@ -266,9 +272,14 @@ createMathPage({
|
||||
async onPreviewFavorite() {
|
||||
const next = !this.data.isPreviewFavorite;
|
||||
this.setData({ isPreviewFavorite: next });
|
||||
if (next) {
|
||||
const id = this.getWorksheetStatsId();
|
||||
if (id) incrementWorksheetLikes(id);
|
||||
const id = this.getWorksheetStatsId();
|
||||
if (id) {
|
||||
this._favoritedMap[id] = next;
|
||||
if (next) {
|
||||
addFavorite(id);
|
||||
} else {
|
||||
removeFavorite(id);
|
||||
}
|
||||
}
|
||||
wx.showToast({
|
||||
title: next ? '收藏成功' : '已取消收藏',
|
||||
@@ -285,6 +296,25 @@ createMathPage({
|
||||
return meta?.id || this.data.functionId;
|
||||
},
|
||||
|
||||
getWorksheetStatsIdFor(mode: string): string {
|
||||
const meta = getPublishMetaByMathState(
|
||||
this.data.functionId,
|
||||
this.data.selectedTypeId,
|
||||
mode,
|
||||
);
|
||||
return meta?.id || this.data.functionId;
|
||||
},
|
||||
|
||||
async loadFavoritedMap() {
|
||||
const ids = MATH_WORKSHEET_DEFINITIONS.map((d) => d.id);
|
||||
this._favoritedMap = await batchCheckFavorited(ids);
|
||||
// 回显当前选中的收藏状态
|
||||
const currentId = this.getWorksheetStatsId();
|
||||
if (this._favoritedMap[currentId]) {
|
||||
this.setData({ isPreviewFavorite: true });
|
||||
}
|
||||
},
|
||||
|
||||
getPublishMeta(): DebugPublishMeta {
|
||||
const meta = getPublishMetaByMathState(
|
||||
this.data.functionId,
|
||||
|
||||
Reference in New Issue
Block a user