sdkUtil.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. import { _decorator, error, Mask, SpriteFrame, sys, utils } from "cc";
  2. import { gameManager } from "./run/gameManager";
  3. import { config } from "./config";
  4. import { statisticsManager } from "./statisticsManager";
  5. import { tools } from "./tools";
  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 isLookAd:boolean = false; //是否在看广告
  23. public static gridGamePanel = null; //互推游戏组件
  24. public static tt_systemInfo: any = null; //抖音_系统信息
  25. public static wx_systemInfo:any = null; //微信_系统信息
  26. public static ks_systemInfo:any = null; //ks_系统信息
  27. private static tt_isSupportSidebar:boolean = false; //抖音_是否支持侧边栏
  28. private static tt_isToEnterFromSidebar:boolean = false; //抖音_是否从侧边栏进入
  29. private static tt_gameRecorder:any = null; //抖音游戏录制
  30. private static tt_recordVideoPath:string = ''; //抖音录制视频路径
  31. private static tt_totalRecord:number = 300; //抖音总录制时间
  32. private static tt_isRecording:boolean = false; //抖音是否录制中
  33. //------------------------------ 公共 ------------------------------//
  34. // 初始化(设置当前运行平台)
  35. public static init() {
  36. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  37. tools.platform = config.Platform.TT
  38. } else if(sys.platform==sys.Platform.WECHAT_GAME) {
  39. let isKSGame = typeof KSGameGlobal != 'undefined'
  40. // console.log('isKSGame=',isKSGame)
  41. if(isKSGame) {
  42. tools.platform = config.Platform.KS
  43. } else {
  44. tools.platform = config.Platform.WX
  45. // 微信初始化设置显示分享弹窗,右上角'···'才可以转发给朋友,复制链接
  46. wx.showShareMenu({
  47. withShareTicket: true,
  48. menus: ['shareAppMessage', 'shareTimeline']
  49. })
  50. }
  51. }
  52. this.getSystemInfo(()=> {
  53. this.ttRegisterInfo()
  54. this.registerGridGamePanel()
  55. })
  56. }
  57. // 获取平台标识
  58. public static getPlatformIdentify():string {
  59. let identify = config.PLATFORM_IDENTIFY.BROWSER
  60. switch (tools.platform) {
  61. case config.Platform.TT:
  62. identify = config.PLATFORM_IDENTIFY.TT
  63. break;
  64. case config.Platform.WX:
  65. identify = config.PLATFORM_IDENTIFY.WX
  66. break;
  67. case config.Platform.KS:
  68. identify = config.PLATFORM_IDENTIFY.KS
  69. break;
  70. default:
  71. break;
  72. }
  73. return identify
  74. }
  75. // 获取系统信息
  76. public static getSystemInfo(callback) {
  77. switch (tools.platform) {
  78. case config.Platform.TT:
  79. tt.getSystemInfo({
  80. success:(res) => {
  81. console.log('tt.getSystemInfo=',res)
  82. // appName: "Douyin" appName: "douyin_lite"
  83. SdkUtil.tt_systemInfo = res
  84. callback && callback()
  85. }
  86. })
  87. break;
  88. case config.Platform.WX:
  89. wx.getSystemInfo({
  90. success:(res) => {
  91. console.log('wx.getSystemInfo=',res)
  92. SdkUtil.wx_systemInfo = res
  93. callback && callback()
  94. }
  95. })
  96. break;
  97. case config.Platform.KS:
  98. ks.getSystemInfo({
  99. success:(res) => {
  100. console.log('ks.getSystemInfo=',res)
  101. SdkUtil.ks_systemInfo = res
  102. callback && callback()
  103. }
  104. })
  105. break;
  106. default:
  107. break;
  108. }
  109. }
  110. // 是否苹果平台
  111. public static is_iphone_platform():boolean {
  112. if(SdkUtil.tt_systemInfo&&SdkUtil.tt_systemInfo.platform=='ios') {
  113. return true
  114. }
  115. if(SdkUtil.wx_systemInfo&&SdkUtil.wx_systemInfo.platform=='ios') {
  116. return true
  117. }
  118. if(SdkUtil.ks_systemInfo&&SdkUtil.ks_systemInfo.platform=='ios') {
  119. return true
  120. }
  121. return false
  122. }
  123. // 是否平台开发工具
  124. public static is_devtools_platform():boolean {
  125. if(SdkUtil.tt_systemInfo&&SdkUtil.tt_systemInfo.platform=='devtools') {
  126. return true
  127. }
  128. if(SdkUtil.wx_systemInfo&&SdkUtil.wx_systemInfo.platform=='devtools') {
  129. return true
  130. }
  131. if(SdkUtil.ks_systemInfo&&SdkUtil.ks_systemInfo.platform=='devtools') {
  132. return true
  133. }
  134. return false
  135. }
  136. // 苹果手机是否有灵动岛
  137. public static iPhoneIsLingdongdao():boolean {
  138. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  139. if(SdkUtil.tt_systemInfo !=null) {
  140. if(SdkUtil.is_iphone_platform()||SdkUtil.tt_systemInfo.platform=='devtools') {
  141. if(SdkUtil.tt_systemInfo.model=='iPhone 14'||
  142. SdkUtil.tt_systemInfo.model=='iPhone 14 Pro'||
  143. SdkUtil.tt_systemInfo.model=='iPhone 14 Pro Max'||
  144. SdkUtil.tt_systemInfo.model=='iPhone 15'||
  145. SdkUtil.tt_systemInfo.model=='iPhone 15 Pro'||
  146. SdkUtil.tt_systemInfo.model=='iPhone 15 Pro Max') {
  147. return true
  148. }
  149. }
  150. }
  151. }
  152. return false
  153. }
  154. // 自定义事件统计
  155. public static customEventStatistics(eventType: string, objParams?: any) {
  156. eventType = eventType.toString();
  157. if (!objParams) { objParams = {}; }
  158. // console.log({'eventType': eventType},{'objParams': objParams});
  159. if (this.platform === 'wx') {
  160. //@ts-ignore
  161. if (window['wx'] && window['wx']['aldSendEvent']) {
  162. //@ts-ignore
  163. window.wx['aldSendEvent'](eventType, objParams);
  164. }
  165. }
  166. //@ts-ignore
  167. if (this.platform === 'cocos' && window.cocosAnalytics && window.cocosAnalytics.isInited()) {
  168. console.log("###统计", eventType, objParams);
  169. //@ts-ignore
  170. window.cocosAnalytics.CACustomEvent.onStarted(eventType, objParams);
  171. }
  172. }
  173. // 登录
  174. public static login(call){
  175. switch (tools.platform) {
  176. case config.Platform.TT:
  177. tt.login({
  178. force: true,
  179. success(res) {
  180. console.log(`tt_login 调用成功:${res.code} ${res.anonymousCode}`);
  181. call({"code":res.code,"anonymousCode":res.anonymousCode})
  182. },
  183. fail(err) {
  184. console.log(`tt_login 调用失败:${err}`);
  185. // call(null)
  186. },
  187. });
  188. break;
  189. case config.Platform.WX:
  190. wx.login({
  191. success(res) {
  192. console.log(`wx_login 调用成功:${res.code}`);
  193. call({"code":res.code})
  194. },
  195. fali(err) {
  196. console.log(`wx_login 调用失败:${err}`);
  197. // call(null)
  198. }
  199. })
  200. break;
  201. case config.Platform.KS:
  202. ks.login({
  203. success(res) {
  204. console.log(`ks_login 调用成功:${res.code}`);
  205. call({"code":res.code})
  206. },
  207. fali(err) {
  208. console.log(`ks_login 调用失败:${err}`);
  209. // call(null)
  210. }
  211. })
  212. break;
  213. default:
  214. call(null)
  215. break;
  216. }
  217. }
  218. // 展示loading
  219. public static showLoading(title='加载中...',mask=false) {
  220. switch (tools.platform) {
  221. case config.Platform.TT:
  222. tt.showLoading({
  223. title: title,
  224. mask:mask
  225. })
  226. break;
  227. case config.Platform.WX:
  228. wx.showLoading({
  229. title: title,
  230. mask:mask
  231. })
  232. break;
  233. case config.Platform.KS:
  234. ks.showLoading({
  235. title: title,
  236. mask:mask
  237. })
  238. break;
  239. default:
  240. break;
  241. }
  242. }
  243. // 隐藏loading
  244. public static hideLoading() {
  245. switch (tools.platform) {
  246. case config.Platform.TT:
  247. tt.hideLoading()
  248. break;
  249. case config.Platform.WX:
  250. wx.hideLoading()
  251. break;
  252. case config.Platform.KS:
  253. ks.hideLoading()
  254. break;
  255. default:
  256. break;
  257. }
  258. }
  259. // 检测显示添加桌面
  260. public static checkIsShowAddDesktop():boolean {
  261. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  262. if(SdkUtil.tt_systemInfo.appName=='Douyin' || SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  263. return true
  264. }
  265. }
  266. return false
  267. }
  268. // 分享游戏
  269. public static shareGame(title: string, imageUrl: string, videoPath: string) {
  270. switch (tools.platform) {
  271. case config.Platform.TT:
  272. if(videoPath!="") {
  273. this.ttShareScreenRecordVideo(config.gameName,videoPath)
  274. }
  275. break;
  276. case config.Platform.WX:
  277. title = title + '好刺激、好好玩'
  278. this.wxShare(title, imageUrl)
  279. break;
  280. case config.Platform.KS:
  281. title = title + '好刺激、好好玩'
  282. this.ksShare(title, imageUrl)
  283. break;
  284. default:
  285. break;
  286. }
  287. }
  288. // 获取广告id
  289. public static getAdId(ad_type = config.AD_TYPE.UNKNOWN):string {
  290. let ad_id = ""
  291. switch (tools.platform) {
  292. case config.Platform.TT:
  293. if(ad_type == config.AD_TYPE.RE_LIFE) {
  294. ad_id = config.TT_REWARD.RE_LIFE
  295. } else if (ad_type == config.AD_TYPE.ANSWER) {
  296. ad_id = config.TT_REWARD.ANSWER
  297. } else if (ad_type == config.AD_TYPE.LOOK_TIPS) {
  298. ad_id = config.TT_REWARD.LOOK_TIPS
  299. } else if (ad_type == config.AD_TYPE.UN_LOCK_24) {
  300. ad_id = config.TT_REWARD.UN_LOCK_24
  301. } else if (ad_type == config.AD_TYPE.UN_LOCK) {
  302. ad_id = config.TT_REWARD.UN_LOCK
  303. } else if (ad_type == config.AD_TYPE.ADD_TIME) {
  304. ad_id = config.TT_REWARD.ADD_TIME
  305. }
  306. break;
  307. case config.Platform.WX:
  308. if(ad_type == config.AD_TYPE.RE_LIFE) {
  309. ad_id = config.WX_REWARD.RE_LIFE
  310. } else if (ad_type == config.AD_TYPE.ANSWER) {
  311. ad_id = config.WX_REWARD.ANSWER
  312. } else if (ad_type == config.AD_TYPE.LOOK_TIPS) {
  313. ad_id = config.WX_REWARD.LOOK_TIPS
  314. } else if (ad_type == config.AD_TYPE.UN_LOCK_24) {
  315. ad_id = config.WX_REWARD.UN_LOCK_24
  316. } else if (ad_type == config.AD_TYPE.UN_LOCK) {
  317. ad_id = config.WX_REWARD.UN_LOCK
  318. } else if (ad_type == config.AD_TYPE.ADD_TIME) {
  319. ad_id = config.WX_REWARD.ADD_TIME
  320. }
  321. break;
  322. case config.Platform.KS:
  323. if(ad_type == config.AD_TYPE.RE_LIFE) {
  324. ad_id = config.KS_REWARD.RE_LIFE
  325. } else if (ad_type == config.AD_TYPE.ANSWER) {
  326. ad_id = config.KS_REWARD.ANSWER
  327. } else if (ad_type == config.AD_TYPE.LOOK_TIPS) {
  328. ad_id = config.KS_REWARD.LOOK_TIPS
  329. } else if (ad_type == config.AD_TYPE.UN_LOCK_24) {
  330. ad_id = config.KS_REWARD.UN_LOCK_24
  331. } else if (ad_type == config.AD_TYPE.UN_LOCK) {
  332. ad_id = config.KS_REWARD.UN_LOCK
  333. } else if (ad_type == config.AD_TYPE.ADD_TIME) {
  334. ad_id = config.KS_REWARD.ADD_TIME
  335. }
  336. break;
  337. default:
  338. break;
  339. }
  340. return ad_id
  341. }
  342. // 显示激励视频广告
  343. public static showVideoAd(_adUnitId: string, call_back) {
  344. if(gameManager.isFreeAds()) {
  345. call_back && call_back({"isEnded":true})
  346. return
  347. }
  348. switch (tools.platform) {
  349. case config.Platform.TT:
  350. gameManager.Singleton.showLoadingLevel()
  351. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId})
  352. break;
  353. case config.Platform.WX:
  354. gameManager.Singleton.showLoadingLevel()
  355. SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
  356. break;
  357. case config.Platform.KS:
  358. gameManager.Singleton.showLoadingLevel()
  359. SdkUtil.videoAd = ks.createRewardedVideoAd({adUnitId: _adUnitId})
  360. break;
  361. default:
  362. break;
  363. }
  364. if(SdkUtil.videoAd==null){
  365. gameManager.Singleton.hideLoadingLevel()
  366. return
  367. }
  368. if(tools.platform == config.Platform.WX) {
  369. // 取消关闭,取消错误,提示重复回调,造成奖励重复发送的问题或错误重复发送的问题
  370. SdkUtil.videoAd.offClose()
  371. SdkUtil.videoAd.offError()
  372. SdkUtil.videoAd.load().then(()=>{
  373. console.log("wx 广告加载完成");
  374. SdkUtil.isLookAd = true
  375. SdkUtil.videoAd.show()
  376. }).catch(err=>{
  377. console.error('wx 视频广告加载或展示失败:', err);
  378. let errorString = err.errCode + '-' + err.errMsg
  379. call_back && call_back({isEnded:false,errorString:errorString})
  380. SdkUtil.isLookAd = false
  381. gameManager.Singleton.hideLoadingLevel()
  382. })
  383. }
  384. if(tools.platform == config.Platform.KS) {
  385. SdkUtil.videoAd.show().then(()=>{
  386. console.log("ks 广告加载完成");
  387. SdkUtil.isLookAd = true
  388. })
  389. }
  390. if(tools.platform == config.Platform.TT) {
  391. SdkUtil.videoAd.onLoad(() => {
  392. console.log("tt 广告加载完成");
  393. SdkUtil.isLookAd = true
  394. SdkUtil.videoAd.show();
  395. });
  396. }
  397. SdkUtil.videoAd.onClose((res) => {
  398. console.log('广告关闭=',res)
  399. call_back && call_back(res)
  400. SdkUtil.isLookAd = false
  401. if(tools.platform!=config.Platform.WX) {
  402. SdkUtil.videoAd.destroy() // 微信销毁后,再次获取广告,不会显示
  403. }
  404. gameManager.Singleton.hideLoadingLevel()
  405. });
  406. SdkUtil.videoAd.onError((res) => {
  407. console.log('广告加载失败=',res)
  408. let errorString = res.errCode + '-' + res.errMsg
  409. call_back && call_back({isEnded:false,errorString:errorString})
  410. SdkUtil.isLookAd = false
  411. if(tools.platform!=config.Platform.WX) {
  412. SdkUtil.videoAd.destroy() // 微信销毁后,再次获取广告,不会显示
  413. }
  414. gameManager.Singleton.hideLoadingLevel()
  415. });
  416. if(tools.platform == config.Platform.TT) {
  417. SdkUtil.videoAd.load()
  418. }
  419. }
  420. // 注册游戏互推组件
  421. public static registerGridGamePanel() {
  422. if(SdkUtil.is_devtools_platform()==true) { return }
  423. switch (tools.platform) {
  424. case config.Platform.TT:
  425. try{
  426. let height = SdkUtil.tt_systemInfo.safeArea.bottom
  427. let gridGamePanel_top = 660
  428. if(height) { gridGamePanel_top = height - 150 }
  429. // 单宫格gridCount: "one",可设置大小和位置 size:"large",position:{top:700,left:20},
  430. // 四宫格gridCount: "four",可设置大小,设置位置无实际作用 size:"large"
  431. // 九宫格gridCount: "nine",设置大小和位置无实际作用
  432. const gridGamePanel = tt.createGridGamePanel({
  433. gridCount: "one",
  434. size:"large",
  435. position:{top:gridGamePanel_top,left:20},
  436. query: {
  437. 'tt3e9d4820a1ac27d902': "", //开局自行车
  438. // 'tt0e4878f5917a358002': "", //找茬
  439. },
  440. });
  441. SdkUtil.gridGamePanel = gridGamePanel
  442. } catch(error) {
  443. console.log('error=',error)
  444. }
  445. break;
  446. case config.Platform.WX:
  447. break;
  448. case config.Platform.KS:
  449. break;
  450. default:
  451. break;
  452. }
  453. }
  454. // 显示游戏互推组件
  455. public static showGridGamePanel() {
  456. if(SdkUtil.is_devtools_platform()==true) { return }
  457. if(!SdkUtil.gridGamePanel) {return}
  458. switch (tools.platform) {
  459. case config.Platform.TT:
  460. SdkUtil.gridGamePanel.show().then(() => {
  461. console.log("tt 展示游戏推荐组件成功");
  462. }).catch((err) => {
  463. console.error("展示游戏推荐组件失败", err);
  464. });
  465. break;
  466. case config.Platform.WX:
  467. break;
  468. case config.Platform.KS:
  469. break;
  470. default:
  471. break;
  472. }
  473. }
  474. // 隐藏互推游戏组件
  475. public static hideGridGamePanel() {
  476. if(SdkUtil.is_devtools_platform()==true) { return }
  477. if(!SdkUtil.gridGamePanel) {return}
  478. switch (tools.platform) {
  479. case config.Platform.TT:
  480. SdkUtil.gridGamePanel.hide()
  481. break;
  482. case config.Platform.WX:
  483. break;
  484. case config.Platform.KS:
  485. break;
  486. default:
  487. break;
  488. }
  489. }
  490. //------------------------------ 微信相关 ------------------------------//
  491. // 微信分享
  492. public static wxShare(title: string, imageUrl: string) {
  493. // console.log('wx分享=',title, 'imageUrl=',imageUrl)
  494. wx.showShareMenu({
  495. withShareTicket: true,
  496. // shareAppMessage(可以删除):显示分享给好友选项,shareTimeline(可以删除):显示分享至朋友圈选项
  497. // 可以只开启前者。如果要开启后者,则两者必须都开启才能生效。
  498. // menus: ['shareAppMessage', 'shareTimeline'],
  499. menus: ['shareAppMessage'],
  500. complete: () => {}
  501. });
  502. // 主动分享
  503. wx.shareAppMessage({
  504. title: title,
  505. imageUrl: imageUrl
  506. });
  507. }
  508. //------------------------------ 快手相关 ------------------------------//
  509. // 快手加载分包
  510. public static ksLoadSubpackage(name:string,cb) {
  511. ks.loadSubpackage({
  512. name: name,
  513. success: function(res) {
  514. // console.log(`success->${name}=${res}`)
  515. cb && cb()
  516. },
  517. fail: function(err) {
  518. // console.log(`err->${name}=${err}`)
  519. }
  520. });
  521. }
  522. // 快手分享
  523. public static ksShare(title: string, imageUrl: string) {
  524. // console.log('ks分享=',title, 'imageUrl=',imageUrl)
  525. ks.shareAppMessage({
  526. success:(res)=>{ console.log('ks success=',res) },
  527. fail:(err)=>{ console.log('ks fail=',err) }
  528. });
  529. }
  530. //------------------------------ 抖音相关 ------------------------------//
  531. // 抖音添加快捷键(目前仅支持:抖音(Douyin) 和 抖音极速版(douyin_lite))
  532. public static ttAddShortcut(onSuccess:Function = null, onFail:Function = null) {
  533. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  534. if(SdkUtil.tt_systemInfo.appName=='Douyin'||SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  535. if(SdkUtil.tt_systemInfo.brand=='Apple') {
  536. tt.addShortcut({
  537. success() {
  538. console.log("添加桌面成功");
  539. onSuccess && onSuccess(null)
  540. },
  541. fail(err) {
  542. console.log("添加桌面失败", err.errMsg);
  543. onFail && onFail(err)
  544. },
  545. });
  546. } else if(SdkUtil.tt_systemInfo.brand=='Android') {
  547. // 检测只支持安卓
  548. tt.checkShortcut({
  549. success(res) {
  550. console.log("检查快捷方式", res.status);
  551. if(res.status.exist==false||res.status.needUpdate==true) {
  552. tt.addShortcut({
  553. success() {
  554. console.log("添加桌面成功");
  555. onSuccess && onSuccess(null)
  556. },
  557. fail(err) {
  558. console.log("添加桌面失败", err.errMsg);
  559. onFail && onFail(err)
  560. },
  561. });
  562. }
  563. },
  564. fail(err) {
  565. console.log("检查快捷方式失败", err.errMsg);
  566. onFail && onFail(err)
  567. },
  568. });
  569. }
  570. }
  571. }
  572. }
  573. // 抖音注册信息
  574. public static ttRegisterInfo() {
  575. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  576. tt.onShow((res) => {
  577. console.log('tt.onShow =', res)
  578. // console.log('res=',res)
  579. if(res.scene == '021036' || res.scene == '101036') {
  580. SdkUtil.tt_isToEnterFromSidebar = true
  581. }
  582. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  583. SdkUtil.tt_isToEnterFromSidebar = true
  584. }
  585. });
  586. tt.onHide(()=>{
  587. console.log('tt.onHide')
  588. if(SdkUtil.isLookAd==false) {
  589. statisticsManager.uploadRecordUserLevel(false)
  590. }
  591. })
  592. tt.checkScene({
  593. scene: "sidebar",
  594. success: (res) => {
  595. console.log("check scene success: ", res.isExist);
  596. if(res.isExist != undefined || res.isExist != null) {
  597. SdkUtil.tt_isSupportSidebar = res.isExist
  598. }
  599. },
  600. fail: (res) => {
  601. console.log("check scene fail:", res);
  602. }
  603. });
  604. let options = tt.getLaunchOptionsSync()
  605. console.log('getLaunchOptionsSync=', options)
  606. if(options.scene == '021036' || options.scene == '101036') {
  607. SdkUtil.tt_isToEnterFromSidebar = true
  608. }
  609. }
  610. }
  611. // 抖音检测是否显示奖励
  612. public static ttCheckSceneShowRewards():boolean {
  613. return SdkUtil.tt_isSupportSidebar
  614. }
  615. // 抖音检测是否从侧边栏进入
  616. public static ttCheckToEnterFromSidebar():boolean {
  617. return SdkUtil.tt_isToEnterFromSidebar
  618. }
  619. // 抖音导航到侧边栏场景
  620. public static ttNavToSidebarScene() {
  621. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  622. tt.navigateToScene({
  623. scene: "sidebar",
  624. success: (res) => {
  625. // console.log("navigate to scene success");
  626. },
  627. fail: (res) => {
  628. // console.log("navigate to scene fail: ", res);
  629. },
  630. });
  631. }
  632. }
  633. // 抖音开启屏幕录制
  634. public static ttStartScreenRecording() {
  635. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  636. return
  637. }
  638. if(this.tt_systemInfo.platform == 'devtools') {
  639. console.log('抖音模拟器')
  640. return
  641. }
  642. if(this.tt_isRecording==true) {
  643. this.ttStopScreenRecording()
  644. }
  645. if(!this.tt_gameRecorder) {
  646. this.tt_gameRecorder = tt.getGameRecorderManager()
  647. }
  648. this.tt_gameRecorder.start({duration: this.tt_totalRecord})
  649. this.tt_gameRecorder.onStart(()=> {
  650. this.tt_isRecording = true
  651. // console.log('GameRecorder onStart onStart onStart')
  652. })
  653. this.tt_gameRecorder.onStop((res)=> {
  654. // console.log('GameRecorder onStop onStop onStop=',res)
  655. this.tt_isRecording = false
  656. this.tt_recordVideoPath = res.videoPath
  657. })
  658. this.tt_gameRecorder.onError((e)=> {
  659. console.log('ttGameRecord error:',e)
  660. })
  661. }
  662. // 抖音关闭屏幕录制
  663. public static ttStopScreenRecording(isClearVideoPath:boolean = false) {
  664. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  665. return
  666. }
  667. if(this.tt_gameRecorder == null) {
  668. return
  669. }
  670. if(isClearVideoPath) {
  671. this.tt_recordVideoPath = "";
  672. }
  673. this.tt_gameRecorder.stop()
  674. }
  675. // 抖音获取屏幕录制视频文件
  676. public static ttGetScreenRecordingVideoPath():string {
  677. return this.tt_recordVideoPath;
  678. }
  679. // 抖音分享屏幕录制视频
  680. private static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
  681. // console.log('tt_录制视频路径=',videoPath)
  682. if(videoPath.length<=0) {
  683. return
  684. }
  685. tt.shareAppMessage({
  686. title: title,
  687. templateId: config.TT_SHARE_TEMPLATEID,
  688. channel: "video",
  689. extra: {
  690. videoTopics: [config.gameName],
  691. hashtag_list: ['小游戏','小程序'],
  692. videoPath: videoPath,
  693. withVideoId: true,
  694. },
  695. success: (res) => {
  696. console.log('抖音分享屏幕录制视频,成功=',res)
  697. onSuccess && onSuccess();
  698. },
  699. fail: (e) => {
  700. // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
  701. console.log('抖音分享屏幕录制视频,失败=',e)
  702. onFail && onFail()
  703. }
  704. })
  705. }
  706. }