feat: 增加收藏、下载种子数据功能
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
export const MOCK_SEED_COUNTS: Record<
|
||||
string,
|
||||
{ downloads: number; likes: number }
|
||||
> = {
|
||||
'word-coloring-grid': {
|
||||
downloads: 0,
|
||||
likes: 1,
|
||||
},
|
||||
'word-coloring-find': {
|
||||
downloads: 0,
|
||||
likes: 0,
|
||||
},
|
||||
'handwriting-sheet': {
|
||||
downloads: 0,
|
||||
likes: 0,
|
||||
},
|
||||
'letter-tracing-single': {
|
||||
downloads: 10,
|
||||
likes: 3,
|
||||
},
|
||||
'letter-tracing-two-column': {
|
||||
downloads: 15,
|
||||
likes: 13,
|
||||
},
|
||||
'letter-tracing-upper-lower': {
|
||||
downloads: 3,
|
||||
likes: 7,
|
||||
},
|
||||
'letter-tracing-case-pairing': {
|
||||
downloads: 9,
|
||||
likes: 9,
|
||||
},
|
||||
'letter-tracing-three': {
|
||||
downloads: 12,
|
||||
likes: 7,
|
||||
},
|
||||
'letter-tracing-half': {
|
||||
downloads: 2,
|
||||
likes: 1,
|
||||
},
|
||||
'letter-tracing-daily-checkin': {
|
||||
downloads: 12,
|
||||
likes: 14,
|
||||
},
|
||||
'number-find': {
|
||||
downloads: 20,
|
||||
likes: 23,
|
||||
},
|
||||
'number-coloring': {
|
||||
downloads: 37,
|
||||
likes: 41,
|
||||
},
|
||||
'counting-matching': {
|
||||
downloads: 20,
|
||||
likes: 28,
|
||||
},
|
||||
'number-object-match': {
|
||||
downloads: 12,
|
||||
likes: 17,
|
||||
},
|
||||
'counting-select': {
|
||||
downloads: 10,
|
||||
likes: 9,
|
||||
},
|
||||
compare: {
|
||||
downloads: 15,
|
||||
likes: 10,
|
||||
},
|
||||
'number-sort': {
|
||||
downloads: 35,
|
||||
likes: 47,
|
||||
},
|
||||
'missing-number': {
|
||||
downloads: 13,
|
||||
likes: 10,
|
||||
},
|
||||
'number-decompose': {
|
||||
downloads: 26,
|
||||
likes: 21,
|
||||
},
|
||||
'number-decompose-20': {
|
||||
downloads: 21,
|
||||
likes: 18,
|
||||
},
|
||||
'one-digit-addition': {
|
||||
downloads: 12,
|
||||
likes: 15,
|
||||
},
|
||||
'addition-5': {
|
||||
downloads: 11,
|
||||
likes: 12,
|
||||
},
|
||||
'make-ten': {
|
||||
downloads: 38,
|
||||
likes: 35,
|
||||
},
|
||||
'break-ten': {
|
||||
downloads: 0,
|
||||
likes: 24,
|
||||
},
|
||||
'flat-ten': {
|
||||
downloads: 26,
|
||||
likes: 26,
|
||||
},
|
||||
'borrow-ten': {
|
||||
downloads: 24,
|
||||
likes: 12,
|
||||
},
|
||||
'practice-addition': {
|
||||
downloads: 31,
|
||||
likes: 21,
|
||||
},
|
||||
'practice-subtraction': {
|
||||
downloads: 12,
|
||||
likes: 14,
|
||||
},
|
||||
'practice-mixed': {
|
||||
downloads: 22,
|
||||
likes: 25,
|
||||
},
|
||||
'multiplication-table': {
|
||||
downloads: 2,
|
||||
likes: 5,
|
||||
},
|
||||
'color-shape-match': {
|
||||
downloads: 22,
|
||||
likes: 23,
|
||||
},
|
||||
'shape-symbol': {
|
||||
downloads: 7,
|
||||
likes: 7,
|
||||
},
|
||||
'position-coloring': {
|
||||
downloads: 18,
|
||||
likes: 20,
|
||||
},
|
||||
'color-pattern': {
|
||||
downloads: 15,
|
||||
likes: 15,
|
||||
},
|
||||
'match-connect': {
|
||||
downloads: 10,
|
||||
likes: 10,
|
||||
},
|
||||
'line-recognition': {
|
||||
downloads: 20,
|
||||
likes: 23,
|
||||
},
|
||||
'grid-reasoning': {
|
||||
downloads: 17,
|
||||
likes: 16,
|
||||
},
|
||||
'code-connect': {
|
||||
downloads: 32,
|
||||
likes: 29,
|
||||
},
|
||||
'dot-connect': {
|
||||
downloads: 10,
|
||||
likes: 20,
|
||||
},
|
||||
'grid-drawing-3x3': {
|
||||
downloads: 20,
|
||||
likes: 23,
|
||||
},
|
||||
'grid-drawing-5x5': {
|
||||
downloads: 8,
|
||||
likes: 12,
|
||||
},
|
||||
};
|
||||
@@ -1,3 +1,5 @@
|
||||
import { MOCK_SEED_COUNTS } from './mockLikes';
|
||||
|
||||
type CloudFunctionResult<T> = {
|
||||
success?: boolean;
|
||||
message?: string;
|
||||
@@ -134,6 +136,8 @@ Page({
|
||||
loadingAges: false,
|
||||
loadingTags: false,
|
||||
loadingRebuild: false,
|
||||
loadingSeed: false,
|
||||
loadingResetStats: false,
|
||||
statusText: '配置加载中,请稍候',
|
||||
},
|
||||
|
||||
@@ -312,4 +316,138 @@ Page({
|
||||
this.setData({ loadingRebuild: false });
|
||||
}
|
||||
},
|
||||
|
||||
async onSyncSeed() {
|
||||
if (
|
||||
this.data.loadingAges ||
|
||||
this.data.loadingTags ||
|
||||
this.data.loadingRebuild ||
|
||||
this.data.loadingSeed
|
||||
)
|
||||
return;
|
||||
|
||||
this.setData({ loadingSeed: true });
|
||||
wx.showLoading({ title: '同步 Seed...' });
|
||||
const startedAt = Date.now();
|
||||
|
||||
try {
|
||||
const patches = Object.entries(MOCK_SEED_COUNTS).map(
|
||||
([id, counts]) => ({
|
||||
id,
|
||||
likes_seed: counts.likes,
|
||||
downloads_seed: counts.downloads,
|
||||
}),
|
||||
);
|
||||
|
||||
const patchRes = await callCloudFunction('worksheetsBatchPatch', {
|
||||
patches,
|
||||
dryRun: false,
|
||||
});
|
||||
|
||||
if (!patchRes.success) {
|
||||
throw new Error(patchRes.message || '同步 Seed 失败');
|
||||
}
|
||||
|
||||
const elapsed = Math.round((Date.now() - startedAt) / 1000);
|
||||
|
||||
const text = [
|
||||
summarizeResult(
|
||||
'同步 Seed(likes_seed / downloads_seed)',
|
||||
patchRes,
|
||||
),
|
||||
`共 ${patches.length} 条 Seed 数据`,
|
||||
`耗时:${elapsed}s`,
|
||||
].join('\n');
|
||||
|
||||
this.setData({ statusText: text });
|
||||
wx.showToast({ title: '已完成', icon: 'success' });
|
||||
} catch (error) {
|
||||
const msg =
|
||||
error instanceof Error ? error.message : '同步 Seed 失败';
|
||||
this.setData({ statusText: msg });
|
||||
wx.showToast({ title: msg, icon: 'none' });
|
||||
} finally {
|
||||
wx.hideLoading();
|
||||
this.setData({ loadingSeed: false });
|
||||
}
|
||||
},
|
||||
|
||||
async onResetStats() {
|
||||
if (
|
||||
this.data.loadingAges ||
|
||||
this.data.loadingTags ||
|
||||
this.data.loadingRebuild ||
|
||||
this.data.loadingSeed ||
|
||||
this.data.loadingResetStats
|
||||
)
|
||||
return;
|
||||
|
||||
const { confirm } = await wx.showModal({
|
||||
title: '确认初始化',
|
||||
content:
|
||||
'将所有 worksheet 的 likes 和 downloads 重置为 0,此操作不可撤销。确定继续?',
|
||||
confirmText: '确定重置',
|
||||
confirmColor: '#e53935',
|
||||
});
|
||||
if (!confirm) return;
|
||||
|
||||
this.setData({ loadingResetStats: true });
|
||||
wx.showLoading({ title: '重置中...' });
|
||||
const startedAt = Date.now();
|
||||
|
||||
try {
|
||||
const queryRes = await callCloudFunction<
|
||||
Array<{ _id: string }>
|
||||
>('worksheetsQuery', {});
|
||||
|
||||
if (!queryRes.success || !queryRes.data) {
|
||||
throw new Error(queryRes.message || '查询 worksheet 列表失败');
|
||||
}
|
||||
|
||||
const patches = (queryRes.data as Array<{ _id: string }>).map(
|
||||
(ws) => ({
|
||||
id: ws._id,
|
||||
likes: 0,
|
||||
downloads: 0,
|
||||
}),
|
||||
);
|
||||
|
||||
if (patches.length === 0) {
|
||||
this.setData({ statusText: '没有需要重置的 worksheet' });
|
||||
wx.showToast({ title: '无数据', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
const patchRes = await callCloudFunction('worksheetsBatchPatch', {
|
||||
patches,
|
||||
dryRun: false,
|
||||
});
|
||||
|
||||
if (!patchRes.success) {
|
||||
throw new Error(patchRes.message || '重置失败');
|
||||
}
|
||||
|
||||
const elapsed = Math.round((Date.now() - startedAt) / 1000);
|
||||
|
||||
const text = [
|
||||
summarizeResult(
|
||||
'初始化收藏和下载数据(likes / downloads → 0)',
|
||||
patchRes,
|
||||
),
|
||||
`共 ${patches.length} 条 worksheet`,
|
||||
`耗时:${elapsed}s`,
|
||||
].join('\n');
|
||||
|
||||
this.setData({ statusText: text });
|
||||
wx.showToast({ title: '已重置', icon: 'success' });
|
||||
} catch (error) {
|
||||
const msg =
|
||||
error instanceof Error ? error.message : '重置失败';
|
||||
this.setData({ statusText: msg });
|
||||
wx.showToast({ title: msg, icon: 'none' });
|
||||
} finally {
|
||||
wx.hideLoading();
|
||||
this.setData({ loadingResetStats: false });
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
@@ -52,23 +52,37 @@
|
||||
type="primary"
|
||||
width="100%"
|
||||
loading="{{loadingAges}}"
|
||||
disabled="{{!configReady || loadingAges || loadingTags || loadingRebuild}}"
|
||||
disabled="{{!configReady || loadingAges || loadingTags || loadingRebuild || loadingSeed || loadingResetStats}}"
|
||||
bindtap="onSyncAges">
|
||||
{{loadingAges ? '同步中...' : '同步年龄'}}
|
||||
</toy-button>
|
||||
<toy-button
|
||||
width="100%"
|
||||
loading="{{loadingTags}}"
|
||||
disabled="{{!configReady || loadingAges || loadingTags || loadingRebuild}}"
|
||||
disabled="{{!configReady || loadingAges || loadingTags || loadingRebuild || loadingSeed || loadingResetStats}}"
|
||||
bindtap="onSyncTags">
|
||||
{{loadingTags ? '同步中...' : '同步标签'}}
|
||||
</toy-button>
|
||||
<toy-button
|
||||
width="100%"
|
||||
loading="{{loadingRebuild}}"
|
||||
disabled="{{loadingAges || loadingTags || loadingRebuild}}"
|
||||
disabled="{{loadingAges || loadingTags || loadingRebuild || loadingSeed || loadingResetStats}}"
|
||||
bindtap="onRebuildConfig">
|
||||
{{loadingRebuild ? '重建中...' : '重建配置'}}
|
||||
</toy-button>
|
||||
<toy-button
|
||||
width="100%"
|
||||
loading="{{loadingSeed}}"
|
||||
disabled="{{loadingAges || loadingTags || loadingRebuild || loadingSeed || loadingResetStats}}"
|
||||
bindtap="onSyncSeed">
|
||||
{{loadingSeed ? '同步中...' : '同步 Seed 数据'}}
|
||||
</toy-button>
|
||||
<toy-button
|
||||
width="100%"
|
||||
loading="{{loadingResetStats}}"
|
||||
disabled="{{loadingAges || loadingTags || loadingRebuild || loadingSeed || loadingResetStats}}"
|
||||
bindtap="onResetStats">
|
||||
{{loadingResetStats ? '重置中...' : '初始化收藏和下载数据'}}
|
||||
</toy-button>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
Reference in New Issue
Block a user