123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- var AdsParam = require("AdsParam");
- var AdsMgr = cc.Class({
- statics:{
- //微信 wx
- //头条 tt
- //百度 baidu
- platform:"wx",
- //对应的平台的 bannerId 采用加后缀的方式
- bannerId_baidu:"6191742",
- bannerId_wx:"adunit-42bcaae6cafaa7c6",
- bannerId_tt:"6152042",
- //百度广告id 激励视频
- bdVideoId_1:"6191756",
- bdVideoId_2:"6191755",
- bdVideoId_3:"6191754",
- bdVideoId_4:"6191753",
- bdVideoId_5:"6191752",
- bdVideoId_6:"6191751",
- bdVideoId_7:"6191749",
- bdVideoId_8:"6191748",
- bdVideoId_9:"6191747",
- bdVideoId_10:"6191744",
- //微信广告id 激励视频
- wxVideoId_1:"",
- wxVideoId_2:"",
- wxVideoId_3:"",
- wxVideoId_4:"",
- wxVideoId_5:"",
- wxVideoId_6:"",
- wxVideoId_7:"",
- wxVideoId_8:"",
- wxVideoId_9:"",
- wxVideoId_10:"",
- allScreen:false,
- //首先确定使用的平台
- Init:function () {
- if(!window.wx && !window.tt && !window.swan)
- this.platform="pc";
- this.allScreen = false;
- },
- //获取广告点ID
- getBannerAdId:function()
- {
- if(this.platform == "wx")
- {
- return this.bannerId_wx;
- }
- else if(this.platform == "baidu")
- {
- return this.bannerId_baidu;
- }
- else if(this.platform == "tt")
- {
- return this.bannerId_tt;
- }
- return null;
- },
- //获取广告点ID 这里配置 视频奖励广告 Id 由于会有多个广告点 根据后台设置来顶
- getVideoAdId:function(adName)
- {
- var adId;
- if(this.platform == "wx")
- {
- switch(adName)
- {
- case AdsParam.PointA:
- adId = this.wxVideoId_1;
- break;
- case AdsParam.PointB:
- adId = this.wxVideoId_2;
- break;
- case AdsParam.PointC:
- adId = this.wxVideoId_3;
- break;
- case AdsParam.PointD:
- adId = this.wxVideoId_4;
- break;
- case AdsParam.PointE:
- adId = this.wxVideoId_5;
- break;
- case AdsParam.PointF:
- adId = this.wxVideoId_6;
- break;
- case AdsParam.PointG:
- adId = this.wxVideoId_7;
- break;
- case AdsParam.PointH:
- adId = this.wxVideoId_8;
- break;
- case AdsParam.PointI:
- adId = this.wxVideoId_9;
- break;
- case AdsParam.PointJ:
- adId = this.wxVideoId_10;
- break;
- }
- }
- else if(this.platform == "baidu")
- {
- switch(adName)
- {
- case AdsParam.PointA:
- adId = this.bdVideoId_1;
- break;
- case AdsParam.PointB:
- adId = this.bdVideoId_2;
- break;
- case AdsParam.PointC:
- adId = this.bdVideoId_3;
- break;
- case AdsParam.PointD:
- adId = this.bdVideoId_4;
- break;
- case AdsParam.PointE:
- adId = this.bdVideoId_5;
- break;
- case AdsParam.PointF:
- adId = this.bdVideoId_6;
- break;
- case AdsParam.PointG:
- adId = this.bdVideoId_7;
- break;
- case AdsParam.PointH:
- adId = this.bdVideoId_8;
- break;
- case AdsParam.PointI:
- adId = this.bdVideoId_9;
- break;
- case AdsParam.PointJ:
- adId = this.bdVideoId_10;
- break;
- }
- }
- return adId;
- },
- ShowBannerAds:function()
- {
- var self = this;
- if(this.bannerAdShowNum == null)
- this.bannerAdShowNum = 0;
- this.bannerAdShowNum ++;
- if(this.platform == "wx")
- {
- if(this.bannerAdCtrl !=null && this.bannerAdShowNum%5!=0)
- {
- return;
- }
- this.bannerAdShowNum = 0;
- if(this.bannerAdCtrl !=null)
- this.bannerAdCtrl.destroy();
- let WXAD={};
- WXAD.W = wx.getSystemInfoSync().windowWidth;
- WXAD.H = wx.getSystemInfoSync().windowHeight;
- let adWidth = 0;
- if(this.allScreen)
- adWidth = WXAD.W;
- else
- adWidth = 300;
- this.bannerAdCtrl = wx.createBannerAd({
- adUnitId: this.getBannerAdId(),
- style: {
- left: 0,
- top: 0/*/(WXAD.H/10)*8*/,
- width: adWidth,
- }
- });
- this.bannerAdCtrl.onResize(res=>{
-
- console.log("重置广告宽度 " + WXAD.H + " , " + self.bannerAdCtrl.style.realHeight);
- self.bannerAdCtrl.style.top = WXAD.H-self.bannerAdCtrl.style.realHeight;
- self.bannerAdCtrl.style.left = (WXAD.W-self.bannerAdCtrl.style.realWidth)/2;
- self.bannerAdCtrl.show();
-
- });
- this.bannerAdCtrl.onError(err => { console.log(err) });
- console.log("显示广告");
- }
- else if(this.platform == "baidu")
- {
- if(swan.getSystemInfoSync().platform == 'ios') //目前 ios 不支持视频广告
- {
- return;
- }
- if(this.bannerAdCtrl !=null)
- this.bannerAdCtrl.destroy();
- let WXAD={};
- WXAD.W = swan.getSystemInfoSync().windowWidth;
- WXAD.H = swan.getSystemInfoSync().windowHeight;
- console.log("百度的 bannerId = " + this.getBannerAdId());
- this.bannerAdCtrl = swan.createBannerAd({
- adUnitId: this.getBannerAdId(),
- appSid:'c0fb407f',
- style: {
- left: 0,
- top: 0,
- width: 321,
- }
- });
- if(this.allScreen)
- self.bannerAdCtrl.style.width = WXAD.H;
- else
- self.bannerAdCtrl.style.width = 300;
-
- this.bannerAdCtrl.onResize(res=>{
- console.log("重置广告宽度 ");
- self.bannerAdCtrl.style.top = WXAD.H-res.height;
- self.bannerAdCtrl.style.left = (WXAD.W-res.width)/2;
- self.bannerAdCtrl.show();
-
- });
- this.bannerAdCtrl.onLoad(() => {
- console.log(' banner 加载完成');
- self.bannerAdCtrl.show();
- });
-
- this.bannerAdCtrl.onError(err => { console.log(err) });
- console.log("显示广告");
- }
- },
- HideBannerAd:function(){
- if(this.bannerAdCtrl != null)
- this.bannerAdCtrl.hide();
- },
- RecoverShowBanner:function(){
- if(this.bannerAdCtrl != null)
- this.bannerAdCtrl.show();
- else
- this.ShowBannerAds();
- },
- DestroyBanner:function(){
- if(this.bannerAdCtrl !=null)
- this.bannerAdCtrl.destroy();
- },
- //cb 回调参数 0 播放完成,1 不放未完成 ,-1 视频加载出错
- ShowVideoAds:function(adName,cb)
- {
- this.cb = cb
- if(this.platform == "wx")
- {
- let videoAd = wx.createRewardedVideoAd({
- adUnitId: this.getVideoAdId(adName)
- })
-
- videoAd.load()
- .then(() => videoAd.show())
- .catch(err => console.log(err.errMsg))
- videoAd.onError(err => {
- console.log("广告加载出错 " + err.errMsg)
- this.cb(-1);
- });
- videoAd.onLoad(()=>{ console.log("加载事件回调") });
- videoAd.onClose(res =>{
- console.log("是否观看完整了 " + res.isEnded)
- this.cb(res.isEnded?0:1);
- videoAd.offLoad();
- videoAd.offError();
- videoAd.offClose();
- });
- }
- else if(this.platform == "baidu")
- {
- console.log("百度的视频广告 " + this.getVideoAdId(adName));
- let videoAd = swan.createRewardedVideoAd({
- adUnitId: this.getVideoAdId(adName),
- appSid: 'c0fb407f',
- })
-
- videoAd.load()
- .then(() => videoAd.show())
- .catch(err => console.log(err.errMsg))
- videoAd.onError(err => {
- console.log("广告加载出错 " + err.errMsg)
- this.cb(-1);
- });
- videoAd.onLoad(()=>{ console.log("加载事件回调") });
- videoAd.onClose(res =>{
- console.log("是否观看完整了 " + res.isEnded)
- this.cb(res.isEnded?0:1);
- videoAd.offLoad();
- videoAd.offError();
- videoAd.offClose();
- });
- }
- else
- {
- this.cb(0);
- }
- },
- },
- });
- module.exports = AdsMgr;
|