AdsMgr.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. var AdsParam = require("AdsParam");
  2. var AdsMgr = cc.Class({
  3. statics:{
  4. //微信 wx
  5. //头条 tt
  6. //百度 baidu
  7. platform:"wx",
  8. //对应的平台的 bannerId 采用加后缀的方式
  9. bannerId_baidu:"6191742",
  10. bannerId_wx:"adunit-42bcaae6cafaa7c6",
  11. bannerId_tt:"6152042",
  12. //百度广告id 激励视频
  13. bdVideoId_1:"6191756",
  14. bdVideoId_2:"6191755",
  15. bdVideoId_3:"6191754",
  16. bdVideoId_4:"6191753",
  17. bdVideoId_5:"6191752",
  18. bdVideoId_6:"6191751",
  19. bdVideoId_7:"6191749",
  20. bdVideoId_8:"6191748",
  21. bdVideoId_9:"6191747",
  22. bdVideoId_10:"6191744",
  23. //微信广告id 激励视频
  24. wxVideoId_1:"",
  25. wxVideoId_2:"",
  26. wxVideoId_3:"",
  27. wxVideoId_4:"",
  28. wxVideoId_5:"",
  29. wxVideoId_6:"",
  30. wxVideoId_7:"",
  31. wxVideoId_8:"",
  32. wxVideoId_9:"",
  33. wxVideoId_10:"",
  34. allScreen:false,
  35. //首先确定使用的平台
  36. Init:function () {
  37. if(!window.wx && !window.tt && !window.swan)
  38. this.platform="pc";
  39. this.allScreen = false;
  40. },
  41. //获取广告点ID
  42. getBannerAdId:function()
  43. {
  44. if(this.platform == "wx")
  45. {
  46. return this.bannerId_wx;
  47. }
  48. else if(this.platform == "baidu")
  49. {
  50. return this.bannerId_baidu;
  51. }
  52. else if(this.platform == "tt")
  53. {
  54. return this.bannerId_tt;
  55. }
  56. return null;
  57. },
  58. //获取广告点ID 这里配置 视频奖励广告 Id 由于会有多个广告点 根据后台设置来顶
  59. getVideoAdId:function(adName)
  60. {
  61. var adId;
  62. if(this.platform == "wx")
  63. {
  64. switch(adName)
  65. {
  66. case AdsParam.PointA:
  67. adId = this.wxVideoId_1;
  68. break;
  69. case AdsParam.PointB:
  70. adId = this.wxVideoId_2;
  71. break;
  72. case AdsParam.PointC:
  73. adId = this.wxVideoId_3;
  74. break;
  75. case AdsParam.PointD:
  76. adId = this.wxVideoId_4;
  77. break;
  78. case AdsParam.PointE:
  79. adId = this.wxVideoId_5;
  80. break;
  81. case AdsParam.PointF:
  82. adId = this.wxVideoId_6;
  83. break;
  84. case AdsParam.PointG:
  85. adId = this.wxVideoId_7;
  86. break;
  87. case AdsParam.PointH:
  88. adId = this.wxVideoId_8;
  89. break;
  90. case AdsParam.PointI:
  91. adId = this.wxVideoId_9;
  92. break;
  93. case AdsParam.PointJ:
  94. adId = this.wxVideoId_10;
  95. break;
  96. }
  97. }
  98. else if(this.platform == "baidu")
  99. {
  100. switch(adName)
  101. {
  102. case AdsParam.PointA:
  103. adId = this.bdVideoId_1;
  104. break;
  105. case AdsParam.PointB:
  106. adId = this.bdVideoId_2;
  107. break;
  108. case AdsParam.PointC:
  109. adId = this.bdVideoId_3;
  110. break;
  111. case AdsParam.PointD:
  112. adId = this.bdVideoId_4;
  113. break;
  114. case AdsParam.PointE:
  115. adId = this.bdVideoId_5;
  116. break;
  117. case AdsParam.PointF:
  118. adId = this.bdVideoId_6;
  119. break;
  120. case AdsParam.PointG:
  121. adId = this.bdVideoId_7;
  122. break;
  123. case AdsParam.PointH:
  124. adId = this.bdVideoId_8;
  125. break;
  126. case AdsParam.PointI:
  127. adId = this.bdVideoId_9;
  128. break;
  129. case AdsParam.PointJ:
  130. adId = this.bdVideoId_10;
  131. break;
  132. }
  133. }
  134. return adId;
  135. },
  136. ShowBannerAds:function()
  137. {
  138. var self = this;
  139. if(this.bannerAdShowNum == null)
  140. this.bannerAdShowNum = 0;
  141. this.bannerAdShowNum ++;
  142. if(this.platform == "wx")
  143. {
  144. if(this.bannerAdCtrl !=null && this.bannerAdShowNum%5!=0)
  145. {
  146. return;
  147. }
  148. this.bannerAdShowNum = 0;
  149. if(this.bannerAdCtrl !=null)
  150. this.bannerAdCtrl.destroy();
  151. let WXAD={};
  152. WXAD.W = wx.getSystemInfoSync().windowWidth;
  153. WXAD.H = wx.getSystemInfoSync().windowHeight;
  154. let adWidth = 0;
  155. if(this.allScreen)
  156. adWidth = WXAD.W;
  157. else
  158. adWidth = 300;
  159. this.bannerAdCtrl = wx.createBannerAd({
  160. adUnitId: this.getBannerAdId(),
  161. style: {
  162. left: 0,
  163. top: 0/*/(WXAD.H/10)*8*/,
  164. width: adWidth,
  165. }
  166. });
  167. this.bannerAdCtrl.onResize(res=>{
  168. console.log("重置广告宽度 " + WXAD.H + " , " + self.bannerAdCtrl.style.realHeight);
  169. self.bannerAdCtrl.style.top = WXAD.H-self.bannerAdCtrl.style.realHeight;
  170. self.bannerAdCtrl.style.left = (WXAD.W-self.bannerAdCtrl.style.realWidth)/2;
  171. self.bannerAdCtrl.show();
  172. });
  173. this.bannerAdCtrl.onError(err => { console.log(err) });
  174. console.log("显示广告");
  175. }
  176. else if(this.platform == "baidu")
  177. {
  178. if(swan.getSystemInfoSync().platform == 'ios') //目前 ios 不支持视频广告
  179. {
  180. return;
  181. }
  182. if(this.bannerAdCtrl !=null)
  183. this.bannerAdCtrl.destroy();
  184. let WXAD={};
  185. WXAD.W = swan.getSystemInfoSync().windowWidth;
  186. WXAD.H = swan.getSystemInfoSync().windowHeight;
  187. console.log("百度的 bannerId = " + this.getBannerAdId());
  188. this.bannerAdCtrl = swan.createBannerAd({
  189. adUnitId: this.getBannerAdId(),
  190. appSid:'c0fb407f',
  191. style: {
  192. left: 0,
  193. top: 0,
  194. width: 321,
  195. }
  196. });
  197. if(this.allScreen)
  198. self.bannerAdCtrl.style.width = WXAD.H;
  199. else
  200. self.bannerAdCtrl.style.width = 300;
  201. this.bannerAdCtrl.onResize(res=>{
  202. console.log("重置广告宽度 ");
  203. self.bannerAdCtrl.style.top = WXAD.H-res.height;
  204. self.bannerAdCtrl.style.left = (WXAD.W-res.width)/2;
  205. self.bannerAdCtrl.show();
  206. });
  207. this.bannerAdCtrl.onLoad(() => {
  208. console.log(' banner 加载完成');
  209. self.bannerAdCtrl.show();
  210. });
  211. this.bannerAdCtrl.onError(err => { console.log(err) });
  212. console.log("显示广告");
  213. }
  214. },
  215. HideBannerAd:function(){
  216. if(this.bannerAdCtrl != null)
  217. this.bannerAdCtrl.hide();
  218. },
  219. RecoverShowBanner:function(){
  220. if(this.bannerAdCtrl != null)
  221. this.bannerAdCtrl.show();
  222. else
  223. this.ShowBannerAds();
  224. },
  225. DestroyBanner:function(){
  226. if(this.bannerAdCtrl !=null)
  227. this.bannerAdCtrl.destroy();
  228. },
  229. //cb 回调参数 0 播放完成,1 不放未完成 ,-1 视频加载出错
  230. ShowVideoAds:function(adName,cb)
  231. {
  232. this.cb = cb
  233. if(this.platform == "wx")
  234. {
  235. let videoAd = wx.createRewardedVideoAd({
  236. adUnitId: this.getVideoAdId(adName)
  237. })
  238. videoAd.load()
  239. .then(() => videoAd.show())
  240. .catch(err => console.log(err.errMsg))
  241. videoAd.onError(err => {
  242. console.log("广告加载出错 " + err.errMsg)
  243. this.cb(-1);
  244. });
  245. videoAd.onLoad(()=>{ console.log("加载事件回调") });
  246. videoAd.onClose(res =>{
  247. console.log("是否观看完整了 " + res.isEnded)
  248. this.cb(res.isEnded?0:1);
  249. videoAd.offLoad();
  250. videoAd.offError();
  251. videoAd.offClose();
  252. });
  253. }
  254. else if(this.platform == "baidu")
  255. {
  256. console.log("百度的视频广告 " + this.getVideoAdId(adName));
  257. let videoAd = swan.createRewardedVideoAd({
  258. adUnitId: this.getVideoAdId(adName),
  259. appSid: 'c0fb407f',
  260. })
  261. videoAd.load()
  262. .then(() => videoAd.show())
  263. .catch(err => console.log(err.errMsg))
  264. videoAd.onError(err => {
  265. console.log("广告加载出错 " + err.errMsg)
  266. this.cb(-1);
  267. });
  268. videoAd.onLoad(()=>{ console.log("加载事件回调") });
  269. videoAd.onClose(res =>{
  270. console.log("是否观看完整了 " + res.isEnded)
  271. this.cb(res.isEnded?0:1);
  272. videoAd.offLoad();
  273. videoAd.offError();
  274. videoAd.offClose();
  275. });
  276. }
  277. else
  278. {
  279. this.cb(0);
  280. }
  281. },
  282. },
  283. });
  284. module.exports = AdsMgr;