feat: 开发收藏和下载功能
This commit is contained in:
@@ -6,9 +6,9 @@ import {
|
||||
type FocusTypeConfig,
|
||||
type FocusTypeAction,
|
||||
} from './registry';
|
||||
import { getPublishMetaByFocusState } from './focusDraw.config';
|
||||
import { getPublishMetaByFocusState, FOCUS_WORKSHEET_DEFINITIONS } from './focusDraw.config';
|
||||
import type { DebugPublishMeta } from '../../utils/debugPublish';
|
||||
import { incrementWorksheetLikes } from '../../utils/worksheetStats';
|
||||
import { addFavorite, removeFavorite, batchCheckFavorited } from '../../utils/favorites';
|
||||
|
||||
const TYPE_LIST = FOCUS_TYPE_CONFIGS.map((t) => ({
|
||||
id: t.id,
|
||||
@@ -24,6 +24,7 @@ createFocusPage({
|
||||
drawService: null as BaseDrawService | null,
|
||||
currentTypeConfig: null as FocusTypeConfig | null,
|
||||
currentData: null as any,
|
||||
_favoritedMap: {} as Record<string, boolean>,
|
||||
|
||||
data: {
|
||||
pageTitle: '专注力练习',
|
||||
@@ -73,6 +74,8 @@ createFocusPage({
|
||||
});
|
||||
|
||||
this.initPageInfo(routeId, title);
|
||||
|
||||
this.loadFavoritedMap();
|
||||
},
|
||||
|
||||
onCanvasReady(e: WechatMiniprogram.CustomEvent) {
|
||||
@@ -121,7 +124,9 @@ createFocusPage({
|
||||
selectedTypeId: id,
|
||||
functionId: id,
|
||||
pageTitle: title,
|
||||
isPreviewFavorite: false,
|
||||
isPreviewFavorite: !!this._favoritedMap[
|
||||
getPublishMetaByFocusState(id, id, mode)?.id || id
|
||||
],
|
||||
showActions: !!typeConfig.actions,
|
||||
currentActions: typeConfig.actions || [],
|
||||
actionsTitle: typeConfig.actionsTitle || '选择模式',
|
||||
@@ -158,7 +163,7 @@ createFocusPage({
|
||||
this.setData({
|
||||
currentMode: value,
|
||||
pageTitle: title,
|
||||
isPreviewFavorite: false,
|
||||
isPreviewFavorite: !!this._favoritedMap[this.getWorksheetStatsIdFor(value)],
|
||||
});
|
||||
|
||||
this.initPageInfo(this.data.functionId, title);
|
||||
@@ -202,9 +207,14 @@ createFocusPage({
|
||||
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 ? '收藏成功' : '已取消收藏',
|
||||
@@ -221,6 +231,24 @@ createFocusPage({
|
||||
return meta?.id || this.data.functionId;
|
||||
},
|
||||
|
||||
getWorksheetStatsIdFor(mode: string): string {
|
||||
const meta = getPublishMetaByFocusState(
|
||||
this.data.functionId,
|
||||
this.data.selectedTypeId,
|
||||
mode,
|
||||
);
|
||||
return meta?.id || this.data.functionId;
|
||||
},
|
||||
|
||||
async loadFavoritedMap() {
|
||||
const ids = FOCUS_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 = getPublishMetaByFocusState(
|
||||
this.data.functionId,
|
||||
|
||||
Reference in New Issue
Block a user