sdkUtil.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. public static tt_isSupportSidebar:boolean = false; //抖音_是否支持侧边栏
  23. public static tt_isToEnterFromSidebar:boolean = false; //抖音_是否从侧边栏进入
  24. /**
  25. * 自定义事件统计
  26. *
  27. * @param {string} eventType
  28. * @param {object} objParams
  29. */
  30. public static customEventStatistics(eventType: string, objParams?: any) {
  31. eventType = eventType.toString();
  32. if (!objParams) {
  33. objParams = {};
  34. }
  35. // console.log({'eventType': eventType},{'objParams': objParams});
  36. if (this.platform === 'wx') {
  37. //@ts-ignore
  38. if (window['wx'] && window['wx']['aldSendEvent']) {
  39. //@ts-ignore
  40. window.wx['aldSendEvent'](eventType, objParams);
  41. }
  42. }
  43. //@ts-ignore
  44. if (this.platform === 'cocos' && window.cocosAnalytics && window.cocosAnalytics.isInited()) {
  45. console.log("###统计", eventType, objParams);
  46. //@ts-ignore
  47. window.cocosAnalytics.CACustomEvent.onStarted(eventType, objParams);
  48. }
  49. }
  50. /**
  51. * 调用震动
  52. */
  53. public static vibrateShort() {
  54. let isEnableVibrate = StorageManager.instance.getGlobalData("vibration") ?? true;
  55. if (isEnableVibrate) {
  56. let now = Date.now();
  57. if (now - this.vibratePreTime >= this.vibrateInterval) {
  58. if (sys.isNative) {
  59. // jsb.reflection.callStaticMethod("com/cocos/game/AppActivity", "vibrator", "()V");
  60. //@ts-ignore
  61. } else if (window.wx) {
  62. //@ts-ignore
  63. wx.vibrateShort({
  64. success: (result: any) => {
  65. },
  66. fail: () => { },
  67. complete: () => { }
  68. });
  69. }
  70. this.vibratePreTime = now;
  71. }
  72. }
  73. }
  74. /**
  75. * 微信分享
  76. *
  77. * @static
  78. * @param {string} title
  79. * @param {string} imageUrl
  80. * @returns
  81. * @memberof SdkUtil
  82. */
  83. public static shareGame(title: string, imageUrl: string) {
  84. //@ts-ignore
  85. if (!window.wx) {
  86. return;
  87. }
  88. //@ts-ignore
  89. wx.showShareMenu({
  90. withShareTicket: true,
  91. complete: () => {
  92. }
  93. });
  94. //@ts-ignore
  95. if (wx.aldOnShareAppMessage) {
  96. //@ts-ignore
  97. wx.aldOnShareAppMessage(function () {
  98. // 用户点击了“转发”按钮
  99. return {
  100. title: title,
  101. imageUrl: imageUrl,
  102. };
  103. });
  104. } else {
  105. //@ts-ignore
  106. wx.onShareAppMessage(function () {
  107. // 用户点击了“转发”按钮
  108. return {
  109. title: title,
  110. imageUrl: imageUrl,
  111. };
  112. });
  113. }
  114. }
  115. /**
  116. * 抖音激励视频
  117. *
  118. * @static
  119. * @param {string} _adUnitId
  120. * @param {Function} call_back
  121. * @returns
  122. * @memberof SdkUtil
  123. */
  124. public static showVideoAd(_adUnitId: string, call_back) {
  125. if(gameManager.havNoAllAd) {
  126. call_back({isEnded:true})
  127. return
  128. }
  129. if(sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
  130. call_back({isEnded:true})
  131. return
  132. }
  133. gameManager.showWaitView(100,"正在加载...");
  134. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  135. SdkUtil.videoAd.onLoad(() => {
  136. SdkUtil.videoAd.show();
  137. console.log("广告加载完成");
  138. });
  139. SdkUtil.videoAd.onClose((res) => {
  140. call_back(res)
  141. SdkUtil.videoAd.destroy()
  142. gameManager.hideWaitView();
  143. });
  144. SdkUtil.videoAd.onError((res) => {
  145. let errorString = res.errCode + '-' + res.errMsg
  146. call_back({isEnded:false,errorString:errorString})
  147. SdkUtil.videoAd.destroy()
  148. gameManager.hideWaitView();
  149. });
  150. SdkUtil.videoAd.load()
  151. }
  152. /**
  153. * 抖音注册侧边栏
  154. *
  155. * @static
  156. * @memberof SdkUtil
  157. */
  158. public static ttRegisterSidebar() {
  159. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  160. tt.onShow((res) => {
  161. console.log('tt.onShow =', res)
  162. // console.log('res=',res)
  163. if(res.scene == '021036' || res.scene == '101036') {
  164. SdkUtil.tt_isToEnterFromSidebar = true
  165. }
  166. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  167. SdkUtil.tt_isToEnterFromSidebar = true
  168. }
  169. });
  170. tt.checkScene({
  171. scene: "sidebar",
  172. success: (res) => {
  173. console.log("check scene success: ", res.isExist);
  174. if(res.isExist != undefined || res.isExist != null) {
  175. SdkUtil.tt_isSupportSidebar = res.isExist
  176. }
  177. },
  178. fail: (res) => {
  179. console.log("check scene fail:", res);
  180. }
  181. });
  182. let options = tt.getLaunchOptionsSync()
  183. console.log('getLaunchOptionsSync=', options)
  184. if(options.scene == '021036' || options.scene == '101036') {
  185. SdkUtil.tt_isToEnterFromSidebar = true
  186. }
  187. }
  188. }
  189. // 抖音检测是否显示奖励
  190. public static ttCheckSceneShowRewards():boolean {
  191. return SdkUtil.tt_isSupportSidebar
  192. }
  193. // 抖音检测是否从侧边栏进入
  194. public static ttCheckToEnterFromSidebar():boolean {
  195. return SdkUtil.tt_isToEnterFromSidebar
  196. }
  197. // 抖音导航到侧边栏场景
  198. public static ttNavToSidebarScene() {
  199. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  200. tt.navigateToScene({
  201. scene: "sidebar",
  202. success: (res) => {
  203. // console.log("navigate to scene success");
  204. },
  205. fail: (res) => {
  206. // console.log("navigate to scene fail: ", res);
  207. },
  208. });
  209. }
  210. }
  211. }