Compare commits
No commits in common. "d6fba57afcdcf8c39be2430d48b0c8898f39c78b" and "be71df2ecd47f1259d43fba22f66b3af0c814c0e" have entirely different histories.
d6fba57afc
...
be71df2ecd
|
|
@ -1,3 +1,8 @@
|
||||||
|
|
||||||
|
@tailwind base;
|
||||||
|
@tailwind components;
|
||||||
|
@tailwind utilities;
|
||||||
|
|
||||||
#app{
|
#app{
|
||||||
width: 100vw;
|
width: 100vw;
|
||||||
height: 100vh;
|
height: 100vh;
|
||||||
|
|
|
||||||
|
|
@ -17,28 +17,16 @@ interface UseAdOptions {
|
||||||
onReward?: AdRewardCallback; // 观看完整广告后的奖励回调
|
onReward?: AdRewardCallback; // 观看完整广告后的奖励回调
|
||||||
onClose?: (isEnded: boolean) => void; // 广告关闭回调,传入是否完整观看
|
onClose?: (isEnded: boolean) => void; // 广告关闭回调,传入是否完整观看
|
||||||
}
|
}
|
||||||
// 创建平台广告实例
|
|
||||||
const factory = createPlatformFactory()
|
|
||||||
export function useAd(options?: UseAdOptions): UseAdReturn {
|
export function useAd(options?: UseAdOptions): UseAdReturn {
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [adAvailable, setAdAvailable] = useState(false);
|
const [adAvailable, setAdAvailable] = useState(false);
|
||||||
const adRef = useRef<RewardedVideoAd | null>(null);
|
const adRef = useRef<RewardedVideoAd | null>(null);
|
||||||
const platform = factory.getPlatform()
|
|
||||||
|
|
||||||
// 检查当前平台是否支持广告
|
|
||||||
const isAdSupportedPlatform = platform === 'bytedance';
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// 如果当前平台不支持广告,直接设置为不可用状态
|
|
||||||
if (!isAdSupportedPlatform) {
|
|
||||||
console.log(`当前平台 ${platform} 未开通广告,跳过广告初始化`);
|
|
||||||
setAdAvailable(false);
|
|
||||||
setLoading(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 创建平台广告实例
|
||||||
|
const factory = createPlatformFactory()
|
||||||
adRef.current = factory.createRewardedVideoAd();
|
adRef.current = factory.createRewardedVideoAd();
|
||||||
const ad = adRef.current!;
|
const ad = adRef.current!;
|
||||||
|
|
||||||
|
|
@ -124,23 +112,14 @@ export function useAd(options?: UseAdOptions): UseAdReturn {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
return () => {}; // 返回空的清理函数
|
return () => {}; // 返回空的清理函数
|
||||||
}
|
}
|
||||||
}, [options, isAdSupportedPlatform, platform]);
|
}, [options]);
|
||||||
|
|
||||||
// 显示广告方法
|
// 显示广告方法
|
||||||
const showAd = useCallback(() => {
|
const showAd = useCallback(() => {
|
||||||
// 如果当前平台不支持广告,直接给予奖励并触发关闭回调
|
|
||||||
if (!isAdSupportedPlatform) {
|
|
||||||
console.log(`当前平台 ${platform} 未开通广告,直接给予奖励`);
|
|
||||||
options?.onReward?.();
|
|
||||||
options?.onClose?.(true); // 模拟广告播放完成
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!adAvailable) {
|
if (!adAvailable) {
|
||||||
console.warn('广告不可用,跳过广告播放');
|
console.warn('广告不可用,跳过广告播放');
|
||||||
// 如果广告不可用,直接给予奖励
|
// 如果广告不可用,直接给予奖励
|
||||||
options?.onReward?.();
|
options?.onReward?.();
|
||||||
options?.onClose?.(true); // 模拟广告播放完成
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -153,24 +132,17 @@ export function useAd(options?: UseAdOptions): UseAdReturn {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
// 如果显示失败,也给予奖励作为降级处理
|
// 如果显示失败,也给予奖励作为降级处理
|
||||||
options?.onReward?.();
|
options?.onReward?.();
|
||||||
options?.onClose?.(true); // 模拟广告播放完成
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [adAvailable, options, isAdSupportedPlatform, platform]);
|
}, [adAvailable, options]);
|
||||||
|
|
||||||
// 加载广告方法
|
// 加载广告方法
|
||||||
const loadAd = useCallback(() => {
|
const loadAd = useCallback(() => {
|
||||||
// 如果当前平台不支持广告,直接跳过加载
|
|
||||||
if (!isAdSupportedPlatform) {
|
|
||||||
console.log(`当前平台 ${platform} 未开通广告,跳过广告加载`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (adRef.current) {
|
if (adRef.current) {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
adRef.current.load();
|
adRef.current.load();
|
||||||
}
|
}
|
||||||
}, [isAdSupportedPlatform, platform]);
|
}, []);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
showAd,
|
showAd,
|
||||||
|
|
|
||||||
|
|
@ -70,13 +70,14 @@ const ResultPage: React.FC = () => {
|
||||||
} else if (!adAvailable) {
|
} else if (!adAvailable) {
|
||||||
// 广告不可用(如流量主未开通),直接允许下载
|
// 广告不可用(如流量主未开通),直接允许下载
|
||||||
showToast({
|
showToast({
|
||||||
title: '已为您跳过广告',
|
title: '已为您跳过广告,直接下载',
|
||||||
icon: 'success',
|
icon: 'success',
|
||||||
duration: 1500
|
duration: 2000
|
||||||
})
|
})
|
||||||
|
setHasWatchedAd(true) // 标记为已"观看",允许下载
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
handleDownloadImages(true) // 跳过广告检查,直接下载
|
handleDownloadImages()
|
||||||
}, 800)
|
}, 500)
|
||||||
} else {
|
} else {
|
||||||
// 还没观看广告,先播放广告
|
// 还没观看广告,先播放广告
|
||||||
showAd()
|
showAd()
|
||||||
|
|
@ -84,8 +85,8 @@ const ResultPage: React.FC = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 下载图片到本地相册
|
// 下载图片到本地相册
|
||||||
const handleDownloadImages = async (skipAdCheck: boolean = false) => {
|
const handleDownloadImages = async () => {
|
||||||
if (!skipAdCheck && !hasWatchedAd) {
|
if (!hasWatchedAd) {
|
||||||
showToast({
|
showToast({
|
||||||
title: '请先观看广告',
|
title: '请先观看广告',
|
||||||
icon: 'none',
|
icon: 'none',
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue