sdkUtil.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. import { _decorator, error, SpriteFrame, sys } from "cc";
  2. import { config } from "./config";
  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. public static isLookAd:boolean = false; //是否在看广告
  20. public static tt_systemInfo: any = null; //抖音_系统信息
  21. private static tt_isSupportSidebar:boolean = false; //抖音_是否支持侧边栏
  22. private static tt_isToEnterFromSidebar:boolean = false; //抖音_是否从侧边栏进入
  23. private static tt_gameRecorder:any = null; //抖音游戏录制
  24. private static tt_recordVideoPath:string = ''; //抖音录制视频路径
  25. private static tt_totalRecord:number = 300; //抖音总录制时间
  26. private static tt_isRecording:boolean = false; //抖音是否录制中
  27. //------------------------------ 公共 ------------------------------//
  28. public static init() {
  29. // this.ttGetSystemInfo(()=> {
  30. // this.ttRegisterInfo()
  31. // })
  32. }
  33. // 苹果手机是否有灵动岛
  34. public static iPhoneIsLingdongdao():boolean {
  35. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  36. if(SdkUtil.tt_systemInfo !=null) {
  37. if(SdkUtil.tt_systemInfo.brand=='Apple'||SdkUtil.tt_systemInfo.brand=='devtools')
  38. if(SdkUtil.tt_systemInfo.model=='iPhone 14'||
  39. SdkUtil.tt_systemInfo.model=='iPhone 14 Pro'||
  40. SdkUtil.tt_systemInfo.model=='iPhone 14 Pro Max'||
  41. SdkUtil.tt_systemInfo.model=='iPhone 15'||
  42. SdkUtil.tt_systemInfo.model=='iPhone 15 Pro'||
  43. SdkUtil.tt_systemInfo.model=='iPhone 15 Pro Max') {
  44. return true
  45. }
  46. }
  47. }
  48. return false
  49. }
  50. // 自定义事件统计
  51. public static customEventStatistics(eventType: string, objParams?: any) {
  52. eventType = eventType.toString();
  53. if (!objParams) { objParams = {}; }
  54. // console.log({'eventType': eventType},{'objParams': objParams});
  55. if (this.platform === 'wx') {
  56. //@ts-ignore
  57. if (window['wx'] && window['wx']['aldSendEvent']) {
  58. //@ts-ignore
  59. window.wx['aldSendEvent'](eventType, objParams);
  60. }
  61. }
  62. //@ts-ignore
  63. if (this.platform === 'cocos' && window.cocosAnalytics && window.cocosAnalytics.isInited()) {
  64. console.log("###统计", eventType, objParams);
  65. //@ts-ignore
  66. window.cocosAnalytics.CACustomEvent.onStarted(eventType, objParams);
  67. }
  68. }
  69. // 检测显示添加桌面
  70. public static checkIsShowAddDesktop():boolean {
  71. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  72. if(SdkUtil.tt_systemInfo.appName=='Douyin' || SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  73. return true
  74. }
  75. }
  76. return false
  77. }
  78. // 分享游戏
  79. public static shareGame(title: string, imageUrl: string, videoPath: string) {
  80. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  81. if(videoPath!="") {
  82. // this.ttShareScreenRecordVideo(config.gameName,videoPath)
  83. }
  84. } else if(sys.platform == sys.Platform.WECHAT_GAME) {
  85. title = title + '好刺激、好好玩'
  86. // this.wxShare(title, imageUrl)
  87. }
  88. }
  89. // 获取广告id
  90. public static getAdId(ad_type = config.AD_TYPE.UNKNOWN):string {
  91. let ad_id = ""
  92. // if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  93. // if(ad_type == config.AD_TYPE.RE_LIFE) {
  94. // ad_id = config.TT_REWARD.RE_LIFE
  95. // } else if (ad_type == config.AD_TYPE.ANSWER) {
  96. // ad_id = config.TT_REWARD.ANSWER
  97. // } else if (ad_type == config.AD_TYPE.LOOK_TIPS) {
  98. // ad_id = config.TT_REWARD.LOOK_TIPS
  99. // } else if (ad_type == config.AD_TYPE.UN_LOCK_24) {
  100. // ad_id = config.TT_REWARD.UN_LOCK_24
  101. // } else if (ad_type == config.AD_TYPE.UN_LOCK) {
  102. // ad_id = config.TT_REWARD.UN_LOCK
  103. // } else if (ad_type == config.AD_TYPE.ADD_TIME) {
  104. // ad_id = config.TT_REWARD.ADD_TIME
  105. // }
  106. // } else if (sys.platform == sys.Platform.WECHAT_GAME) {
  107. // if(ad_type == config.AD_TYPE.RE_LIFE) {
  108. // ad_id = config.WX_REWARD.RE_LIFE
  109. // } else if (ad_type == config.AD_TYPE.ANSWER) {
  110. // ad_id = config.WX_REWARD.ANSWER
  111. // } else if (ad_type == config.AD_TYPE.LOOK_TIPS) {
  112. // ad_id = config.WX_REWARD.LOOK_TIPS
  113. // } else if (ad_type == config.AD_TYPE.UN_LOCK_24) {
  114. // ad_id = config.WX_REWARD.UN_LOCK_24
  115. // } else if (ad_type == config.AD_TYPE.UN_LOCK) {
  116. // ad_id = config.WX_REWARD.UN_LOCK
  117. // } else if (ad_type == config.AD_TYPE.ADD_TIME) {
  118. // ad_id = config.WX_REWARD.ADD_TIME
  119. // }
  120. // }
  121. return ad_id
  122. }
  123. // 显示激励视频广告
  124. public static showVideoAd(_adUnitId: string, call_back) {
  125. // if(gameManager.isFreeAds()) {
  126. // call_back({"isEnded":true})
  127. // return
  128. // }
  129. // if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME||sys.platform==sys.Platform.WECHAT_GAME){
  130. // gameManager.Singleton.showLoadingLevel()
  131. // if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  132. // SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  133. // } else if(sys.platform==sys.Platform.WECHAT_GAME) {
  134. // gameManager.Singleton.hideLoadingLevel()
  135. // call_back({"isEnded":true})
  136. // return
  137. // // SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
  138. // }
  139. // if(SdkUtil.videoAd==null){
  140. // gameManager.Singleton.hideLoadingLevel()
  141. // return
  142. // }
  143. // SdkUtil.videoAd.onLoad(() => {
  144. // SdkUtil.isLookAd = true
  145. // SdkUtil.videoAd.show();
  146. // console.log("广告加载完成");
  147. // });
  148. // SdkUtil.videoAd.onClose((res) => {
  149. // call_back(res)
  150. // SdkUtil.isLookAd = false
  151. // SdkUtil.videoAd.destroy()
  152. // gameManager.Singleton.hideLoadingLevel()
  153. // });
  154. // SdkUtil.videoAd.onError((res) => {
  155. // let errorString = res.errCode + '-' + res.errMsg
  156. // call_back({isEnded:false,errorString:errorString})
  157. // SdkUtil.isLookAd = false
  158. // SdkUtil.videoAd.destroy()
  159. // gameManager.Singleton.hideLoadingLevel()
  160. // });
  161. // SdkUtil.videoAd.load()
  162. // }
  163. // }
  164. // //------------------------------ 微信相关 ------------------------------//
  165. // // 微信分享
  166. // public static wxShare(title: string, imageUrl: string) {
  167. // // console.log('微信分享=',title, 'imageUrl=',imageUrl)
  168. // wx.showShareMenu({
  169. // withShareTicket: true,
  170. // // shareAppMessage(可以删除):显示分享给好友选项,shareTimeline(可以删除):显示分享至朋友圈选项
  171. // // 可以只开启前者。如果要开启后者,则两者必须都开启才能生效。
  172. // // menus: ['shareAppMessage', 'shareTimeline'],
  173. // menus: ['shareAppMessage'],
  174. // complete: () => {}
  175. // });
  176. // // 主动分享
  177. // wx.shareAppMessage({
  178. // title: title,
  179. // imageUrl: imageUrl
  180. // });
  181. // }
  182. // //------------------------------ 抖音相关 ------------------------------//
  183. // // 抖音获取系统信息
  184. // public static ttGetSystemInfo(callback) {
  185. // if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  186. // tt.getSystemInfo({
  187. // success:(res) => {
  188. // // console.log('tt.getSystemInfo=',res)
  189. // // appName: "Douyin" appName: "douyin_lite"
  190. // SdkUtil.tt_systemInfo = res
  191. // callback()
  192. // }
  193. // })
  194. // }
  195. }
  196. // 抖音添加快捷键(目前仅支持:抖音(Douyin) 和 抖音极速版(douyin_lite))
  197. public static ttAddShortcut(onSuccess:Function = null, onFail:Function = null) {
  198. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  199. if(SdkUtil.tt_systemInfo.appName=='Douyin'||SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  200. if(SdkUtil.tt_systemInfo.brand=='Apple') {
  201. tt.addShortcut({
  202. success() {
  203. console.log("添加桌面成功");
  204. onSuccess && onSuccess(null)
  205. },
  206. fail(err) {
  207. console.log("添加桌面失败", err.errMsg);
  208. onFail && onFail(err)
  209. },
  210. });
  211. } else if(SdkUtil.tt_systemInfo.brand=='Android') {
  212. // 检测只支持安卓
  213. tt.checkShortcut({
  214. success(res) {
  215. console.log("检查快捷方式", res.status);
  216. if(res.status.exist==false||res.status.needUpdate==true) {
  217. tt.addShortcut({
  218. success() {
  219. console.log("添加桌面成功");
  220. onSuccess && onSuccess(null)
  221. },
  222. fail(err) {
  223. console.log("添加桌面失败", err.errMsg);
  224. onFail && onFail(err)
  225. },
  226. });
  227. }
  228. },
  229. fail(err) {
  230. console.log("检查快捷方式失败", err.errMsg);
  231. onFail && onFail(err)
  232. },
  233. });
  234. }
  235. }
  236. }
  237. }
  238. // 抖音注册信息
  239. public static ttRegisterInfo() {
  240. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  241. tt.onShow((res) => {
  242. console.log('tt.onShow =', res)
  243. // console.log('res=',res)
  244. if(res.scene == '021036' || res.scene == '101036') {
  245. SdkUtil.tt_isToEnterFromSidebar = true
  246. }
  247. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  248. SdkUtil.tt_isToEnterFromSidebar = true
  249. }
  250. });
  251. tt.onHide(()=>{
  252. console.log('tt.onHide')
  253. if(SdkUtil.isLookAd==false) {
  254. // statisticsManager.uploadRecordUserLevel(false)
  255. }
  256. })
  257. tt.checkScene({
  258. scene: "sidebar",
  259. success: (res) => {
  260. console.log("check scene success: ", res.isExist);
  261. if(res.isExist != undefined || res.isExist != null) {
  262. SdkUtil.tt_isSupportSidebar = res.isExist
  263. }
  264. },
  265. fail: (res) => {
  266. console.log("check scene fail:", res);
  267. }
  268. });
  269. let options = tt.getLaunchOptionsSync()
  270. console.log('getLaunchOptionsSync=', options)
  271. if(options.scene == '021036' || options.scene == '101036') {
  272. SdkUtil.tt_isToEnterFromSidebar = true
  273. }
  274. }
  275. }
  276. // 抖音检测是否显示奖励
  277. public static ttCheckSceneShowRewards():boolean {
  278. return SdkUtil.tt_isSupportSidebar
  279. }
  280. // 抖音检测是否从侧边栏进入
  281. public static ttCheckToEnterFromSidebar():boolean {
  282. return SdkUtil.tt_isToEnterFromSidebar
  283. }
  284. // 抖音导航到侧边栏场景
  285. public static ttNavToSidebarScene() {
  286. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  287. tt.navigateToScene({
  288. scene: "sidebar",
  289. success: (res) => {
  290. // console.log("navigate to scene success");
  291. },
  292. fail: (res) => {
  293. // console.log("navigate to scene fail: ", res);
  294. },
  295. });
  296. }
  297. }
  298. // 抖音开启屏幕录制
  299. public static ttStartScreenRecording() {
  300. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  301. return
  302. }
  303. if(this.tt_systemInfo.platform == 'devtools') {
  304. console.log('抖音模拟器')
  305. return
  306. }
  307. if(this.tt_isRecording==true) {
  308. this.ttStopScreenRecording()
  309. }
  310. if(!this.tt_gameRecorder) {
  311. this.tt_gameRecorder = tt.getGameRecorderManager()
  312. }
  313. this.tt_gameRecorder.start({duration: this.tt_totalRecord})
  314. this.tt_gameRecorder.onStart(()=> {
  315. this.tt_isRecording = true
  316. // console.log('GameRecorder onStart onStart onStart')
  317. })
  318. this.tt_gameRecorder.onStop((res)=> {
  319. // console.log('GameRecorder onStop onStop onStop=',res)
  320. this.tt_isRecording = false
  321. this.tt_recordVideoPath = res.videoPath
  322. })
  323. this.tt_gameRecorder.onError((e)=> {
  324. console.log('ttGameRecord error:',e)
  325. })
  326. }
  327. // 抖音关闭屏幕录制
  328. public static ttStopScreenRecording(isClearVideoPath:boolean = false) {
  329. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  330. return
  331. }
  332. if(this.tt_gameRecorder == null) {
  333. return
  334. }
  335. if(isClearVideoPath) {
  336. this.tt_recordVideoPath = "";
  337. }
  338. this.tt_gameRecorder.stop()
  339. }
  340. // 抖音获取屏幕录制视频文件
  341. public static ttGetScreenRecordingVideoPath():string {
  342. return this.tt_recordVideoPath;
  343. }
  344. // 抖音分享屏幕录制视频
  345. private static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
  346. // tt.shareAppMessage({
  347. // title: title,
  348. // templateId: config.TT_SHARE_TEMPLATEID,
  349. // channel: "video",
  350. // extra: {
  351. // videoTopics: [config.gameName],
  352. // hashtag_list: ['小游戏','小程序'],
  353. // videoPath: videoPath,
  354. // withVideoId: true,
  355. // },
  356. // success: (res) => {
  357. // console.log('抖音分享屏幕录制视频,成功=',res)
  358. // onSuccess && onSuccess();
  359. // },
  360. // fail: (e) => {
  361. // // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
  362. // console.log('抖音分享屏幕录制视频,失败=',e)
  363. // onFail && onFail()
  364. // }
  365. // })
  366. }
  367. public static getUserInfo(call){
  368. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  369. tt.getUserInfo({
  370. // withCredentials: true,
  371. // withRealNameAuthenticationInfo: true,
  372. success(res) {
  373. console.log(`getUserInfo 调用成功`, res.userInfo);
  374. call(res.userInfo)
  375. },
  376. fail(res) {
  377. console.log(`getUserInfo 调用失败`, res.errMsg);
  378. },
  379. });
  380. }else{
  381. call(null)
  382. }
  383. }
  384. public static login(call){
  385. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  386. tt.login({
  387. force: true,
  388. success(res) {
  389. console.log(`login 调用成功${res.code} ${res.anonymousCode}`);
  390. call({"code":res.code,"anonymousCode":res.anonymousCode})
  391. },
  392. fail(res) {
  393. console.log(`login 调用失败`);
  394. call(null)
  395. },
  396. });
  397. }else{
  398. call(null)
  399. }
  400. }
  401. public static vibrateShort(){
  402. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  403. tt.vibrateShort({
  404. success(res) {
  405. // console.log(`${res}`);
  406. },
  407. fail(res) {
  408. // console.log(`vibrateShort调用失败`);
  409. },
  410. });
  411. }
  412. }
  413. }