fix: user deduct point

This commit is contained in:
zzc
2026-03-10 11:41:05 +08:00
parent d8c5c3a919
commit 2d3cefa43e
10 changed files with 140 additions and 125 deletions

View File

@@ -1,14 +1,14 @@
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").
* @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) => {
export const checkAbilityAndHandle = async (scene) => {
try {
const abilityRes = await abilityCheck(scene);
if (abilityRes.canUse) {
@@ -35,19 +35,7 @@ export const checkAbilityAndHandle = async (scene, rewardAdRef) => {
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" });
}
adManager.showVideoAd();
}
},
});

107
utils/adManager.js Normal file
View File

@@ -0,0 +1,107 @@
import { watchAdStart, watchAdReward } from "@/api/system.js";
import { useUserStore } from "@/stores/user";
class AdManager {
constructor() {
this.videoAd = null;
this.rewardToken = "";
this.isLoaded = false;
this.adUnitId = "adunit-d7a28e0357d98947"; // Default ID from RewardAd.vue
this._init();
}
_init() {
if (uni.createRewardedVideoAd) {
this.videoAd = uni.createRewardedVideoAd({
adUnitId: this.adUnitId,
});
this.videoAd.onLoad(() => {
console.log("Ad Manager: Ad Loaded");
this.isLoaded = true;
});
this.videoAd.onError((err) => {
console.error("Ad Manager: Ad Load Error", err);
this.isLoaded = false;
});
}
}
/**
* Show the rewarded video ad.
* Handles the full flow: start session -> show ad -> verify completion -> claim reward -> refresh assets.
* @returns {Promise<boolean>} Resolves with true if reward was claimed, false otherwise.
*/
async showVideoAd() {
if (!this.videoAd) {
uni.showToast({ title: "当前环境不支持广告", icon: "none" });
return false;
}
try {
// Step 1: Start Ad Session
const startRes = await watchAdStart();
if (!startRes || !startRes.rewardToken) {
uni.showToast({ title: "广告启动失败,请稍后再试", icon: "none" });
return false;
}
this.rewardToken = startRes.rewardToken;
// Step 2: Show Ad
try {
await this.videoAd.show();
} catch (e) {
// Retry load and show
await this.videoAd.load();
await this.videoAd.show();
}
// Step 3: Wait for Close
return new Promise((resolve) => {
const onCloseHandler = async (res) => {
this.videoAd.offClose(onCloseHandler); // Clean up listener
if (res && res.isEnded) {
// Step 4: Claim Reward
await this._claimReward(this.rewardToken);
resolve(true);
} else {
uni.showToast({ title: "观看完整广告才能获取奖励哦", icon: "none" });
resolve(false);
}
};
this.videoAd.onClose(onCloseHandler);
});
} catch (e) {
console.error("Ad Manager: Show failed", e);
uni.showToast({ title: "广告加载失败,请稍后再试", icon: "none" });
return false;
}
}
async _claimReward(token) {
try {
uni.showLoading({ title: "发放奖励中..." });
const res = await watchAdReward(token);
if (res) {
uni.showToast({
title: "获得50积分",
icon: "success",
});
// Refresh user assets
const userStore = useUserStore();
await userStore.fetchUserAssets();
}
} catch (e) {
console.error("Ad Manager: Reward claim failed", e);
uni.showToast({ title: "奖励发放失败", icon: "none" });
} finally {
uni.hideLoading();
}
}
}
export default new AdManager();