import { _decorator, Component, Node } from 'cc'; import { NoticeManager } from '../../engines/notices/NoticeManager'; import { BannerLoop } from '../BannerLoop'; import { BasePlatform } from './BasePlatform'; const { ccclass, property } = _decorator; @ccclass('WeChatPlatform') export class WeChatPlatform extends BasePlatform { private wx = window['wx']; private __bannerIDs: string[] = ["adunit-91be4efcdebcba8d", "adunit-ff8ca74a9b61c63b"]; private __bannerLoop: BannerLoop; private __windowWidth: number; private __windowHeight: number; //激励广告 private __rewardedVideoAd: any; private __rewardedSuccess: Function; private __rewardedfailure: Function; private __systemInfo: any; constructor() { super() this.__systemInfo = this.wx.getSystemInfoSync(); this.__windowWidth = this.__systemInfo.screenWidth; this.__windowHeight = this.__systemInfo.screenHeight; } login(success: Function, failure: Function): void { this.__bannerLoop = new BannerLoop(); this.__bannerLoop.init(2, this.__bannerIDs, this.__bannerCreate.bind(this), this.__bannerDestory.bind(this)); //激励广告 this.__initRewardedVideoAd(); } private __initRewardedVideoAd(): void { try { //提前创建广告 this.__rewardedVideoAd = this.wx.createRewardedVideoAd({ adUnitId: 'adunit-06ba7bdaa32c6cc5' }) //拉取成功 this.__rewardedVideoAd.onLoad(() => { console.log("激励广告拉取成功"); }); //拉取失败 this.__rewardedVideoAd.onError(err => { console.log("激励广告拉取失败"); }); //激励广告回调 this.__rewardedVideoAd.onClose(res => { // 用户点击了【关闭广告】按钮 // 小于 2.1.0 的基础库版本,res 是一个 undefined if (res && res.isEnded || res === undefined) { console.log("广告看完了"); // 正常播放结束,可以下发游戏奖励 if (this.__rewardedSuccess) { this.__rewardedSuccess(); this.__rewardedSuccess = null; } this.__rewardedfailure = null; } else { console.log("看广告中途退出!"); // 播放中途退出,不下发游戏奖励 if (this.__rewardedfailure) { this.__rewardedfailure(); this.__rewardedfailure = null; } this.__rewardedSuccess = null; } }); } catch (error) { console.error("初始化激励广告失败"); } } private __bannerCreate(id: string): any { let targetBannerAdWidth: number = this.__windowWidth; let resizeBool: boolean = false; let banner: any = this.wx.createBannerAd( { adUnitId: id, style: { width: targetBannerAdWidth, left: 0, top: this.__windowHeight - (targetBannerAdWidth / 16 * 9) } } ); // 尺寸调整时会触发回调 // 注意:如果在回调里再次调整尺寸,要确保不要触发死循环!!! banner.onResize(size => { if (resizeBool == false) { resizeBool = true; banner.style.top = this.__windowHeight - size.height; banner.style.left = (this.__windowWidth - size.width) / 2; } }); banner.onError(err => { console.log("err", err) }) return banner; } private __bannerDestory(banner: any): void { banner.destroy(); } navigate2Mini(data: any, success?: Function, failure?: Function): void { throw new Error('Method not implemented.'); } shareMessage(data: any, success?: Function, failure?: Function): void { throw new Error('Method not implemented.'); } showBanner(data: any): void { console.log("显示Banner"); this.__bannerLoop.showBanner(); } hideBanner(): void { console.log("隐藏Banner"); this.__bannerLoop.hideBanner(); } showRewardedVideo(success?: Function, failure?: Function): void { console.log("显示激励广告"); this.__rewardedSuccess = success; this.__rewardedfailure = failure; if (this.__rewardedVideoAd) { this.__rewardedVideoAd.show().catch(err => { this.__rewardedVideoAd.load().then(() => this.__rewardedVideoAd.show()) }); } } showInsertAd(success?: Function, failure?: Function): void { throw new Error('Method not implemented.'); } showNativeAd(success?: Function, failure?: Function): void { throw new Error('Method not implemented.'); } sendEvent(eventName: string, data?: any): void { throw new Error('Method not implemented.'); } setLoadingProgress(progress: number): void { throw new Error('Method not implemented.'); } loadingComplete(completeHandler?: Function): void { throw new Error('Method not implemented.'); } getRandomPageAd(num: number): any[] { throw new Error('Method not implemented.'); } /** * 开始游戏 * @param num */ startBranchAnalytics(num: string): void { try { if(this.wx){ this.wx.reportUserBehaviorBranchAnalytics({ branchId: 'BCBgAAoXHx5d0WLb_vUvr7', branchDim: num, eventType: 1 }); } } catch (error) { } } /** * 成功 */ successBranchAnalytics(num: string): void { try { if(this.wx){ this.wx.reportUserBehaviorBranchAnalytics({ branchId: 'BCBgAAoXHx5d0WLb_vUvr-', branchDim: num, eventType: 1 }); } } catch (error) { } } /** * 失败 */ failBranchAnalytics(num: string): void { try { if(this.wx){ this.wx.reportUserBehaviorBranchAnalytics({ branchId: 'BCBgAAoXHx5d0WLb_vUvr9', branchDim: num, eventType: 1 }); } } catch (error) { } } }