ADVideo.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. const GameModule = require("../utils/GameModule");
  2. const SkillApi = require('../net/SkillApi');
  3. class ADVideo {
  4. //// 有个时候输入的不是对象。。。可能是字符串或者数字 这里做一个兼容
  5. static toNormal(coinCount) {
  6. /// 不是正常的金币对象
  7. let newCoin = coinCount;
  8. if (coinCount.n == undefined) {
  9. newCoin = TapTapTool.goldStrToClass(coinCount + '');
  10. /// 说明是数字
  11. if (newCoin.n == undefined) {
  12. newCoin = {'n': newCoin, 'e': 0};
  13. }
  14. }
  15. return {'e': newCoin.e, 'n': Number(newCoin.n)};
  16. }
  17. static wxPlayADVideo(adId,error,close) {
  18. let videoAd = wx.createRewardedVideoAd({
  19. adUnitId: adId
  20. })
  21. videoAd.load()
  22. .then(() => {
  23. if (GameModule.audioMng) {
  24. GameModule.audioMng.stopBgm();
  25. }
  26. videoAd.show()
  27. })
  28. .catch(err => {
  29. if ((err.errMsg && err.errMsg.includes('fetch advertisement failed')) || (err.errCode && err.errCode == 1004)) {
  30. Global.commonAlert.showCommonErrorAlert('今日份视频已经播放完啦~~');
  31. } else {
  32. GameGlobal.commonAlert.showCommonErrorAlert('获取视频失败');
  33. }
  34. error && error();
  35. });
  36. //处理获取视频错误
  37. videoAd.onError( (res) => {
  38. if (res.errMsg) {
  39. if (res.errMsg.includes('fetch advertisement failed') || res.errCode == 1004) {
  40. Global.commonAlert.showCommonErrorAlert('今日份视频已经播放完啦~~');
  41. } else {
  42. GameGlobal.commonAlert.showCommonErrorAlert('获取视频失败');
  43. }
  44. } else {
  45. GameGlobal.commonAlert.showCommonErrorAlert('获取视频失败');
  46. }
  47. error && error();
  48. });
  49. //判断是否完整播放视频
  50. videoAd.onClose((res) => {
  51. if (res && res.isEnded || res === undefined) {
  52. close && close(true);
  53. SkillApi.report(1, (responseData) => {
  54. },(error) => {
  55. });
  56. } else {
  57. close && close(false);
  58. }
  59. if (GameModule.audioMng) {
  60. GameModule.audioMng.playBgm();
  61. }
  62. })
  63. }
  64. static qqPlayADVideo(error,close) {
  65. var isFinished = false;
  66. var videoAd = BK.Advertisement.createVideoAd();
  67. videoAd.onLoad(() => {
  68. //加载成功
  69. BK.Script.log(1,1,"onLoad")
  70. });
  71. videoAd.onPlayStart(() => {
  72. if (GameModule.audioMng) {
  73. GameModule.audioMng.stopBgm();
  74. }
  75. //开始播放
  76. BK.Script.log(1,1,"onPlayStart")
  77. });
  78. videoAd.onPlayFinish(() => {
  79. //播放结束
  80. BK.Script.log(1,1,"onPlayFinish")
  81. isFinished = true;
  82. });
  83. videoAd.onError((err) => {
  84. //加载失败
  85. BK.Script.log(1,1,"onError code:"+err.code+" msg:"+err.msg);
  86. GameGlobal.commonAlert.showCommonErrorAlert('获取视频失败');
  87. error && error();
  88. });
  89. videoAd.onClose(() => {
  90. if (isFinished) {
  91. //看完完整广告,可下发奖励
  92. close && close(true);
  93. SkillApi.report(1, (responseData) => {
  94. },(error) => {
  95. });
  96. } else {
  97. // 未看完广告,不下发游戏奖励
  98. close && close(false);
  99. }
  100. if (GameModule.audioMng) {
  101. GameModule.audioMng.playBgm();
  102. }
  103. });
  104. videoAd.show();
  105. }
  106. }
  107. module.exports = ADVideo;