From d15012841cfc0efac87f33555ddd3838e64c0ef4 Mon Sep 17 00:00:00 2001 From: zzc <1761997216@qq.com> Date: Wed, 4 Mar 2026 12:35:59 +0800 Subject: [PATCH] fix: ability --- api/system.js | 2 +- pages/avatar/download.vue | 43 ++++-------------------- pages/index/index.vue | 2 +- pages/wallpaper/detail.vue | 41 ++++------------------- pages/wallpaper/index.vue | 41 ++++------------------- utils/ability.js | 67 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 90 insertions(+), 106 deletions(-) create mode 100644 utils/ability.js diff --git a/api/system.js b/api/system.js index d841023..4ca2c1a 100644 --- a/api/system.js +++ b/api/system.js @@ -2,7 +2,7 @@ import { request } from "@/utils/request.js"; export const abilityCheck = async (scene) => { return request({ - url: "/api/blessing/ability/check?scene=" + scene, + url: "/api/ability/check?scene=" + scene, method: "GET", }); }; diff --git a/pages/avatar/download.vue b/pages/avatar/download.vue index e6305e8..e7a7a51 100644 --- a/pages/avatar/download.vue +++ b/pages/avatar/download.vue @@ -116,7 +116,8 @@ import { saveViewRequest, } from "@/utils/common.js"; import { onShareAppMessage, onShareTimeline, onLoad } from "@dcloudio/uni-app"; -import { getShareReward, abilityCheck, watchAdReward } from "@/api/system.js"; +import { getShareReward, watchAdReward } from "@/api/system.js"; +import { checkAbilityAndHandle } from "@/utils/ability.js"; import { useUserStore } from "@/stores/user"; import NavBar from "@/components/NavBar/NavBar.vue"; import RewardAd from "@/components/RewardAd/RewardAd.vue"; @@ -313,41 +314,11 @@ const downloadAvatar = async (item) => { return; } - // Use avatar specific ability check if exists, otherwise reuse generic or skip? - // Assuming 'avatar_download' ability check exists or similar - const abilityRes = await abilityCheck("avatar_download"); - if (!abilityRes.canUse) { - if ( - abilityRes?.blockType === "need_share" && - abilityRes?.message === "分享可继续" - ) { - uni.showToast({ - title: "分享给好友即可下载", - icon: "none", - }); - return; - } - if ( - abilityRes?.blockType === "need_ad" && - abilityRes?.message === "观看广告可继续" - ) { - uni.showModal({ - title: "积分不足", - content: "观看广告可获得50积分,继续下载", - success: (res) => { - if (res.confirm) { - rewardAdRef.value.show(); - } - }, - }); - return; - } - uni.showToast({ - title: "您今日下载次数已用完,明日再试", - icon: "none", - }); - return; - } + const canProceed = await checkAbilityAndHandle( + "avatar_download", + rewardAdRef, + ); + if (!canProceed) return; uni.showLoading({ title: "下载中..." }); try { diff --git a/pages/index/index.vue b/pages/index/index.vue index 1b3411c..49f9e09 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -272,7 +272,7 @@ - 每日签到:+40积分 + 每日签到:+20积分 diff --git a/pages/wallpaper/detail.vue b/pages/wallpaper/detail.vue index 7ecb9ae..ac2bc19 100644 --- a/pages/wallpaper/detail.vue +++ b/pages/wallpaper/detail.vue @@ -115,7 +115,8 @@ import { trackRecord, } from "@/utils/common.js"; import { useUserStore } from "@/stores/user"; -import { abilityCheck, watchAdReward } from "@/api/system.js"; +import { watchAdReward } from "@/api/system.js"; +import { checkAbilityAndHandle } from "@/utils/ability.js"; import LoginPopup from "@/components/LoginPopup/LoginPopup.vue"; import RewardAd from "@/components/RewardAd/RewardAd.vue"; @@ -287,39 +288,11 @@ const downloadWallpaper = async () => { return; } - const abilityRes = await abilityCheck("wallpaper_download"); - if (!abilityRes.canUse) { - if ( - abilityRes?.blockType === "need_share" && - abilityRes?.message === "分享可继续" - ) { - uni.showToast({ - title: "分享给好友即可下载", - icon: "none", - }); - return; - } - if ( - abilityRes?.blockType === "need_ad" && - abilityRes?.message === "观看广告可继续" - ) { - uni.showModal({ - title: "积分不足", - content: "观看广告可获得50积分,继续下载", - success: (res) => { - if (res.confirm) { - rewardAdRef.value.show(); - } - }, - }); - return; - } - uni.showToast({ - title: "您今日壁纸下载次数已用完,明日再试", - icon: "none", - }); - return; - } + const canProceed = await checkAbilityAndHandle( + "wallpaper_download", + rewardAdRef, + ); + if (!canProceed) return; uni.showLoading({ title: "下载中..." }); try { diff --git a/pages/wallpaper/index.vue b/pages/wallpaper/index.vue index 8b399eb..0d6edae 100644 --- a/pages/wallpaper/index.vue +++ b/pages/wallpaper/index.vue @@ -104,7 +104,8 @@ import { getShareToken, } from "@/utils/common.js"; import { onShareAppMessage, onShareTimeline, onLoad } from "@dcloudio/uni-app"; -import { getShareReward, abilityCheck, watchAdReward } from "@/api/system.js"; +import { getShareReward, watchAdReward } from "@/api/system.js"; +import { checkAbilityAndHandle } from "@/utils/ability.js"; import { useUserStore } from "@/stores/user"; import { saveViewRequest, trackRecord } from "@/utils/common.js"; import NavBar from "@/components/NavBar/NavBar.vue"; @@ -303,39 +304,11 @@ const downloadWallpaper = async (item) => { return; } - const abilityRes = await abilityCheck("wallpaper_download"); - if (!abilityRes.canUse) { - if ( - abilityRes?.blockType === "need_share" && - abilityRes?.message === "分享可继续" - ) { - uni.showToast({ - title: "分享给好友即可下载", - icon: "none", - }); - return; - } - if ( - abilityRes?.blockType === "need_ad" && - abilityRes?.message === "观看广告可继续" - ) { - uni.showModal({ - title: "积分不足", - content: "观看广告可获得50积分,继续下载", - success: (res) => { - if (res.confirm) { - rewardAdRef.value.show(); - } - }, - }); - return; - } - uni.showToast({ - title: "您今日壁纸下载次数已用完,明日再试", - icon: "none", - }); - return; - } + const canProceed = await checkAbilityAndHandle( + "wallpaper_download", + rewardAdRef, + ); + if (!canProceed) return; uni.showLoading({ title: "下载中..." }); try { diff --git a/utils/ability.js b/utils/ability.js new file mode 100644 index 0000000..01e9740 --- /dev/null +++ b/utils/ability.js @@ -0,0 +1,67 @@ +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} - 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; + } + + uni.showToast({ + title: "您今日下载次数已用完,明日再试", + icon: "none", + }); + return false; + } catch (e) { + console.error("Ability check failed", e); + uni.showToast({ title: "系统繁忙,请稍后重试", icon: "none" }); + return false; + } +};