WeChatPlatform.ts 6.6 KB

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