sdkUtil.ts 4.1 KB

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