196 lines
5.0 KiB
TypeScript
196 lines
5.0 KiB
TypeScript
/**
|
|
* 下载打印通用工具
|
|
* 支持下载次数限制和激励广告
|
|
*/
|
|
|
|
import { checkAndSaveImage } from './saveImage';
|
|
import tracker from './tracker';
|
|
import { addDownloadLog } from './downloadLogs';
|
|
import {
|
|
getUnlockMethod,
|
|
isDownloadLimitEnabled,
|
|
needsUnlockBeforeDownload,
|
|
recordAdUnlockSuccess,
|
|
recordDownloadSuccess,
|
|
} from './shareGuide';
|
|
|
|
// 导出供个人中心等使用
|
|
export { getTodayDownloadCount } from './shareGuide';
|
|
|
|
/** 是否跳过下载配额与激励广告(与 shareGuide.isDownloadLimitEnabled 相反) */
|
|
function isDevBypassLimits(): boolean {
|
|
return !isDownloadLimitEnabled();
|
|
}
|
|
|
|
// 激励视频广告单元ID
|
|
const AD_UNIT_ID = 'adunit-eff5626f8b68c2e0';
|
|
|
|
let rewardedVideoAd: WechatMiniprogram.RewardedVideoAd | null = null;
|
|
|
|
function getRewardedVideoAd(): WechatMiniprogram.RewardedVideoAd | null {
|
|
if (!wx.createRewardedVideoAd) {
|
|
return null;
|
|
}
|
|
if (!rewardedVideoAd) {
|
|
rewardedVideoAd = wx.createRewardedVideoAd({
|
|
adUnitId: AD_UNIT_ID,
|
|
});
|
|
rewardedVideoAd.onLoad(() => {
|
|
console.log('激励视频广告加载成功');
|
|
});
|
|
rewardedVideoAd.onError((err) => {
|
|
console.error('激励视频广告加载失败', err);
|
|
});
|
|
}
|
|
return rewardedVideoAd;
|
|
}
|
|
|
|
/**
|
|
* 显示激励视频广告(单例实例,与改版前 downloadPrint 内逻辑一致)
|
|
*/
|
|
function showVideoAd(): Promise<boolean> {
|
|
return new Promise((resolve) => {
|
|
const videoAd = getRewardedVideoAd();
|
|
|
|
if (!videoAd) {
|
|
wx.showToast({
|
|
title: '当前环境不支持激励广告',
|
|
icon: 'none',
|
|
});
|
|
resolve(false);
|
|
return;
|
|
}
|
|
|
|
wx.showLoading({
|
|
title: '加载中...',
|
|
mask: true,
|
|
});
|
|
|
|
const onClose: WechatMiniprogram.RewardedVideoAdOnCloseCallback = (res) => {
|
|
videoAd.offClose(onClose);
|
|
wx.hideLoading();
|
|
resolve(!!(res && res.isEnded));
|
|
};
|
|
|
|
videoAd.onClose(onClose);
|
|
|
|
videoAd
|
|
.show()
|
|
.then(() => {
|
|
wx.hideLoading();
|
|
})
|
|
.catch(() => {
|
|
videoAd
|
|
.load()
|
|
.then(() => videoAd.show())
|
|
.then(() => {
|
|
wx.hideLoading();
|
|
})
|
|
.catch((err) => {
|
|
videoAd.offClose(onClose);
|
|
wx.hideLoading();
|
|
console.error('激励视频广告显示失败', err);
|
|
wx.showToast({
|
|
title: '广告加载失败,请稍后重试',
|
|
icon: 'none',
|
|
});
|
|
resolve(false);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 观看激励广告以解锁下一批下载(不执行保存)
|
|
*/
|
|
export async function tryUnlockWithRewardedAd(): Promise<boolean> {
|
|
if (isDevBypassLimits()) {
|
|
return true;
|
|
}
|
|
if (!needsUnlockBeforeDownload()) {
|
|
return true;
|
|
}
|
|
if (getUnlockMethod() !== 'ad') {
|
|
return false;
|
|
}
|
|
const watched = await showVideoAd();
|
|
if (watched) {
|
|
recordAdUnlockSuccess();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 执行下载
|
|
*/
|
|
function doDownload(
|
|
canvas: Canvas,
|
|
options: {
|
|
successToast?: string;
|
|
errorToast?: string;
|
|
trackerName?: string;
|
|
trackerMode?: string;
|
|
worksheetId?: string;
|
|
},
|
|
): Promise<boolean> {
|
|
if (options.trackerName) {
|
|
if (options.trackerMode) {
|
|
tracker.reportDownload(options.trackerName, options.trackerMode);
|
|
} else {
|
|
tracker.reportDownload(options.trackerName);
|
|
}
|
|
}
|
|
|
|
return checkAndSaveImage(canvas).then((saved) => {
|
|
if (saved) {
|
|
recordDownloadSuccess();
|
|
if (options.worksheetId) {
|
|
addDownloadLog(options.worksheetId);
|
|
}
|
|
}
|
|
return saved;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 下载打印(通用方法)
|
|
*/
|
|
export async function downloadPrint(
|
|
canvas: Canvas | null,
|
|
options: {
|
|
successToast?: string;
|
|
errorToast?: string;
|
|
trackerName?: string;
|
|
trackerMode?: string;
|
|
worksheetId?: string;
|
|
} = {},
|
|
): Promise<boolean> {
|
|
if (!canvas) {
|
|
wx.showToast({
|
|
title: options.errorToast || '请先生成内容',
|
|
icon: 'none',
|
|
});
|
|
return false;
|
|
}
|
|
|
|
if (isDevBypassLimits()) {
|
|
return doDownload(canvas, options);
|
|
}
|
|
|
|
if (needsUnlockBeforeDownload()) {
|
|
const method = getUnlockMethod();
|
|
if (method === 'share') {
|
|
return false;
|
|
}
|
|
if (method === 'ad') {
|
|
const unlocked = await tryUnlockWithRewardedAd();
|
|
if (!unlocked) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return doDownload(canvas, options);
|
|
}
|