feat: 完成分龄页开发
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
import { checkAndSaveImage } from './saveImage';
|
||||
import tracker from './tracker';
|
||||
import { incrementWorksheetDownloads } from './worksheetStats';
|
||||
|
||||
// 存储键名
|
||||
const STORAGE_KEY_DOWNLOAD_COUNT = 'downloadCount';
|
||||
@@ -195,8 +196,9 @@ function doDownload(
|
||||
errorToast?: string;
|
||||
trackerName?: string;
|
||||
trackerMode?: string;
|
||||
worksheetId?: string;
|
||||
},
|
||||
) {
|
||||
): Promise<boolean> {
|
||||
// 上报下载埋点
|
||||
if (options.trackerName) {
|
||||
if (options.trackerMode) {
|
||||
@@ -206,11 +208,15 @@ function doDownload(
|
||||
}
|
||||
}
|
||||
|
||||
// 增加下载次数
|
||||
incrementDownloadCount();
|
||||
|
||||
// 执行下载(checkAndSaveImage 内部已有成功提示,这里不需要额外处理)
|
||||
checkAndSaveImage(canvas);
|
||||
return checkAndSaveImage(canvas).then((saved) => {
|
||||
if (saved) {
|
||||
incrementDownloadCount();
|
||||
if (options.worksheetId) {
|
||||
incrementWorksheetDownloads(options.worksheetId);
|
||||
}
|
||||
}
|
||||
return saved;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -221,6 +227,7 @@ function doDownload(
|
||||
* @param options.errorToast 错误提示(可选,默认:'请先生成内容')
|
||||
* @param options.trackerName 埋点名称(可选)
|
||||
* @param options.trackerMode 埋点模式(可选)
|
||||
* @param options.worksheetId worksheet ID(可选,保存成功后更新 downloads)
|
||||
*/
|
||||
export async function downloadPrint(
|
||||
canvas: Canvas | null,
|
||||
@@ -229,20 +236,20 @@ export async function downloadPrint(
|
||||
errorToast?: string;
|
||||
trackerName?: string;
|
||||
trackerMode?: string;
|
||||
worksheetId?: string;
|
||||
} = {},
|
||||
): Promise<void> {
|
||||
): Promise<boolean> {
|
||||
// 检查 canvas 是否存在
|
||||
if (!canvas) {
|
||||
wx.showToast({
|
||||
title: options.errorToast || '请先生成内容',
|
||||
icon: 'none',
|
||||
});
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isDevBypassLimits()) {
|
||||
doDownload(canvas, options);
|
||||
return;
|
||||
return doDownload(canvas, options);
|
||||
}
|
||||
|
||||
// 获取今日下载次数
|
||||
@@ -251,21 +258,19 @@ export async function downloadPrint(
|
||||
|
||||
// 如果已观看广告,直接下载
|
||||
if (hasWatchedAd) {
|
||||
doDownload(canvas, options);
|
||||
return;
|
||||
return doDownload(canvas, options);
|
||||
}
|
||||
|
||||
// 如果未超过免费次数,直接下载
|
||||
if (downloadCount < FREE_DOWNLOAD_LIMIT) {
|
||||
doDownload(canvas, options);
|
||||
return;
|
||||
return doDownload(canvas, options);
|
||||
}
|
||||
|
||||
// 超过免费次数,需要观看广告
|
||||
const watched = await showVideoAd();
|
||||
if (watched) {
|
||||
// 观看完成,可以下载
|
||||
doDownload(canvas, options);
|
||||
return doDownload(canvas, options);
|
||||
}
|
||||
/* else {
|
||||
// 未观看完成,提示用户
|
||||
@@ -275,4 +280,5 @@ export async function downloadPrint(
|
||||
duration: 2000,
|
||||
});
|
||||
} */
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -3,63 +3,75 @@ import { A4_EXPORT_SIZE_150DPI } from '../constants/colors';
|
||||
/**
|
||||
* 导出为固定打印分辨率(A4 150 DPI),避免高 DPR 设备导出过大图导致打印慢、文件大
|
||||
*/
|
||||
const doSaveImage = (canvas: Canvas) => {
|
||||
const { width: destWidth, height: destHeight } = A4_EXPORT_SIZE_150DPI;
|
||||
wx.canvasToTempFilePath({
|
||||
canvas,
|
||||
fileType: 'jpg',
|
||||
quality: 0.9,
|
||||
destWidth,
|
||||
destHeight,
|
||||
success: (res) => {
|
||||
wx.saveImageToPhotosAlbum({
|
||||
filePath: res.tempFilePath,
|
||||
success: () => {
|
||||
// 下载成功提示(下载次数记录在 downloadPrint.ts 中处理)
|
||||
wx.showToast({
|
||||
title: '保存成功,请在相册中查看',
|
||||
icon: 'success',
|
||||
duration: 2000,
|
||||
});
|
||||
},
|
||||
// fail: () => wx.showToast({
|
||||
// title: '保存涂色卡失败,请重试!',
|
||||
// icon: 'none',
|
||||
// duration: 2000,
|
||||
// })
|
||||
});
|
||||
},
|
||||
fail: () => {
|
||||
wx.showToast({
|
||||
title: '生成涂色卡失败,请重试!',
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
});
|
||||
},
|
||||
const doSaveImage = (canvas: Canvas): Promise<boolean> =>
|
||||
new Promise((resolve) => {
|
||||
const { width: destWidth, height: destHeight } = A4_EXPORT_SIZE_150DPI;
|
||||
wx.canvasToTempFilePath({
|
||||
canvas,
|
||||
fileType: 'jpg',
|
||||
quality: 0.9,
|
||||
destWidth,
|
||||
destHeight,
|
||||
success: (res) => {
|
||||
wx.saveImageToPhotosAlbum({
|
||||
filePath: res.tempFilePath,
|
||||
success: () => {
|
||||
wx.showToast({
|
||||
title: '保存成功,请在相册中查看',
|
||||
icon: 'success',
|
||||
duration: 2000,
|
||||
});
|
||||
resolve(true);
|
||||
},
|
||||
fail: () => {
|
||||
wx.showToast({
|
||||
title: '保存涂色卡失败,请重试!',
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
});
|
||||
resolve(false);
|
||||
},
|
||||
});
|
||||
},
|
||||
fail: () => {
|
||||
wx.showToast({
|
||||
title: '生成涂色卡失败,请重试!',
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
});
|
||||
resolve(false);
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const requestPhotoPermission = (canvas: Canvas) => {
|
||||
wx.authorize({
|
||||
scope: 'scope.writePhotosAlbum',
|
||||
success: () => doSaveImage(canvas),
|
||||
fail: () =>
|
||||
wx.showToast({
|
||||
title: '请在“设置-添加到相册”中开启相册权限以保存涂色卡',
|
||||
icon: 'none',
|
||||
duration: 3000,
|
||||
}),
|
||||
const requestPhotoPermission = (canvas: Canvas): Promise<boolean> =>
|
||||
new Promise((resolve) => {
|
||||
wx.authorize({
|
||||
scope: 'scope.writePhotosAlbum',
|
||||
success: () => {
|
||||
doSaveImage(canvas).then(resolve);
|
||||
},
|
||||
fail: () => {
|
||||
wx.showToast({
|
||||
title: '请在“设置-添加到相册”中开启相册权限以保存涂色卡',
|
||||
icon: 'none',
|
||||
duration: 3000,
|
||||
});
|
||||
resolve(false);
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const checkAndSaveImage = (canvas: Canvas) => {
|
||||
wx.getSetting({
|
||||
success: (res) => {
|
||||
if (!res.authSetting['scope.writePhotosAlbum']) {
|
||||
requestPhotoPermission(canvas);
|
||||
} else {
|
||||
doSaveImage(canvas);
|
||||
}
|
||||
},
|
||||
export const checkAndSaveImage = (canvas: Canvas): Promise<boolean> =>
|
||||
new Promise((resolve) => {
|
||||
wx.getSetting({
|
||||
success: (res) => {
|
||||
if (!res.authSetting['scope.writePhotosAlbum']) {
|
||||
requestPhotoPermission(canvas).then(resolve);
|
||||
} else {
|
||||
doSaveImage(canvas).then(resolve);
|
||||
}
|
||||
},
|
||||
fail: () => resolve(false),
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
type WorksheetStatsField = 'likes' | 'downloads';
|
||||
|
||||
export async function updateWorksheetStats(
|
||||
id: string,
|
||||
field: WorksheetStatsField,
|
||||
): Promise<boolean> {
|
||||
const worksheetId = String(id || '').trim();
|
||||
if (!worksheetId) return false;
|
||||
|
||||
try {
|
||||
const { result } = await wx.cloud.callFunction({
|
||||
name: 'worksheetsStatsUpdate',
|
||||
data: {
|
||||
id: worksheetId,
|
||||
field,
|
||||
},
|
||||
});
|
||||
return !!(result as { success?: boolean } | undefined)?.success;
|
||||
} catch (error) {
|
||||
console.error('update worksheet stats failed', field, worksheetId, error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function incrementWorksheetLikes(id: string): Promise<boolean> {
|
||||
return updateWorksheetStats(id, 'likes');
|
||||
}
|
||||
|
||||
export function incrementWorksheetDownloads(id: string): Promise<boolean> {
|
||||
return updateWorksheetStats(id, 'downloads');
|
||||
}
|
||||
Reference in New Issue
Block a user