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

108 lines
2.9 KiB
JavaScript
Raw Normal View History

2026-03-10 11:41:05 +08:00
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();