ADVideo.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const GameModule = require("../utils/GameModule");
  2. const SkillApi = require('../net/SkillApi');
  3. // const GameNotificationKey = require('../utils/GameEnum').GameNotificationKey;
  4. const {GameNotificationKey, ADVideoLoadState} = require('../utils/GameEnum');
  5. class ADVideo {
  6. // constructor() {
  7. // this.isNeedShowErr = false;
  8. // }
  9. wxCreateADVideo() {
  10. let defualtADdId = 'adunit-9d6b728b8e8a52d3';
  11. let videoAd = wx.createRewardedVideoAd({
  12. adUnitId: defualtADdId
  13. });
  14. /// 默认是加载成功的 啦
  15. Global._adVideoState = ADVideoLoadState.success;
  16. this.videoAd = videoAd;
  17. videoAd.load()
  18. .then(() => {
  19. this.isFirstLoad = false;
  20. Global._adVideoState = ADVideoLoadState.success;
  21. GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.success);
  22. console.log('视频加载成功');
  23. })
  24. .catch(err => {
  25. // if ((err.errMsg && err.errMsg.includes('fetch advertisement failed')) || (err.errCode && err.errCode == 1004)) {
  26. // Global.commonAlert.showCommonErrorAlert('今日份视频已经播放完啦~~');
  27. // } else {
  28. // Global.commonAlert.showCommonErrorAlert('获取视频失败');
  29. // }
  30. this.handelVideoErr(err);
  31. this.isFirstLoad = false;
  32. });
  33. // videoAd.onLoad(() => {
  34. // /// 视频加载成功
  35. // Global._adVideoState = ADVideoLoadState.success;
  36. // GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.success);
  37. // console.log('视频加载成功');
  38. // });
  39. // 处理获取视频错误
  40. videoAd.onError( (res) => {
  41. this.handelVideoErr(res);
  42. });
  43. videoAd.onClose((res) => {
  44. if (res && res.isEnded || res === undefined) {
  45. console.log('视频正常关闭');
  46. GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.closeFinish, this.callBackObjct);
  47. } else {
  48. console.log('视频未正常关闭');
  49. GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.closeFail, this.callBackObjct);
  50. }
  51. if (GameModule.audioMng) {
  52. GameModule.audioMng.playBgm();
  53. }
  54. SkillApi.report(1, (responseData) => {
  55. },(error) => {
  56. });
  57. })
  58. }
  59. /// 需要在close中回调的对象
  60. showVideo(callBackObjct = null) {
  61. this.callBackObjct = callBackObjct;
  62. this.videoAd.load()
  63. .then(() => {
  64. if (GameModule.audioMng) {
  65. GameModule.audioMng.stopBgm();
  66. }
  67. this.videoAd.show();
  68. })
  69. .catch(err => {
  70. // if ((err.errMsg && err.errMsg.includes('fetch advertisement failed')) || (err.errCode && err.errCode == 1004)) {
  71. // Global.commonAlert.showCommonErrorAlert('今日份视频已经播放完啦~~');
  72. // } else {
  73. // Global.commonAlert.showCommonErrorAlert('获取视频失败');
  74. // }
  75. if (err.errMsg && err.errMsg.includes('video-ad is showed')) {
  76. Global.commonAlert.showCommonErrorAlert('视频正在播放请不要重复点击~~');
  77. return;
  78. } else {
  79. this.handelVideoErr(err);
  80. }
  81. });
  82. }
  83. handelVideoErr(res) {
  84. // if (isShow) {
  85. // Global.commonAlert.showCommonErrorAlert('获取视频失败');
  86. // }
  87. Global._adVideoState = ADVideoLoadState.fail;
  88. console.log('获取视频失败 fa' + res.errMsg);
  89. GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.fail);
  90. }
  91. // static wxPlayADVideo(adId,error,close) {
  92. // let videoAd = wx.createRewardedVideoAd({
  93. // adUnitId: adId
  94. // })
  95. // //处理获取视频错误
  96. // videoAd.onError( (res) => {
  97. // if (res.errMsg) {
  98. // if (res.errMsg.includes('fetch advertisement failed') || res.errCode == 1004) {
  99. // Global.commonAlert.showCommonErrorAlert('今日份视频已经播放完啦~~');
  100. // } else {
  101. // Global.commonAlert.showCommonErrorAlert('获取视频失败');
  102. // }
  103. // } else {
  104. // Global.commonAlert.showCommonErrorAlert('获取视频失败');
  105. // }
  106. // error && error();
  107. // });
  108. // //判断是否完整播放视频
  109. // videoAd.onClose((res) => {
  110. // if (res && res.isEnded || res === undefined) {
  111. // close && close(true);
  112. // } else {
  113. // close && close(false);
  114. // }
  115. // if (GameModule.audioMng) {
  116. // GameModule.audioMng.playBgm();
  117. // }
  118. // SkillApi.report(1, (responseData) => {
  119. // },(error) => {
  120. // });
  121. // })
  122. // }
  123. // static buildingAdId = 'adunit-4ac219410677b685'
  124. // static roomAdId = 'adunit-70b8fbdab9e3e7a8'
  125. // static offlineAdId = 'adunit-a104eef5cc68d0d3'
  126. // /// 抽奖广告id
  127. // static drawAdId = 'adunit-d067ec556ad5c9c2'
  128. // ///商城广告id
  129. // static storeAdCoinId = 'adunit-3494836fc16b0e76'
  130. // static storeAdDiamand = 'adunit-9d6b728b8e8a52d3'
  131. // static resetAdId = 'adunit-72f6285169164939'
  132. // static starAdId = 'adunit-0e09f5a550f077ab'
  133. }
  134. module.exports = ADVideo;
  135. // module.exports = ADVideoLoadState;