Files
spring-festival-greetings/utils/ability.js
2026-03-11 16:36:54 +08:00

92 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<boolean>} - 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 "sing1":
return "登录1天";
case "ad":
return "广告";
case "vip":
return "VIP";
default:
return "解锁";
}
};