Files
spring-festival-greetings/utils/ability.js
2026-03-06 14:14:27 +08:00

83 lines
2.5 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";
/**
* 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").
* @param {Object} rewardAdRef - The ref to the RewardAd component (must have a .value.show() method or be the instance itself).
* @returns {Promise<boolean>} - Returns true if the action can proceed, false otherwise.
*/
export const checkAbilityAndHandle = async (scene, rewardAdRef) => {
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) {
// Check if rewardAdRef is a ref (has .value) or the component instance itself
if (
rewardAdRef &&
rewardAdRef.value &&
typeof rewardAdRef.value.show === "function"
) {
rewardAdRef.value.show();
} else if (rewardAdRef && typeof rewardAdRef.show === "function") {
rewardAdRef.show();
} else {
console.error("RewardAd component reference is invalid");
uni.showToast({ title: "广告加载失败", icon: "none" });
}
}
},
});
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;
}
};