feat: 增加收藏、下载种子数据功能

This commit is contained in:
R524809
2026-05-11 12:38:10 +08:00
parent debecd67c6
commit 39b8e01c23
11 changed files with 521 additions and 21 deletions
@@ -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(
'同步 Seedlikes_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 });
}
},
});