ADVideo.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.onLoad(() => {
  18. /// 视频加载成功
  19. Global._adVideoState = ADVideoLoadState.success;
  20. GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.success);
  21. console.log('视频加载成功');
  22. });
  23. //处理获取视频错误
  24. videoAd.onError( (res) => {
  25. if (this.isNeedShowErr) {
  26. Global.commonAlert.showCommonErrorAlert('获取视频失败');
  27. }
  28. Global._adVideoState = ADVideoLoadState.fail;
  29. this.isNeedShowErr = true;
  30. console.log('获取视频失败 fa' + res.errMsg);
  31. GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.fail);
  32. });
  33. videoAd.onClose((res) => {
  34. if (res && res.isEnded || res === undefined) {
  35. console.log('视频正常关闭');
  36. GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.closeFinish, this.callBackObjct);
  37. } else {
  38. console.log('视频未正常关闭');
  39. GameEvent.fire(GameNotificationKey.AdUpdateStateNotification, ADVideoLoadState.closeFail, this.callBackObjct);
  40. }
  41. if (GameModule.audioMng) {
  42. GameModule.audioMng.playBgm();
  43. }
  44. SkillApi.report(1, (responseData) => {
  45. },(error) => {
  46. });
  47. })
  48. }
  49. /// 需要在close中回调的对象
  50. showVideo(callBackObjct = null) {
  51. if (GameModule.audioMng) {
  52. GameModule.audioMng.stopBgm();
  53. }
  54. console.log('开始播放视频');
  55. this.callBackObjct = callBackObjct;
  56. this.videoAd.show();
  57. }
  58. // static wxPlayADVideo(adId,error,close) {
  59. // let videoAd = wx.createRewardedVideoAd({
  60. // adUnitId: adId
  61. // })
  62. // videoAd.load()
  63. // .then(() => {
  64. // if (GameModule.audioMng) {
  65. // GameModule.audioMng.stopBgm();
  66. // }
  67. // 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. // error && error();
  76. // });
  77. // //处理获取视频错误
  78. // videoAd.onError( (res) => {
  79. // if (res.errMsg) {
  80. // if (res.errMsg.includes('fetch advertisement failed') || res.errCode == 1004) {
  81. // Global.commonAlert.showCommonErrorAlert('今日份视频已经播放完啦~~');
  82. // } else {
  83. // Global.commonAlert.showCommonErrorAlert('获取视频失败');
  84. // }
  85. // } else {
  86. // Global.commonAlert.showCommonErrorAlert('获取视频失败');
  87. // }
  88. // error && error();
  89. // });
  90. // //判断是否完整播放视频
  91. // videoAd.onClose((res) => {
  92. // if (res && res.isEnded || res === undefined) {
  93. // close && close(true);
  94. // } else {
  95. // close && close(false);
  96. // }
  97. // if (GameModule.audioMng) {
  98. // GameModule.audioMng.playBgm();
  99. // }
  100. // SkillApi.report(1, (responseData) => {
  101. // },(error) => {
  102. // });
  103. // })
  104. // }
  105. // static buildingAdId = 'adunit-4ac219410677b685'
  106. // static roomAdId = 'adunit-70b8fbdab9e3e7a8'
  107. // static offlineAdId = 'adunit-a104eef5cc68d0d3'
  108. // /// 抽奖广告id
  109. // static drawAdId = 'adunit-d067ec556ad5c9c2'
  110. // ///商城广告id
  111. // static storeAdCoinId = 'adunit-3494836fc16b0e76'
  112. // static storeAdDiamand = 'adunit-9d6b728b8e8a52d3'
  113. // static resetAdId = 'adunit-72f6285169164939'
  114. // static starAdId = 'adunit-0e09f5a550f077ab'
  115. }
  116. module.exports = ADVideo;
  117. // module.exports = ADVideoLoadState;