/** * 分享引导与下载配额 * - 当日第 1 次满批:须分享解锁,弹窗引导,不分享不可下载 * - 当日第 2 次及以后满批:直接播放激励广告解锁 */ import { downloadFreeBatchSize, enableDownloadLimitInDevelop, } from '../config/config'; const STORAGE_KEY_DOWNLOAD_DATE = 'downloadDate'; const STORAGE_KEY_DOWNLOAD_COUNT = 'downloadCount'; 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; } function getTodayDateString(): string { const today = new Date(); const year = today.getFullYear(); const month = String(today.getMonth() + 1).padStart(2, '0'); const day = String(today.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } /** 跨日时重置当日计数与解锁批次 */ function ensureDailyReset(): void { const today = getTodayDateString(); const downloadDate = wx.getStorageSync(STORAGE_KEY_DOWNLOAD_DATE); 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); wx.setStorageSync(STORAGE_KEY_DOWNLOAD_COUNT, total + 1); wx.setStorageSync(STORAGE_KEY_DOWNLOADS_SINCE_UNLOCK, sinceUnlock + 1); } /** * 记录分享解锁成功 */ export function recordShareSuccess(): void { resetUnlockQuota(); incrementDailyUnlockCount(); } /** * 记录激励广告解锁成功 */ 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; } } /** * 是否启用下载配额(分享引导、激励广告) */ 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: '分享给好友后即可继续下载', }; }