import { BannerLoop } from "../BannerLoop"; import { LiangLiangSDK } from "../libs/liangliangSDK"; import { BasePlatform } from "./BasePlatform"; 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; //分享语录 private _shareTitles:string[]=[ '打僵尸,我是最强的,不服来战', '你的空投到了!点击领取!', '什么,这游戏还能这么玩?!', '这一波我有点扛不住啊!', '枪神?我预定了,不服来战!', '我终于得到了“哒哒哒”冒蓝光的加特林', '老同学,还记得当年一起打的僵尸吗', '人类!就你这样的手速,最后将会沦为我的食物!', '啊呜,啊呜,啊呜,虽然二哈叫声也这样,但我可是会吃人的!', '当我扣下扳机的那一刻,请你从哪里来回哪去!' ] 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(); super.login(success,failure); } 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.'); } onShareAppMessage():void{ try { this.wx.showShareMenu({ withShareTicket: true }); let shareTitle = this._shareTitles[Math.floor(Math.random()*this._shareTitles.length)] this.wx.onShareAppMessage(()=>{ return { title: shareTitle, imageUrl: "https://www.maoxingame.com/QiangShenZhanJi/share/share1.jpg" } }) } catch (error) { } } showBanner(data: any): void { console.log("显示Banner"); if(this.__bannerLoop){ this.__bannerLoop.showBanner(); } } hideBanner(): void { console.log("隐藏Banner"); if(this.__bannerLoop){ 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[] { return LiangLiangSDK.GetList(); } /** * 通用打点 * @param str 打点数据 * @param num 参数 */ branchAnalytics(str: string, num?: string): void { try { if (this.wx) { let Analytics = {} if (num) { Analytics = { branchId: str, branchDim: num, eventType: 1 } } else { Analytics = { branchId: str, eventType: 1 } } this.wx.reportUserBehaviorBranchAnalytics(Analytics); } } catch (error) { console.log("打点失败:", str) } } ///////////下面的版本,如果上面成功,即下个版本删除2020-10-19, 1.0.7////////////////////// /** * 开始游戏 * @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) { } } }