feat: 下载次数限制,免费额度用完后第一次需要分享,之后需要看广告
This commit is contained in:
+135
-36
@@ -1,15 +1,28 @@
|
||||
/**
|
||||
* 分享引导工具
|
||||
* 用于控制每日免费下载次数和分享引导
|
||||
* 分享引导与下载配额
|
||||
* - 当日第 1 次满批:须分享解锁,弹窗引导,不分享不可下载
|
||||
* - 当日第 2 次及以后满批:直接播放激励广告解锁
|
||||
*/
|
||||
|
||||
import {
|
||||
downloadFreeBatchSize,
|
||||
enableDownloadLimitInDevelop,
|
||||
} from '../config/config';
|
||||
|
||||
const STORAGE_KEY_DOWNLOAD_DATE = 'downloadDate';
|
||||
const STORAGE_KEY_DOWNLOAD_COUNT = 'downloadCount';
|
||||
const STORAGE_KEY_SHARED_DATE = 'sharedDate';
|
||||
const STORAGE_KEY_DOWNLOADS_SINCE_UNLOCK = 'downloadsSinceUnlock';
|
||||
/** 当日已完成解锁次数(分享或广告各计 1 次) */
|
||||
const STORAGE_KEY_DAILY_UNLOCK_COUNT = 'dailyUnlockCount';
|
||||
|
||||
export type DownloadUnlockMethod = 'share' | 'ad';
|
||||
|
||||
/** 每批免费下载次数(来自 config,至少为 1) */
|
||||
export function getFreeDownloadBatchSize(): number {
|
||||
const n = Number(downloadFreeBatchSize);
|
||||
return Number.isFinite(n) && n >= 1 ? Math.floor(n) : 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取今天的日期字符串(YYYY-MM-DD格式)
|
||||
*/
|
||||
function getTodayDateString(): string {
|
||||
const today = new Date();
|
||||
const year = today.getFullYear();
|
||||
@@ -18,54 +31,140 @@ function getTodayDateString(): string {
|
||||
return `${year}-${month}-${day}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查今天是否已经下载过
|
||||
* @returns true表示今天已下载过,false表示今天未下载
|
||||
*/
|
||||
export function hasDownloadedToday(): boolean {
|
||||
/** 跨日时重置当日计数与解锁批次 */
|
||||
function ensureDailyReset(): void {
|
||||
const today = getTodayDateString();
|
||||
const downloadDate = wx.getStorageSync(STORAGE_KEY_DOWNLOAD_DATE);
|
||||
return downloadDate === today;
|
||||
if (downloadDate !== today) {
|
||||
wx.setStorageSync(STORAGE_KEY_DOWNLOAD_DATE, today);
|
||||
wx.setStorageSync(STORAGE_KEY_DOWNLOAD_COUNT, 0);
|
||||
wx.setStorageSync(STORAGE_KEY_DOWNLOADS_SINCE_UNLOCK, 0);
|
||||
wx.setStorageSync(STORAGE_KEY_DAILY_UNLOCK_COUNT, 0);
|
||||
}
|
||||
}
|
||||
|
||||
/** 当日已完成解锁次数 */
|
||||
export function getDailyUnlockCount(): number {
|
||||
ensureDailyReset();
|
||||
return wx.getStorageSync(STORAGE_KEY_DAILY_UNLOCK_COUNT) || 0;
|
||||
}
|
||||
|
||||
function incrementDailyUnlockCount(): void {
|
||||
ensureDailyReset();
|
||||
const count = getDailyUnlockCount();
|
||||
wx.setStorageSync(STORAGE_KEY_DAILY_UNLOCK_COUNT, count + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录今天的下载成功
|
||||
* 获取今日总下载次数(供个人中心等展示)
|
||||
*/
|
||||
export function getTodayDownloadCount(): number {
|
||||
ensureDailyReset();
|
||||
return wx.getStorageSync(STORAGE_KEY_DOWNLOAD_COUNT) || 0;
|
||||
}
|
||||
|
||||
/** 当前批次内已下载次数 */
|
||||
export function getDownloadsSinceUnlock(): number {
|
||||
ensureDailyReset();
|
||||
return wx.getStorageSync(STORAGE_KEY_DOWNLOADS_SINCE_UNLOCK) || 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否已达本批次上限,下载前需先解锁
|
||||
*/
|
||||
export function needsUnlockBeforeDownload(): boolean {
|
||||
return getDownloadsSinceUnlock() >= getFreeDownloadBatchSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前满批时应使用的解锁方式
|
||||
*/
|
||||
export function getUnlockMethod(): DownloadUnlockMethod | null {
|
||||
if (!isDownloadLimitEnabled() || !needsUnlockBeforeDownload()) {
|
||||
return null;
|
||||
}
|
||||
return getDailyUnlockCount() === 0 ? 'share' : 'ad';
|
||||
}
|
||||
|
||||
/**
|
||||
* 分享或看广告成功后,重置批次计数,可再免费下载一批
|
||||
*/
|
||||
export function resetUnlockQuota(): void {
|
||||
ensureDailyReset();
|
||||
wx.setStorageSync(STORAGE_KEY_DOWNLOADS_SINCE_UNLOCK, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录一次下载成功(总次数 + 当前批次次数各 +1)
|
||||
*/
|
||||
export function recordDownloadSuccess(): void {
|
||||
ensureDailyReset();
|
||||
const today = getTodayDateString();
|
||||
const total = getTodayDownloadCount();
|
||||
const sinceUnlock = getDownloadsSinceUnlock();
|
||||
wx.setStorageSync(STORAGE_KEY_DOWNLOAD_DATE, today);
|
||||
const count = wx.getStorageSync(STORAGE_KEY_DOWNLOAD_COUNT) || 0;
|
||||
wx.setStorageSync(STORAGE_KEY_DOWNLOAD_COUNT, count + 1);
|
||||
wx.setStorageSync(STORAGE_KEY_DOWNLOAD_COUNT, total + 1);
|
||||
wx.setStorageSync(STORAGE_KEY_DOWNLOADS_SINCE_UNLOCK, sinceUnlock + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查今天是否已经分享过
|
||||
* @returns true表示今天已分享过,false表示今天未分享
|
||||
*/
|
||||
export function hasSharedToday(): boolean {
|
||||
const today = getTodayDateString();
|
||||
const sharedDate = wx.getStorageSync(STORAGE_KEY_SHARED_DATE);
|
||||
return sharedDate === today;
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录今天的分享成功
|
||||
* 记录分享解锁成功
|
||||
*/
|
||||
export function recordShareSuccess(): void {
|
||||
const today = getTodayDateString();
|
||||
wx.setStorageSync(STORAGE_KEY_SHARED_DATE, today);
|
||||
resetUnlockQuota();
|
||||
incrementDailyUnlockCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否需要显示分享引导
|
||||
* @returns true表示需要显示分享引导,false表示不需要
|
||||
* 记录激励广告解锁成功
|
||||
*/
|
||||
export function shouldShowShareGuide(): boolean {
|
||||
// 如果今天已经分享过,不需要显示引导
|
||||
const env = getApp().globalData.env;
|
||||
if (env === 'develop' || hasSharedToday()) {
|
||||
export function recordAdUnlockSuccess(): void {
|
||||
resetUnlockQuota();
|
||||
incrementDailyUnlockCount();
|
||||
}
|
||||
|
||||
function isDevelopEnv(): boolean {
|
||||
try {
|
||||
const env = getApp().globalData.env;
|
||||
if (env === 'develop') {
|
||||
return true;
|
||||
}
|
||||
const accountEnv =
|
||||
wx.getAccountInfoSync().miniProgram.envVersion || 'release';
|
||||
return accountEnv === 'develop';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
// 如果今天已经下载过,需要显示引导
|
||||
return hasDownloadedToday();
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否启用下载配额(分享引导、激励广告)
|
||||
*/
|
||||
export function isDownloadLimitEnabled(): boolean {
|
||||
if (!isDevelopEnv()) {
|
||||
return true;
|
||||
}
|
||||
return enableDownloadLimitInDevelop;
|
||||
}
|
||||
|
||||
/** 当日首次满批:须分享解锁,弹分享引导窗 */
|
||||
export function shouldShowShareGuide(): boolean {
|
||||
return getUnlockMethod() === 'share';
|
||||
}
|
||||
|
||||
/** 当日第 2 次及以后满批:直接播放激励广告 */
|
||||
export function shouldUnlockWithAd(): boolean {
|
||||
return getUnlockMethod() === 'ad';
|
||||
}
|
||||
|
||||
/** 分享解锁弹窗文案 */
|
||||
export function getShareGuidePopupCopy(): {
|
||||
message: string;
|
||||
hint: string;
|
||||
} {
|
||||
const n = getFreeDownloadBatchSize();
|
||||
return {
|
||||
message: `免费下载次数已用完`,
|
||||
hint: '分享给好友后即可继续下载',
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user