sdkUtil.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import { _decorator, SpriteFrame, sys } from "cc";
  2. import { gameManager } from "./run/gameManager";
  3. import { config } from "./config";
  4. //管理广告、分享、SDK相关内容的组件
  5. export class SdkUtil {
  6. public static platform: string = 'cocos'; //平台
  7. public static imgAd: SpriteFrame = null!;
  8. public static imgShare: SpriteFrame = null!;
  9. public static isDebugMode: boolean = false;
  10. public static onlineInterval: number = -1;
  11. public static isEnableVibrate: boolean = true;
  12. public static isCheckOffline: boolean = false; //登录后会检查是否展示登录界面,而且只检查一次
  13. public static isWatchVideoAd: boolean = false;//是否正在播放广告
  14. public static isEnableMoving: boolean = false;//是否允许屏幕上下移动
  15. public static isEnableZoom: boolean = false;//是否允许屏幕缩放
  16. public static arrLockDiary = [];//未解锁日记
  17. public static vibrateInterval: number = 100;//两次震动之间的间隔,AppActivity里面的震动间隔也是100
  18. public static vibratePreTime: number = 0;//上次震动时间
  19. public static videoAd:any =null;
  20. public static tt_isSupportSidebar:boolean = false; //抖音_是否支持侧边栏
  21. public static tt_isToEnterFromSidebar:boolean = false; //抖音_是否从侧边栏进入
  22. private static tt_gameRecorder:any = null; //抖音游戏录制
  23. private static tt_recordVideoPath:string = ''; //抖音录制视频路径
  24. private static tt_totalRecord:number = 300; //抖音总录制时间
  25. private static tt_isRecording:boolean = false; //抖音是否录制中
  26. /**
  27. * 自定义事件统计
  28. *
  29. * @param {string} eventType
  30. * @param {object} objParams
  31. */
  32. public static customEventStatistics(eventType: string, objParams?: any) {
  33. eventType = eventType.toString();
  34. if (!objParams) {
  35. objParams = {};
  36. }
  37. // console.log({'eventType': eventType},{'objParams': objParams});
  38. if (this.platform === 'wx') {
  39. //@ts-ignore
  40. if (window['wx'] && window['wx']['aldSendEvent']) {
  41. //@ts-ignore
  42. window.wx['aldSendEvent'](eventType, objParams);
  43. }
  44. }
  45. //@ts-ignore
  46. if (this.platform === 'cocos' && window.cocosAnalytics && window.cocosAnalytics.isInited()) {
  47. console.log("###统计", eventType, objParams);
  48. //@ts-ignore
  49. window.cocosAnalytics.CACustomEvent.onStarted(eventType, objParams);
  50. }
  51. }
  52. /**
  53. * 抖音/微信分享
  54. *
  55. * @static
  56. * @param {string} title
  57. * @param {string} imageUrl
  58. * @param {string} videoPath
  59. * @returns
  60. * @memberof SdkUtil
  61. */
  62. public static shareGame(title: string, imageUrl: string, videoPath: string) {
  63. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  64. if(videoPath!="") {
  65. this.ttShareScreenRecordVideo(title,videoPath)
  66. }
  67. } else if(sys.platform == sys.Platform.WECHAT_GAME) {
  68. // 微信分享
  69. }
  70. // //@ts-ignore
  71. // if (!window.wx) {
  72. // return;
  73. // }
  74. // //@ts-ignore
  75. // wx.showShareMenu({
  76. // withShareTicket: true,
  77. // complete: () => {
  78. // }
  79. // });
  80. // //@ts-ignore
  81. // if (wx.aldOnShareAppMessage) {
  82. // //@ts-ignore
  83. // wx.aldOnShareAppMessage(function () {
  84. // // 用户点击了“转发”按钮
  85. // return {
  86. // title: title,
  87. // imageUrl: imageUrl,
  88. // };
  89. // });
  90. // } else {
  91. // //@ts-ignore
  92. // wx.onShareAppMessage(function () {
  93. // // 用户点击了“转发”按钮
  94. // return {
  95. // title: title,
  96. // imageUrl: imageUrl,
  97. // };
  98. // });
  99. // }
  100. }
  101. /**
  102. * 抖音/微信激励视频
  103. *
  104. * @static
  105. * @param {string} _adUnitId
  106. * @param {Function} call_back
  107. * @returns
  108. * @memberof SdkUtil
  109. */
  110. public static showVideoAd(_adUnitId: string, call_back) {
  111. if(gameManager.isFreeAds()) {
  112. call_back({"isEnded":true})
  113. return
  114. }
  115. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME||sys.platform==sys.Platform.WECHAT_GAME){
  116. gameManager.Singleton.showLoadingLevel()
  117. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  118. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  119. } else if(sys.platform==sys.Platform.WECHAT_GAME) {
  120. gameManager.Singleton.hideLoadingLevel()
  121. call_back({"isEnded":true})
  122. return
  123. // SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
  124. }
  125. if(SdkUtil.videoAd==null){
  126. return
  127. }
  128. SdkUtil.videoAd.onLoad(() => {
  129. SdkUtil.videoAd.show();
  130. console.log("广告加载完成");
  131. });
  132. SdkUtil.videoAd.onClose((res) => {
  133. call_back(res)
  134. SdkUtil.videoAd.destroy()
  135. gameManager.Singleton.hideLoadingLevel()
  136. });
  137. SdkUtil.videoAd.onError((res) => {
  138. let errorString = res.errCode + '-' + res.errMsg
  139. call_back({isEnded:false,errorString:errorString})
  140. SdkUtil.videoAd.destroy()
  141. gameManager.Singleton.hideLoadingLevel()
  142. });
  143. SdkUtil.videoAd.load()
  144. }
  145. }
  146. /**
  147. * 抖音侧边栏
  148. *
  149. * @static
  150. * @memberof SdkUtil
  151. */
  152. public static ttRegisterSidebar() {
  153. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  154. tt.onShow((res) => {
  155. console.log('tt.onShow =', res)
  156. // console.log('res=',res)
  157. if(res.scene == '021036' || res.scene == '101036') {
  158. SdkUtil.tt_isToEnterFromSidebar = true
  159. }
  160. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  161. SdkUtil.tt_isToEnterFromSidebar = true
  162. }
  163. });
  164. tt.checkScene({
  165. scene: "sidebar",
  166. success: (res) => {
  167. console.log("check scene success: ", res.isExist);
  168. if(res.isExist != undefined || res.isExist != null) {
  169. SdkUtil.tt_isSupportSidebar = res.isExist
  170. }
  171. },
  172. fail: (res) => {
  173. console.log("check scene fail:", res);
  174. }
  175. });
  176. let options = tt.getLaunchOptionsSync()
  177. console.log('getLaunchOptionsSync=', options)
  178. if(options.scene == '021036' || options.scene == '101036') {
  179. SdkUtil.tt_isToEnterFromSidebar = true
  180. }
  181. }
  182. }
  183. // 抖音检测是否显示奖励
  184. public static ttCheckSceneShowRewards():boolean {
  185. return SdkUtil.tt_isSupportSidebar
  186. }
  187. // 抖音检测是否从侧边栏进入
  188. public static ttCheckToEnterFromSidebar():boolean {
  189. return SdkUtil.tt_isToEnterFromSidebar
  190. }
  191. // 抖音导航到侧边栏场景
  192. public static ttNavToSidebarScene() {
  193. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  194. tt.navigateToScene({
  195. scene: "sidebar",
  196. success: (res) => {
  197. // console.log("navigate to scene success");
  198. },
  199. fail: (res) => {
  200. // console.log("navigate to scene fail: ", res);
  201. },
  202. });
  203. }
  204. }
  205. // 抖音开启屏幕录制
  206. public static ttStartScreenRecording() {
  207. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  208. return
  209. }
  210. if(this.ttIsScreenRecording()==true) {
  211. this.ttStopScreenRecording()
  212. }
  213. if(!this.tt_gameRecorder) {
  214. this.tt_gameRecorder = tt.getGameRecorderManager()
  215. }
  216. this.tt_gameRecorder.start({duration: this.tt_totalRecord})
  217. this.tt_gameRecorder.onStart(()=> {
  218. this.tt_isRecording = true
  219. // console.log('GameRecorder onStart onStart onStart')
  220. })
  221. this.tt_gameRecorder.onStop((res)=> {
  222. // console.log('GameRecorder onStop onStop onStop=',res)
  223. this.tt_isRecording = false
  224. this.tt_recordVideoPath = res.videoPath
  225. })
  226. this.tt_gameRecorder.onError((e)=> {
  227. console.log('ttGameRecord error:',e)
  228. })
  229. }
  230. // 抖音关闭屏幕录制
  231. public static ttStopScreenRecording(isClearVideoPath:boolean = false) {
  232. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  233. return
  234. }
  235. if(this.tt_gameRecorder == null) {
  236. return
  237. }
  238. if(isClearVideoPath) {
  239. this.tt_recordVideoPath = "";
  240. }
  241. this.tt_gameRecorder.stop()
  242. }
  243. // 抖音是否屏幕录制
  244. public static ttIsScreenRecording():boolean {
  245. return this.tt_isRecording;
  246. }
  247. // 抖音获取屏幕录制视频文件
  248. public static ttGetScreenRecordingVideoPath():string {
  249. return this.tt_recordVideoPath;
  250. }
  251. // 抖音分享屏幕录制视频
  252. private static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
  253. tt.shareAppMessage({
  254. title: title,
  255. templateId: config.TT_SHARE_TEMPLATEID,
  256. channel: "video",
  257. extra: {
  258. videoTopics: [config.gameName],
  259. hashtag_list: ['小游戏','小程序'],
  260. videoPath: videoPath,
  261. withVideoId: true,
  262. },
  263. success: (res) => {
  264. console.log('抖音分享屏幕录制视频,成功=',res)
  265. onSuccess && onSuccess();
  266. },
  267. fail: (e) => {
  268. // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
  269. console.log('抖音分享屏幕录制视频,失败=',e)
  270. onFail && onFail()
  271. }
  272. })
  273. }
  274. }