Files
spring-festival-greetings/utils/ability.js

92 lines
2.3 KiB
JavaScript
Raw Normal View History

2026-03-04 12:35:59 +08:00
import { abilityCheck } from "@/api/system";
2026-03-10 11:41:05 +08:00
import adManager from "@/utils/adManager";
2026-03-04 12:35:59 +08:00
/**
* 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.
*/
2026-03-10 11:41:05 +08:00
export const checkAbilityAndHandle = async (scene) => {
2026-03-04 12:35:59 +08:00
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) {
2026-03-10 11:41:05 +08:00
adManager.showVideoAd();
2026-03-04 12:35:59 +08:00
}
},
});
return false;
}
2026-03-06 14:14:27 +08:00
if (abilityRes?.blockType === "need_vip") {
uni.showModal({
title: "会员激活",
content: "会员激活即可继续使用该功能,是否前往开通?",
success: (res) => {
if (res.confirm) {
uni.navigateTo({
url: "/pages/mine/vip",
});
}
},
});
return false;
}
2026-03-04 12:35:59 +08:00
uni.showToast({
title: "您今日下载次数已用完,明日再试",
icon: "none",
});
return false;
} catch (e) {
console.error("Ability check failed", e);
uni.showToast({ title: "系统繁忙,请稍后重试", icon: "none" });
return false;
}
};
2026-03-11 16:36:54 +08:00
/**
* 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 "解锁";
}
};