sdkUtil.ts 4.8 KB

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