WeChatPlatform.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { _decorator, Component, Node } from 'cc';
  2. import { BannerLoop } from '../BannerLoop';
  3. import { BasePlatform } from './BasePlatform';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('WeChatPlatform')
  6. export class WeChatPlatform extends BasePlatform {
  7. private wx = window['wx'];
  8. private __bannerIDs: string[] = ["adunit-91be4efcdebcba8d", "adunit-ff8ca74a9b61c63b"];
  9. private __bannerLoop: BannerLoop;
  10. private __windowWidth: number;
  11. private __windowHeight: number;
  12. //激励广告
  13. private __rewardedVideoAd: any;
  14. private __rewardedSuccess: Function;
  15. private __rewardedfailure: Function;
  16. private __systemInfo: any;
  17. constructor() {
  18. super()
  19. this.__systemInfo = this.wx.getSystemInfoSync();
  20. this.__windowWidth = this.__systemInfo.screenWidth;
  21. this.__windowHeight = this.__systemInfo.screenHeight;
  22. }
  23. login(success: Function, failure: Function): void {
  24. this.__bannerLoop = new BannerLoop();
  25. this.__bannerLoop.init(2, this.__bannerIDs, this.__bannerCreate.bind(this), this.__bannerDestory.bind(this));
  26. //激励广告
  27. this.__initRewardedVideoAd();
  28. }
  29. private __initRewardedVideoAd(): void {
  30. try {
  31. //提前创建广告
  32. this.__rewardedVideoAd = this.wx.createRewardedVideoAd({ adUnitId: 'adunit-06ba7bdaa32c6cc5' })
  33. //拉取成功
  34. this.__rewardedVideoAd.onLoad(() => {
  35. console.log("激励广告拉取成功");
  36. });
  37. //拉取失败
  38. this.__rewardedVideoAd.onError(err => {
  39. console.log("激励广告拉取失败");
  40. });
  41. //激励广告回调
  42. this.__rewardedVideoAd.onClose(res => {
  43. // 用户点击了【关闭广告】按钮
  44. // 小于 2.1.0 的基础库版本,res 是一个 undefined
  45. if (res && res.isEnded || res === undefined) {
  46. console.log("广告看完了");
  47. // 正常播放结束,可以下发游戏奖励
  48. if (this.__rewardedSuccess) {
  49. this.__rewardedSuccess();
  50. this.__rewardedSuccess = null;
  51. }
  52. this.__rewardedfailure = null;
  53. }
  54. else {
  55. console.log("看广告中途退出!");
  56. // 播放中途退出,不下发游戏奖励
  57. if (this.__rewardedfailure) {
  58. this.__rewardedfailure();
  59. this.__rewardedfailure = null;
  60. }
  61. this.__rewardedSuccess = null;
  62. }
  63. });
  64. } catch (error) {
  65. console.error("初始化激励广告失败");
  66. }
  67. }
  68. private __bannerCreate(id: string): any {
  69. let targetBannerAdWidth: number = this.__windowWidth;
  70. let resizeBool: boolean = false;
  71. let banner: any = this.wx.createBannerAd(
  72. {
  73. adUnitId: id,
  74. style: {
  75. width: targetBannerAdWidth,
  76. left: 0,
  77. top: this.__windowHeight - (targetBannerAdWidth / 16 * 9)
  78. }
  79. }
  80. );
  81. // 尺寸调整时会触发回调
  82. // 注意:如果在回调里再次调整尺寸,要确保不要触发死循环!!!
  83. banner.onResize(size => {
  84. if(resizeBool == false){
  85. resizeBool = true;
  86. banner.style.top = this.__windowHeight - size.height;
  87. banner.style.left = (this.__windowWidth - size.width) / 2;
  88. }
  89. });
  90. banner.onError(err => {
  91. console.log("err", err)
  92. })
  93. return banner;
  94. }
  95. private __bannerDestory(banner: any): void {
  96. banner.destroy();
  97. }
  98. navigate2Mini(data: any, success?: Function, failure?: Function): void {
  99. throw new Error('Method not implemented.');
  100. }
  101. shareMessage(data: any, success?: Function, failure?: Function): void {
  102. throw new Error('Method not implemented.');
  103. }
  104. showBanner(data: any): void {
  105. console.log("显示Banner");
  106. this.__bannerLoop.showBanner();
  107. }
  108. hideBanner(): void {
  109. console.log("隐藏Banner");
  110. this.__bannerLoop.hideBanner();
  111. }
  112. showRewardedVideo(success?: Function, failure?: Function): void {
  113. console.log("显示激励广告");
  114. this.__rewardedSuccess = success;
  115. this.__rewardedfailure = failure;
  116. if (this.__rewardedVideoAd) {
  117. this.__rewardedVideoAd.show().catch(err => {
  118. this.__rewardedVideoAd.load().then(() => this.__rewardedVideoAd.show())
  119. });
  120. }
  121. }
  122. showInsertAd(success?: Function, failure?: Function): void {
  123. throw new Error('Method not implemented.');
  124. }
  125. showNativeAd(success?: Function, failure?: Function): void {
  126. throw new Error('Method not implemented.');
  127. }
  128. sendEvent(eventName: string, data?: any): void {
  129. throw new Error('Method not implemented.');
  130. }
  131. setLoadingProgress(progress: number): void {
  132. throw new Error('Method not implemented.');
  133. }
  134. loadingComplete(completeHandler?: Function): void {
  135. throw new Error('Method not implemented.');
  136. }
  137. getRandomPageAd(num: number): any[] {
  138. throw new Error('Method not implemented.');
  139. }
  140. }