sdkUtil.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. import { _decorator, error, SpriteFrame, sys } from "cc";
  2. import { config } from "./config";
  3. import { uiManager } from "./manager/uiManager";
  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 KS_GAME:boolean = false; //是否ks游戏
  20. public static videoAd:any =null;
  21. public static isLookAd:boolean = false; //是否在看广告
  22. public static tt_systemInfo: any = null; //抖音_系统信息
  23. private static tt_isSupportSidebar:boolean = false; //抖音_是否支持侧边栏
  24. private static tt_isToEnterFromSidebar:boolean = false; //抖音_是否从侧边栏进入
  25. private static tt_gameRecorder:any = null; //抖音游戏录制
  26. private static tt_recordVideoPath:string = ''; //抖音录制视频路径
  27. private static tt_totalRecord:number = 300; //抖音总录制时间
  28. private static tt_isRecording:boolean = false; //抖音是否录制中
  29. //------------------------------ 公共 ------------------------------//
  30. public static init() {
  31. if(sys.platform==sys.Platform.WECHAT_GAME) {
  32. let isKSGame = typeof KSGameGlobal != 'undefined'
  33. // console.log('isKSGame=',isKSGame)
  34. SdkUtil.KS_GAME = isKSGame
  35. }
  36. this.ttGetSystemInfo(()=> {
  37. this.ttRegisterInfo()
  38. })
  39. }
  40. // 自定义事件统计
  41. public static customEventStatistics(eventType: string, objParams?: any) {
  42. eventType = eventType.toString();
  43. if (!objParams) { objParams = {}; }
  44. // console.log({'eventType': eventType},{'objParams': objParams});
  45. if (this.platform === 'wx') {
  46. //@ts-ignore
  47. if (window['wx'] && window['wx']['aldSendEvent']) {
  48. //@ts-ignore
  49. window.wx['aldSendEvent'](eventType, objParams);
  50. }
  51. }
  52. //@ts-ignore
  53. if (this.platform === 'cocos' && window.cocosAnalytics && window.cocosAnalytics.isInited()) {
  54. console.log("###统计", eventType, objParams);
  55. //@ts-ignore
  56. window.cocosAnalytics.CACustomEvent.onStarted(eventType, objParams);
  57. }
  58. }
  59. // 登录
  60. public static login(call){
  61. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  62. tt.login({
  63. force: true,
  64. success(res) {
  65. console.log(`tt_login 调用成功:${res.code} ${res.anonymousCode}`);
  66. call({"code":res.code,"anonymousCode":res.anonymousCode})
  67. },
  68. fail(err) {
  69. console.log(`tt_login 调用失败:${err}`);
  70. // call(null)
  71. },
  72. });
  73. }
  74. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  75. if(SdkUtil.KS_GAME) {
  76. ks.login({
  77. success(res) {
  78. console.log(`ks_login 调用成功:${res.code}`);
  79. call({"code":res.code})
  80. },
  81. fali(err) {
  82. console.log(`ks_login 调用失败:${err}`);
  83. // call(null)
  84. }
  85. })
  86. } else {
  87. wx.login({
  88. success(res) {
  89. console.log(`wx_login 调用成功:${res.code}`);
  90. call({"code":res.code})
  91. },
  92. fali(err) {
  93. console.log(`wx_login 调用失败:${err}`);
  94. // call(null)
  95. }
  96. })
  97. }
  98. }
  99. else{
  100. call(null)
  101. }
  102. }
  103. // 获取用户信息
  104. public static getUserInfo(call){
  105. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  106. tt.getUserInfo({
  107. // withCredentials: true,
  108. // withRealNameAuthenticationInfo: true,
  109. success(res) {
  110. console.log(`tt_getUserInfo 调用成功`, res.userInfo);
  111. call(res.userInfo)
  112. },
  113. fail(err) {
  114. console.log(`tt_getUserInfo 调用失败`, err);
  115. },
  116. });
  117. }
  118. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  119. if(SdkUtil.KS_GAME) {
  120. ks.getUserInfo({
  121. success(res) {
  122. console.log(`ks_getUserInfo 调用成功`, res.userInfo);
  123. call(res.userInfo)
  124. },
  125. fail(err) {
  126. console.log(`ks_getUserInfo 调用失败`, err);
  127. },
  128. })
  129. } else {
  130. wx.getUserInfo({
  131. success(res) {
  132. console.log(`wx_getUserInfo 调用成功`, res.userInfo);
  133. call(res.userInfo)
  134. },
  135. fail(err) {
  136. console.log(`wx_getUserInfo 调用失败`, err);
  137. },
  138. })
  139. }
  140. }
  141. else{
  142. call(null)
  143. }
  144. }
  145. // 震动
  146. public static vibrateShort(){
  147. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  148. tt.vibrateShort({ success(res) {}, fail(err) {} });
  149. }
  150. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  151. if(SdkUtil.KS_GAME) {
  152. ks.vibrateShort({ success(res) {}, fail(err) {} });
  153. } else {
  154. wx.vibrateShort({ success(res) {}, fail(err) {} });
  155. }
  156. }
  157. }
  158. // 关于loading
  159. public static showLoading(title:string) {
  160. if(title.length<=0) { return }
  161. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  162. tt.showLoading({ title: title, success(res) {},fail(err) {} })
  163. }
  164. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  165. if(SdkUtil.KS_GAME) {
  166. ks.showLoading({ title: title, success(res) {},fail(err) {} })
  167. } else {
  168. wx.showLoading({ title: title, success(res) {},fail(err) {} })
  169. }
  170. }
  171. }
  172. public static hideLoading() {
  173. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  174. tt.hideLoading({ success(res) { }, fail(err) { } });
  175. }
  176. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  177. if(SdkUtil.KS_GAME) {
  178. ks.hideLoading({ success(res) { }, fail(err) { } });
  179. } else {
  180. wx.hideLoading({ success(res) { }, fail(err) { } });
  181. }
  182. }
  183. }
  184. // 展示toast
  185. public static showToast(title:string,duration=2000,success_cb=null, fail_cb=null) {
  186. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  187. tt.showToast({
  188. title: title,
  189. duration: duration,
  190. success(res) { if(success_cb){ success_cb() } },
  191. fail(err) { if(fail_cb) { fail_cb() } },
  192. });
  193. }
  194. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  195. if(SdkUtil.KS_GAME) {
  196. ks.showToast({
  197. title: title,
  198. duration: duration,
  199. success(res) { if(success_cb){ success_cb() } },
  200. fail(err) { if(fail_cb) { fail_cb() } },
  201. });
  202. } else {
  203. wx.showToast({
  204. title: title,
  205. duration: duration,
  206. success(res) { if(success_cb){ success_cb() } },
  207. fail(err) { if(fail_cb) { fail_cb() } },
  208. });
  209. }
  210. }
  211. }
  212. // 展示modal (确定:res.confirm 取消:res.cancel)
  213. public static showModal(title:string, content:string, suc_cb=null, fail_cb=null, confirmText:string='确定', showCancel:boolean=true, cancelText:string='取消') {
  214. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  215. tt.showModal({
  216. title: title,
  217. content: content,
  218. confirmText: confirmText,
  219. showCancel: showCancel,
  220. cancelText: cancelText,
  221. success(res) { if(suc_cb) { suc_cb(res) } },
  222. fail(err) { if(fail_cb){ fail_cb(err) } },
  223. });
  224. }
  225. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  226. if(SdkUtil.KS_GAME) {
  227. ks.showModal({
  228. title: title,
  229. content: content,
  230. confirmText: confirmText,
  231. showCancel: showCancel,
  232. cancelText: cancelText,
  233. success(res) { if(suc_cb) { suc_cb(res) } },
  234. fail(err) { if(fail_cb){ fail_cb(err) } },
  235. });
  236. } else {
  237. wx.showModal({
  238. title: title,
  239. content: content,
  240. confirmText: confirmText,
  241. showCancel: showCancel,
  242. cancelText: cancelText,
  243. success(res) { if(suc_cb) { suc_cb(res) } },
  244. fail(err) { if(fail_cb){ fail_cb(err) } },
  245. });
  246. }
  247. }
  248. }
  249. // 展示actionSheet (点击的索引:`res.tapIndex`)
  250. public static showActionSheet(item_list:string[], suc_cb=null, fail_cb=null) {
  251. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  252. tt.showActionSheet({
  253. item_list,
  254. success(res) { if(suc_cb) { suc_cb(res) } },
  255. fail(err) { if(fail_cb) { fail_cb(err) } },
  256. });
  257. }
  258. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  259. if(SdkUtil.KS_GAME) {
  260. ks.showActionSheet({
  261. item_list,
  262. success(res) { if(suc_cb) { suc_cb(res) } },
  263. fail(err) { if(fail_cb) { fail_cb(err) } },
  264. });
  265. } else {
  266. wx.showActionSheet({
  267. item_list,
  268. success(res) { if(suc_cb) { suc_cb(res) } },
  269. fail(err) { if(fail_cb) { fail_cb(err) } },
  270. });
  271. }
  272. }
  273. }
  274. // 选择系统相册
  275. public static choosSystemImage(call_back) {
  276. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  277. tt.chooseImage({
  278. sourceType: ["album"],
  279. count:1,
  280. success:(res)=>{
  281. // console.log('chooseImage调用成功=${res}')
  282. if(res.tempFilePaths.length>0) { call_back(res.tempFilePaths[0])}
  283. },
  284. fail(err) { console.log(`tt_chooseImage调用失败=${err}`); },
  285. })
  286. }
  287. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  288. if(SdkUtil.KS_GAME) {
  289. ks.chooseMedia({
  290. mediaType: ["image"],
  291. sourceType: ["album"],
  292. count:1,
  293. success:(res)=>{
  294. // console.log('chooseImage调用成功=${res}')
  295. if(res.tempFilePaths.length>0) { call_back(res.tempFilePaths[0])}
  296. },
  297. fail(err) { console.log(`wx_chooseImage调用失败=${err}`); },
  298. })
  299. } else {
  300. wx.chooseMedia({
  301. mediaType: ["image"],
  302. sourceType: ["album"],
  303. count:1,
  304. success:(res)=>{
  305. // console.log('chooseImage调用成功=${res}')
  306. if(res.tempFilePaths.length>0) { call_back(res.tempFilePaths[0])}
  307. },
  308. fail(err) { console.log(`wx_chooseImage调用失败=${err}`); },
  309. })
  310. }
  311. }
  312. }
  313. // 检测显示添加桌面
  314. public static checkIsShowAddDesktop():boolean {
  315. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  316. if(SdkUtil.tt_systemInfo.appName=='Douyin' || SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  317. return true
  318. }
  319. }
  320. return false
  321. }
  322. // 分享游戏
  323. public static shareGame(title: string, desc:string, call_back) {
  324. if(desc==undefined||desc==null) { desc = ''}
  325. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  326. SdkUtil.ttShare(title,desc, null, ()=>{
  327. this.showToast('分享失败')
  328. })
  329. } else if(sys.platform == sys.Platform.WECHAT_GAME) {
  330. if(SdkUtil.KS_GAME) {
  331. SdkUtil.ksShare(desc)
  332. } else {
  333. SdkUtil.wxShare(desc)
  334. }
  335. }else{
  336. call_back(true)
  337. }
  338. }
  339. public static shareGameVideo() {
  340. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  341. let videoPath = SdkUtil.ttGetScreenRecordingVideoPath()
  342. uiManager.Instance().showLoading()
  343. this.ttShareScreenRecordVideo(config.gameName,videoPath,()=>{
  344. uiManager.Instance().hideLoading()
  345. }, ()=>{
  346. uiManager.Instance().hideLoading()
  347. })
  348. }
  349. }
  350. // 获取广告id
  351. public static getAdId(ad_type = config.ADS_TYPE.UNKNOWN):string {
  352. let ad_id = ""
  353. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  354. if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
  355. ad_id = config.TT_CONFIG.ADS_ID_INFINITE_DEGREE_VIDEO
  356. } else if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
  357. ad_id = config.TT_CONFIG.ADS_ID_RELIFE_VIDEO
  358. } else if(ad_type==config.ADS_TYPE.GAME_RESTART) {
  359. ad_id = config.TT_CONFIG.ADS_ID_RESTART
  360. }
  361. } else if (sys.platform == sys.Platform.WECHAT_GAME) {
  362. if(SdkUtil.KS_GAME) {
  363. if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
  364. ad_id = config.KS_CONFIG.ADS_ID_INFINITE_DEGREE_VIDEO
  365. } else if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
  366. ad_id = config.KS_CONFIG.ADS_ID_RELIFE_VIDEO
  367. } else if(ad_type==config.ADS_TYPE.GAME_RESTART) {
  368. ad_id = config.KS_CONFIG.ADS_ID_RESTART
  369. }
  370. } else {
  371. if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
  372. ad_id = config.WX_CONFIG.ADS_ID_INFINITE_DEGREE_VIDEO
  373. } else if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
  374. ad_id = config.WX_CONFIG.ADS_ID_RELIFE_VIDEO
  375. } else if(ad_type==config.ADS_TYPE.GAME_RESTART) {
  376. ad_id = config.WX_CONFIG.ADS_ID_RESTART
  377. }
  378. }
  379. }
  380. return ad_id
  381. }
  382. // 显示激励视频广告
  383. public static showVideoAd(_adUnitId: string, call_back) {
  384. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  385. uiManager.Instance().showLoading()
  386. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  387. }
  388. else if(sys.platform==sys.Platform.WECHAT_GAME) {
  389. call_back({"isEnded":true})
  390. // uiManager.Instance().showLoading()
  391. // if(SdkUtil.KS_GAME) {
  392. // SdkUtil.videoAd = ks.createRewardedVideoAd({adUnitId: _adUnitId});
  393. // } else {
  394. // SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
  395. // }
  396. }
  397. else {
  398. call_back({"isEnded":true})
  399. return
  400. }
  401. if(SdkUtil.videoAd==null){
  402. uiManager.Instance().hideLoading()
  403. return
  404. }
  405. SdkUtil.videoAd.onLoad(() => {
  406. SdkUtil.isLookAd = true
  407. SdkUtil.videoAd.show();
  408. console.log("广告加载完成");
  409. });
  410. SdkUtil.videoAd.onClose((res) => {
  411. // console.log('广告关闭')
  412. uiManager.Instance().hideLoading()
  413. SdkUtil.isLookAd = false
  414. SdkUtil.videoAd.destroy()
  415. call_back(res)
  416. });
  417. SdkUtil.videoAd.onError((res) => {
  418. // console.log('广告错误')
  419. uiManager.Instance().hideLoading()
  420. SdkUtil.isLookAd = false
  421. SdkUtil.videoAd.destroy()
  422. let errorString = res.errCode + '-' + res.errMsg
  423. call_back({isEnded:false,errorString:errorString})
  424. });
  425. SdkUtil.videoAd.load()
  426. }
  427. //------------------------------ 微信相关 ------------------------------//
  428. // 微信分享
  429. public static wxShare(title: string, imageUrl: string='') {
  430. // console.log('wx分享=',title, 'imageUrl=',imageUrl)
  431. wx.showShareMenu({
  432. withShareTicket: true,
  433. // shareAppMessage(可以删除):显示分享给好友选项,shareTimeline(可以删除):显示分享至朋友圈选项
  434. // 可以只开启前者。如果要开启后者,则两者必须都开启才能生效。
  435. // menus: ['shareAppMessage', 'shareTimeline'],
  436. menus: ['shareAppMessage'],
  437. complete: () => {}
  438. });
  439. // 主动分享
  440. wx.shareAppMessage({
  441. title: title,
  442. imageUrl: imageUrl
  443. });
  444. }
  445. //------------------------------ 快手相关 ------------------------------//
  446. // 快手分享
  447. public static ksShare(title: string, imageUrl:string='', success_cb = null, fail_cb = null) {
  448. // console.log('ks分享=',title, 'imageUrl=',imageUrl)
  449. ks.shareAppMessage({
  450. success:(res)=>{ if(success_cb){ success_cb(res) } },
  451. fail:(err)=>{ if(fail_cb){ fail_cb(err) }}
  452. });
  453. }
  454. //------------------------------ 抖音相关 ------------------------------//
  455. // 抖音获取系统信息
  456. public static ttGetSystemInfo(callback) {
  457. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  458. tt.getSystemInfo({
  459. success:(res) => {
  460. // console.log('tt.getSystemInfo=',res)
  461. // appName: "Douyin" appName: "douyin_lite"
  462. SdkUtil.tt_systemInfo = res
  463. callback()
  464. }
  465. })
  466. }
  467. }
  468. // 分享
  469. public static ttShare(title:string, desc:string, sucess_cb=null, fail_cb=null) {
  470. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  471. tt.shareAppMessage({
  472. title: title,
  473. desc: desc,
  474. success() {
  475. console.log("分享成功")
  476. // this.showToast('分享成功')
  477. if(sucess_cb) { sucess_cb() }
  478. },
  479. fail(e) {
  480. console.log("分享失败",e)
  481. if(fail_cb) { fail_cb() }
  482. },
  483. });
  484. }
  485. }
  486. // 抖音添加快捷键(目前仅支持:抖音(Douyin) 和 抖音极速版(douyin_lite))
  487. public static ttAddShortcut(onSuccess:Function = null, onFail:Function = null) {
  488. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  489. if(SdkUtil.tt_systemInfo.appName=='Douyin'||SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  490. if(SdkUtil.tt_systemInfo.brand=='Apple') {
  491. tt.addShortcut({
  492. success() {
  493. console.log("添加桌面成功");
  494. onSuccess && onSuccess(null)
  495. },
  496. fail(err) {
  497. console.log("添加桌面失败", err.errMsg);
  498. onFail && onFail(err)
  499. },
  500. });
  501. } else if(SdkUtil.tt_systemInfo.brand=='Android') {
  502. // 检测只支持安卓
  503. tt.checkShortcut({
  504. success(res) {
  505. console.log("检查快捷方式", res.status);
  506. if(res.status.exist==false||res.status.needUpdate==true) {
  507. tt.addShortcut({
  508. success() {
  509. console.log("添加桌面成功");
  510. onSuccess && onSuccess(null)
  511. },
  512. fail(err) {
  513. console.log("添加桌面失败", err.errMsg);
  514. onFail && onFail(err)
  515. },
  516. });
  517. }
  518. },
  519. fail(err) {
  520. console.log("检查快捷方式失败", err.errMsg);
  521. onFail && onFail(err)
  522. },
  523. });
  524. }
  525. }
  526. }
  527. }
  528. // 抖音注册信息
  529. public static ttRegisterInfo() {
  530. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  531. tt.onShow((res) => {
  532. console.log('tt.onShow =', res)
  533. // console.log('res=',res)
  534. if(res.scene == '021036' || res.scene == '101036') {
  535. SdkUtil.tt_isToEnterFromSidebar = true
  536. }
  537. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  538. SdkUtil.tt_isToEnterFromSidebar = true
  539. }
  540. });
  541. tt.onHide(()=>{
  542. console.log('tt.onHide')
  543. if(SdkUtil.isLookAd==false) {
  544. // statisticsManager.uploadRecordUserLevel(false)
  545. }
  546. })
  547. tt.checkScene({
  548. scene: "sidebar",
  549. success: (res) => {
  550. console.log("check scene success: ", res.isExist);
  551. if(res.isExist != undefined || res.isExist != null) {
  552. SdkUtil.tt_isSupportSidebar = res.isExist
  553. }
  554. },
  555. fail: (res) => {
  556. console.log("check scene fail:", res);
  557. }
  558. });
  559. let options = tt.getLaunchOptionsSync()
  560. console.log('getLaunchOptionsSync=', options)
  561. if(options.scene == '021036' || options.scene == '101036') {
  562. SdkUtil.tt_isToEnterFromSidebar = true
  563. }
  564. }
  565. }
  566. // 抖音检测是否显示奖励
  567. public static ttCheckSceneShowRewards():boolean {
  568. return SdkUtil.tt_isSupportSidebar
  569. }
  570. // 抖音检测是否从侧边栏进入
  571. public static ttCheckToEnterFromSidebar():boolean {
  572. return SdkUtil.tt_isToEnterFromSidebar
  573. }
  574. // 抖音导航到侧边栏场景
  575. public static ttNavToSidebarScene() {
  576. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  577. tt.navigateToScene({
  578. scene: "sidebar",
  579. success: (res) => {
  580. // console.log("navigate to scene success");
  581. },
  582. fail: (res) => {
  583. // console.log("navigate to scene fail: ", res);
  584. },
  585. });
  586. }
  587. }
  588. // 抖音开启屏幕录制
  589. public static ttStartScreenRecording() {
  590. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  591. return
  592. }
  593. if(this.tt_systemInfo.platform == 'devtools') {
  594. console.log('抖音模拟器')
  595. return
  596. }
  597. if(this.tt_isRecording==true) {
  598. this.ttStopScreenRecording()
  599. }
  600. if(!this.tt_gameRecorder) {
  601. this.tt_gameRecorder = tt.getGameRecorderManager()
  602. }
  603. this.tt_gameRecorder.start({duration: this.tt_totalRecord})
  604. this.tt_gameRecorder.onStart(()=> {
  605. this.tt_isRecording = true
  606. // console.log('GameRecorder onStart onStart onStart')
  607. })
  608. this.tt_gameRecorder.onStop((res)=> {
  609. // console.log('GameRecorder onStop onStop onStop=',res)
  610. this.tt_isRecording = false
  611. this.tt_recordVideoPath = res.videoPath
  612. })
  613. this.tt_gameRecorder.onError((e)=> {
  614. console.log('ttGameRecord error:',e)
  615. })
  616. }
  617. // 抖音关闭屏幕录制
  618. public static ttStopScreenRecording(isClearVideoPath:boolean = false) {
  619. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  620. return
  621. }
  622. if(this.tt_gameRecorder == null) {
  623. return
  624. }
  625. if(isClearVideoPath) {
  626. this.tt_recordVideoPath = "";
  627. }
  628. this.tt_gameRecorder.stop()
  629. }
  630. // 抖音获取屏幕录制视频文件
  631. public static ttGetScreenRecordingVideoPath():string {
  632. return this.tt_recordVideoPath;
  633. }
  634. // 抖音分享屏幕录制视频
  635. private static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
  636. console.log('tt_录制视频路径=',videoPath)
  637. if(videoPath.length<=0) {
  638. return
  639. }
  640. tt.shareAppMessage({
  641. title: title,
  642. templateId: config.TT_CONFIG.SHARE_RECORD_VIDEO_ID,
  643. channel: "video",
  644. extra: {
  645. videoTopics: [config.gameName],
  646. hashtag_list: ['小游戏','小程序'],
  647. videoPath: videoPath,
  648. withVideoId: true,
  649. },
  650. success: (res) => {
  651. console.log('抖音分享屏幕录制视频,成功=',res)
  652. onSuccess && onSuccess();
  653. },
  654. fail: (e) => {
  655. // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
  656. console.log('抖音分享屏幕录制视频,失败=',e)
  657. onFail && onFail()
  658. }
  659. })
  660. }
  661. }