sdkManager.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { EType } from "../../tyq3.x/Platform/wechat/WXCustomAd";
  2. import SdkMgr from "../../tyq3.x/SdkMgr";
  3. import TyqEventMgr from "../../tyq3.x/tyq-event-mgr";
  4. import { tyqSDK } from "../../tyq3.x/tyq-sdk";
  5. import { cocosUtil } from "../../utils/cocosUtil";
  6. import { localText } from "../data/localText";
  7. import { msgac } from "../data/msgac";
  8. import { eventManager } from "./eventManager";
  9. export class sdkManager {
  10. private static _instance: sdkManager;
  11. private constructor() { }
  12. public static get instance(): sdkManager {
  13. if (!this._instance) {
  14. this._instance = new sdkManager();
  15. }
  16. return this._instance;
  17. }
  18. private levelCount: number = 0;
  19. /**
  20. * 激励视频广告统一播放接口
  21. * 完整观看广告后,获得奖励,调用:tmpFunc(1)
  22. * 中途退出或者观看失败,调用:tmpFunc(0)
  23. * @param cb
  24. */
  25. openAd(cb: Function) {
  26. let tmpFunc = (st: number) => {
  27. if (st == 0) {
  28. eventManager.instance.send(msgac.showNotice, localText.textObj.adAwardHint);
  29. } else if (st == -1) {
  30. eventManager.instance.send(msgac.showNotice, localText.textObj.noRewardAd);
  31. }
  32. cb(st);
  33. };
  34. if (cocosUtil.isDesktopBrowser()) {
  35. // pc web默认直接成功
  36. tmpFunc(1);
  37. return;
  38. }
  39. this.showRewardAd(tmpFunc);
  40. }
  41. init(cb?: Function) {
  42. tyqSDK.init(() => {
  43. tyqSDK.login();
  44. SdkMgr.ins.preLoadAd();
  45. if (SdkMgr.ins.isWechat()) {
  46. wx.showShareMenu();
  47. }
  48. if (cb) {
  49. cb();
  50. }
  51. });
  52. }
  53. addLevelCount() {
  54. this.levelCount++;
  55. let val = tyqSDK.getSwitchValue("tyq_video_interval");
  56. if (val) {
  57. val = parseInt(val);
  58. if (this.levelCount >= val) {
  59. this.levelCount = 0;
  60. this.showRewardAd();
  61. }
  62. }
  63. }
  64. // 展示激励视频广告
  65. showRewardAd(cb?: Function) {
  66. SdkMgr.ins.showRewardAd("", () => {
  67. // 完全观看结束,可领取奖励
  68. if (cb) {
  69. cb(1);
  70. }
  71. }, () => {
  72. // 用户点击关闭
  73. if (cb) {
  74. cb(0);
  75. }
  76. }, () => {
  77. // 广告加载失败
  78. if (cb) {
  79. cb(-1);
  80. }
  81. });
  82. }
  83. // 展示底部banner广告
  84. showBannerAd() {
  85. let time = tyqSDK.getSwitchValue("tyq_bannerTurns");
  86. if (time) {
  87. SdkMgr.ins.showBannerTurns(time);
  88. }
  89. }
  90. // 隐藏banner广告
  91. hideBannerAd() {
  92. SdkMgr.ins.hideBanner();
  93. }
  94. // 展示原生格子广告,包括顶部,左边和右边
  95. showCustomAdCommon() {
  96. if (tyqSDK.getSwitchValue("tyq_gametop")) {
  97. SdkMgr.ins.showWxCustomAd("custom_ad_top", EType.horizontal, {
  98. top: 0,
  99. });
  100. }
  101. if (tyqSDK.getSwitchValue("tyq_gameFire")) {
  102. SdkMgr.ins.showWxCustomAd("custom_ad_left", EType.vertical, {
  103. left: 0
  104. });
  105. SdkMgr.ins.showWxCustomAd("custom_ad_right", EType.vertical, {
  106. right: 0
  107. })
  108. }
  109. }
  110. // 隐藏原生格子广告,包括顶部,左边和右边
  111. hideCustomAdCommon() {
  112. SdkMgr.ins.hideWxCustomAd("custom_ad_top");
  113. SdkMgr.ins.hideWxCustomAd("custom_ad_left");
  114. SdkMgr.ins.hideWxCustomAd("custom_ad_right");
  115. }
  116. // 展示原生格子广告,包括左边和右边
  117. showCustomAdLeftRight() {
  118. if (tyqSDK.getSwitchValue("tyq_gameFire")) {
  119. SdkMgr.ins.showWxCustomAd("custom_ad_left", EType.vertical, {
  120. left: 0
  121. });
  122. SdkMgr.ins.showWxCustomAd("custom_ad_right", EType.vertical, {
  123. right: 0
  124. })
  125. }
  126. }
  127. hideCustomAdLeftRight() {
  128. SdkMgr.ins.hideWxCustomAd("custom_ad_left");
  129. SdkMgr.ins.hideWxCustomAd("custom_ad_right");
  130. }
  131. // 展示原生格子广告,矩阵类型
  132. showCustomAdRect() {
  133. SdkMgr.ins.showWxCustomAd("custom_ad_rect", EType.rect, {
  134. centerX: 0,
  135. centerY: 0
  136. });
  137. }
  138. hideCustomAdRect() {
  139. SdkMgr.ins.hideWxCustomAd("custom_ad_rect");
  140. }
  141. // 回到主界面 需要处理的广告逻辑
  142. onStartLayerShow() {
  143. let val = tyqSDK.getSwitchValue("tyq_returnMain");
  144. if (val == 1) {
  145. // 显示插屏广告
  146. SdkMgr.ins.showIntersAd();
  147. } else if (val == 2) {
  148. // 显示矩阵广告
  149. this.showCustomAdRect();
  150. }
  151. }
  152. // 关卡事件埋点
  153. sendEventLevelStart(level: string | number) {
  154. tyqSDK.startGame(level);
  155. }
  156. sendEventLevelEnd(isWin: boolean) {
  157. tyqSDK.endGame(isWin);
  158. }
  159. // 自定义事件埋点
  160. sendEvent(name: string) {
  161. TyqEventMgr.ins.sendEvent(name);
  162. }
  163. }