import { abilityCheck } from "@/api/system"; import adManager from "@/utils/adManager"; /** * Checks if a user has the ability to perform an action (e.g., download). * Handles common blocking scenarios like "need_share" or "need_ad". * * @param {string} scene - The scene identifier for the ability check (e.g., "wallpaper_download"). * @returns {Promise} - Returns true if the action can proceed, false otherwise. */ export const checkAbilityAndHandle = async (scene) => { try { const abilityRes = await abilityCheck(scene); if (abilityRes.canUse) { return true; } if ( abilityRes?.blockType === "need_share" && abilityRes?.message === "分享可继续" ) { uni.showToast({ title: "分享给好友即可下载", icon: "none", }); return false; } if ( abilityRes?.blockType === "need_ad" && abilityRes?.message === "观看广告可继续" ) { uni.showModal({ title: "积分不足", content: "观看广告可获得50积分,继续下载", success: (res) => { if (res.confirm) { adManager.showVideoAd(); } }, }); return false; } if (abilityRes?.blockType === "need_vip") { uni.showModal({ title: "会员激活", content: "会员激活即可继续使用该功能,是否前往开通?", success: (res) => { if (res.confirm) { uni.navigateTo({ url: "/pages/mine/vip", }); } }, }); return false; } uni.showToast({ title: "您今日下载次数已用完,明日再试", icon: "none", }); return false; } catch (e) { console.error("Ability check failed", e); uni.showToast({ title: "系统繁忙,请稍后重试", icon: "none" }); return false; } }; /** * Gets the display label for an unlock type. * @param {string} unlockType - The unlock type identifier. * @returns {string} - The display label. */ export const getUnlockLabel = (unlockType) => { if (!unlockType) return "解锁"; switch (unlockType) { case "sing3": return "连续签到3天"; case "sing5": return "连续签到5天"; case "sing7": return "连续签到7天"; case "ad": return "广告"; case "vip": return "VIP"; default: return "解锁"; } };