sdkUtil.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { _decorator, log, SpriteFrame, sys } from "cc";
  2. import { PlayerData } from "./playerData";
  3. import { StorageManager } from './storageManager';
  4. import { gameManager } from "../gameManager";
  5. import { config } from "../config";
  6. //管理广告、分享、SDK相关内容的组件
  7. export class SdkUtil {
  8. public static platform: string = 'cocos'; //平台
  9. public static imgAd: SpriteFrame = null!;
  10. public static imgShare: SpriteFrame = null!;
  11. public static isDebugMode: boolean = false;
  12. public static onlineInterval: number = -1;
  13. public static isEnableVibrate: boolean = true;
  14. public static isCheckOffline: boolean = false; //登录后会检查是否展示登录界面,而且只检查一次
  15. public static isWatchVideoAd: boolean = false;//是否正在播放广告
  16. public static isEnableMoving: boolean = false;//是否允许屏幕上下移动
  17. public static isEnableZoom: boolean = false;//是否允许屏幕缩放
  18. public static arrLockDiary = [];//未解锁日记
  19. public static vibrateInterval: number = 100;//两次震动之间的间隔,AppActivity里面的震动间隔也是100
  20. public static vibratePreTime: number = 0;//上次震动时间
  21. public static videoAd:any =null;
  22. /**
  23. * 自定义事件统计
  24. *
  25. * @param {string} eventType
  26. * @param {object} objParams
  27. */
  28. public static customEventStatistics(eventType: string, objParams?: any) {
  29. eventType = eventType.toString();
  30. if (!objParams) {
  31. objParams = {};
  32. }
  33. // console.log({'eventType': eventType},{'objParams': objParams});
  34. if (this.platform === 'wx') {
  35. //@ts-ignore
  36. if (window['wx'] && window['wx']['aldSendEvent']) {
  37. //@ts-ignore
  38. window.wx['aldSendEvent'](eventType, objParams);
  39. }
  40. }
  41. //@ts-ignore
  42. if (this.platform === 'cocos' && window.cocosAnalytics && window.cocosAnalytics.isInited()) {
  43. console.log("###统计", eventType, objParams);
  44. //@ts-ignore
  45. window.cocosAnalytics.CACustomEvent.onStarted(eventType, objParams);
  46. }
  47. }
  48. /**
  49. * 调用震动
  50. */
  51. public static vibrateShort() {
  52. let isEnableVibrate = StorageManager.instance.getGlobalData("vibration") ?? true;
  53. if (isEnableVibrate) {
  54. let now = Date.now();
  55. if (now - this.vibratePreTime >= this.vibrateInterval) {
  56. if (sys.isNative) {
  57. // jsb.reflection.callStaticMethod("com/cocos/game/AppActivity", "vibrator", "()V");
  58. //@ts-ignore
  59. } else if (window.wx) {
  60. //@ts-ignore
  61. wx.vibrateShort({
  62. success: (result: any) => {
  63. },
  64. fail: () => { },
  65. complete: () => { }
  66. });
  67. }
  68. this.vibratePreTime = now;
  69. }
  70. }
  71. }
  72. /**
  73. * 微信分享
  74. *
  75. * @static
  76. * @param {string} title
  77. * @param {string} imageUrl
  78. * @returns
  79. * @memberof SdkUtil
  80. */
  81. public static shareGame(title: string, imageUrl: string) {
  82. //@ts-ignore
  83. if (!window.wx) {
  84. return;
  85. }
  86. //@ts-ignore
  87. wx.showShareMenu({
  88. withShareTicket: true,
  89. complete: () => {
  90. }
  91. });
  92. //@ts-ignore
  93. if (wx.aldOnShareAppMessage) {
  94. //@ts-ignore
  95. wx.aldOnShareAppMessage(function () {
  96. // 用户点击了“转发”按钮
  97. return {
  98. title: title,
  99. imageUrl: imageUrl,
  100. };
  101. });
  102. } else {
  103. //@ts-ignore
  104. wx.onShareAppMessage(function () {
  105. // 用户点击了“转发”按钮
  106. return {
  107. title: title,
  108. imageUrl: imageUrl,
  109. };
  110. });
  111. }
  112. }
  113. /**
  114. * 抖音激励视频
  115. *
  116. * @static
  117. * @param {string} _adUnitId
  118. * @param {Function} call_back
  119. * @returns
  120. * @memberof SdkUtil
  121. */
  122. public static showVideoAd(_adUnitId: string, call_back) {
  123. if(gameManager.havNoAllAd) {
  124. call_back({isEnded:true})
  125. return
  126. }
  127. gameManager.showWaitView(100,"正在加载...");
  128. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  129. SdkUtil.videoAd.onLoad(() => {
  130. SdkUtil.videoAd.show();
  131. console.log("广告加载完成");
  132. });
  133. SdkUtil.videoAd.onClose((res) => {
  134. call_back(res)
  135. SdkUtil.videoAd.destroy()
  136. gameManager.hideWaitView();
  137. });
  138. SdkUtil.videoAd.onError((res) => {
  139. call_back({isEnded:false})
  140. SdkUtil.videoAd.destroy()
  141. gameManager.hideWaitView();
  142. });
  143. SdkUtil.videoAd.load()
  144. }
  145. }