sdkUtil.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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 = 4000; //抖音总录制时间
  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 checkAuthUserInfo(cb) {
  61. if(sys.platform == sys.Platform.WECHAT_GAME && !SdkUtil.KS_GAME) {
  62. SdkUtil.wxCheckAuthUserInfo(cb)
  63. } else {
  64. cb(true)
  65. }
  66. }
  67. // 登录
  68. public static login(call){
  69. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  70. tt.login({
  71. force: true,
  72. success(res) {
  73. console.log(`tt_login 调用成功:${res.code} ${res.anonymousCode}`);
  74. call({"code":res.code,"anonymousCode":res.anonymousCode})
  75. },
  76. fail(err) {
  77. console.log(`tt_login 调用失败:${err}`);
  78. // call(null)
  79. },
  80. });
  81. }
  82. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  83. if(SdkUtil.KS_GAME) {
  84. ks.login({
  85. success(res) {
  86. console.log(`ks_login 调用成功:${res.code}`);
  87. call({"code":res.code})
  88. },
  89. fali(err) {
  90. console.log(`ks_login 调用失败:${err}`);
  91. // call(null)
  92. }
  93. })
  94. } else {
  95. wx.login({
  96. success(res) {
  97. console.log(`wx_login 调用成功:${res.code}`);
  98. call({"code":res.code})
  99. },
  100. fali(err) {
  101. console.log(`wx_login 调用失败:${err}`);
  102. // call(null)
  103. }
  104. })
  105. }
  106. }
  107. else{
  108. call(null)
  109. }
  110. }
  111. // 获取用户信息
  112. public static getUserInfo(call){
  113. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  114. tt.getUserInfo({
  115. // withCredentials: true,
  116. // withRealNameAuthenticationInfo: true,
  117. success(res) {
  118. console.log(`tt_getUserInfo 调用成功`, res);
  119. call(res.userInfo)
  120. },
  121. fail(err) {
  122. console.log(`tt_getUserInfo 调用失败`, err);
  123. },
  124. });
  125. }
  126. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  127. if(SdkUtil.KS_GAME) {
  128. ks.getUserInfo({
  129. success(res) {
  130. console.log(`ks_getUserInfo 调用成功`, res);
  131. call(res.userInfo)
  132. },
  133. fail(err) {
  134. console.log(`ks_getUserInfo 调用失败`, err);
  135. },
  136. })
  137. } else {
  138. SdkUtil.wxCheckAuthUserInfo((is_true)=>{
  139. if(is_true) {
  140. wx.getUserInfo({
  141. success(res) {
  142. console.log(`wx_getUserInfo 调用成功`, res);
  143. call(res.userInfo)
  144. },
  145. fail(err) {
  146. console.log(`wx_getUserInfo 调用失败`, err);
  147. },
  148. })
  149. }
  150. })
  151. }
  152. }
  153. else{
  154. call(null)
  155. }
  156. }
  157. // 震动
  158. public static vibrateShort(){
  159. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  160. tt.vibrateShort({ success(res) {}, fail(err) {} });
  161. }
  162. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  163. if(SdkUtil.KS_GAME) {
  164. ks.vibrateShort({ success(res) {}, fail(err) {} });
  165. } else {
  166. wx.vibrateShort({ success(res) {}, fail(err) {} });
  167. }
  168. }
  169. }
  170. // 关于loading
  171. public static showLoading(title:string) {
  172. if(title.length<=0) { return }
  173. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  174. tt.showLoading({ title: title, success(res) {},fail(err) {} })
  175. }
  176. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  177. if(SdkUtil.KS_GAME) {
  178. ks.showLoading({ title: title, success(res) {},fail(err) {} })
  179. } else {
  180. wx.showLoading({ title: title, success(res) {},fail(err) {} })
  181. }
  182. }
  183. }
  184. public static hideLoading() {
  185. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  186. tt.hideLoading({ success(res) { }, fail(err) { } });
  187. }
  188. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  189. if(SdkUtil.KS_GAME) {
  190. ks.hideLoading({ success(res) { }, fail(err) { } });
  191. } else {
  192. wx.hideLoading({ success(res) { }, fail(err) { } });
  193. }
  194. }
  195. }
  196. // 展示toast
  197. public static showToast(title:string,duration=2000,success_cb=null, fail_cb=null) {
  198. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  199. tt.showToast({
  200. title: title,
  201. duration: duration,
  202. success(res) { if(success_cb){ success_cb() } },
  203. fail(err) { if(fail_cb) { fail_cb() } },
  204. });
  205. }
  206. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  207. if(SdkUtil.KS_GAME) {
  208. ks.showToast({
  209. title: title,
  210. duration: duration,
  211. success(res) { if(success_cb){ success_cb() } },
  212. fail(err) { if(fail_cb) { fail_cb() } },
  213. });
  214. } else {
  215. wx.showToast({
  216. title: title,
  217. duration: duration,
  218. success(res) { if(success_cb){ success_cb() } },
  219. fail(err) { if(fail_cb) { fail_cb() } },
  220. });
  221. }
  222. }
  223. }
  224. // 展示modal (确定:res.confirm 取消:res.cancel)
  225. public static showModal(title:string, content:string, suc_cb=null, fail_cb=null, confirmText:string='确定', showCancel:boolean=true, cancelText:string='取消') {
  226. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  227. tt.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. }
  237. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  238. if(SdkUtil.KS_GAME) {
  239. ks.showModal({
  240. title: title,
  241. content: content,
  242. confirmText: confirmText,
  243. showCancel: showCancel,
  244. cancelText: cancelText,
  245. success(res) { if(suc_cb) { suc_cb(res) } },
  246. fail(err) { if(fail_cb){ fail_cb(err) } },
  247. });
  248. } else {
  249. wx.showModal({
  250. title: title,
  251. content: content,
  252. confirmText: confirmText,
  253. showCancel: showCancel,
  254. cancelText: cancelText,
  255. success(res) { if(suc_cb) { suc_cb(res) } },
  256. fail(err) { if(fail_cb){ fail_cb(err) } },
  257. });
  258. }
  259. } else {
  260. if(suc_cb) {
  261. suc_cb({'confirm':true})
  262. }
  263. }
  264. }
  265. // 展示actionSheet (点击的索引:`res.tapIndex`)
  266. public static showActionSheet(item_list:string[], suc_cb=null, fail_cb=null) {
  267. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  268. tt.showActionSheet({
  269. item_list,
  270. success(res) { if(suc_cb) { suc_cb(res) } },
  271. fail(err) { if(fail_cb) { fail_cb(err) } },
  272. });
  273. }
  274. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  275. if(SdkUtil.KS_GAME) {
  276. ks.showActionSheet({
  277. item_list,
  278. success(res) { if(suc_cb) { suc_cb(res) } },
  279. fail(err) { if(fail_cb) { fail_cb(err) } },
  280. });
  281. } else {
  282. wx.showActionSheet({
  283. item_list,
  284. success(res) { if(suc_cb) { suc_cb(res) } },
  285. fail(err) { if(fail_cb) { fail_cb(err) } },
  286. });
  287. }
  288. }
  289. }
  290. // 选择系统相册
  291. public static choosSystemImage(call_back) {
  292. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  293. tt.chooseImage({
  294. sourceType: ["album"],
  295. count:1,
  296. success:(res)=>{
  297. // console.log('chooseImage调用成功=${res}')
  298. if(res.tempFilePaths.length>0) { call_back(res.tempFilePaths[0])}
  299. },
  300. fail(err) { console.log(`tt_chooseImage调用失败=${err}`); },
  301. })
  302. }
  303. else if(sys.platform == sys.Platform.WECHAT_GAME) {
  304. if(SdkUtil.KS_GAME) {
  305. ks.chooseMedia({
  306. mediaType: ["image"],
  307. sourceType: ["album"],
  308. count:1,
  309. success:(res)=>{
  310. // console.log('chooseImage调用成功=${res}')
  311. if(res.tempFilePaths.length>0) { call_back(res.tempFilePaths[0])}
  312. },
  313. fail(err) { console.log(`wx_chooseImage调用失败=${err}`); },
  314. })
  315. } else {
  316. wx.chooseMedia({
  317. mediaType: ["image"],
  318. sourceType: ["album"],
  319. count:1,
  320. success:(res)=>{
  321. // console.log('chooseImage调用成功=${res}')
  322. if(res.tempFilePaths.length>0) { call_back(res.tempFilePaths[0])}
  323. },
  324. fail(err) { console.log(`wx_chooseImage调用失败=${err}`); },
  325. })
  326. }
  327. }
  328. }
  329. // 检测显示添加桌面
  330. public static checkIsShowAddDesktop():boolean {
  331. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  332. if(SdkUtil.tt_systemInfo.appName=='Douyin' || SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  333. return true
  334. }
  335. }
  336. return false
  337. }
  338. // 分享游戏
  339. public static shareGame(title: string, desc:string, call_back=null) {
  340. if(desc==undefined||desc==null) { desc = config.gameName}
  341. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  342. SdkUtil.ttShare(title,desc, null, ()=>{
  343. this.showToast('分享失败')
  344. })
  345. } else if(sys.platform == sys.Platform.WECHAT_GAME) {
  346. if(SdkUtil.KS_GAME) {
  347. SdkUtil.ksShare(desc)
  348. } else {
  349. SdkUtil.wxShare(desc)
  350. }
  351. }else{
  352. if(call_back) {
  353. call_back(true)
  354. }
  355. }
  356. }
  357. public static shareGameVideo() {
  358. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  359. let videoPath = SdkUtil.ttGetScreenRecordingVideoPath()
  360. uiManager.Instance().showLoading()
  361. this.ttShareScreenRecordVideo(config.gameName,videoPath,()=>{
  362. uiManager.Instance().hideLoading()
  363. }, ()=>{
  364. uiManager.Instance().hideLoading()
  365. })
  366. }
  367. }
  368. // 获取广告id
  369. public static getAdId(ad_type = config.ADS_TYPE.UNKNOWN):string {
  370. let ad_id = ""
  371. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  372. if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
  373. ad_id = config.TT_CONFIG.ADS_ID_INFINITE_DEGREE_VIDEO
  374. } else if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
  375. ad_id = config.TT_CONFIG.ADS_ID_RELIFE_VIDEO
  376. } else if(ad_type==config.ADS_TYPE.GAME_RESTART) {
  377. ad_id = config.TT_CONFIG.ADS_ID_RESTART
  378. } else if(ad_type==config.ADS_TYPE.GAME_SIGN_DOUBLE) {
  379. ad_id = config.TT_CONFIG.ADS_ID_SIGN_DOUBLE
  380. } else if(ad_type==config.ADS_TYPE.GAME_SIGN_BUQIAN) {
  381. ad_id = config.TT_CONFIG.ADS_ID_SIGN_BUQIAN
  382. }
  383. } else if (sys.platform == sys.Platform.WECHAT_GAME) {
  384. if(SdkUtil.KS_GAME) {
  385. if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
  386. ad_id = config.KS_CONFIG.ADS_ID_INFINITE_DEGREE_VIDEO
  387. } else if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
  388. ad_id = config.KS_CONFIG.ADS_ID_RELIFE_VIDEO
  389. } else if(ad_type==config.ADS_TYPE.GAME_RESTART) {
  390. ad_id = config.KS_CONFIG.ADS_ID_RESTART
  391. } else if(ad_type==config.ADS_TYPE.GAME_SIGN_DOUBLE) {
  392. ad_id = config.KS_CONFIG.ADS_ID_SIGN_DOUBLE
  393. } else if(ad_type==config.ADS_TYPE.GAME_SIGN_BUQIAN) {
  394. ad_id = config.KS_CONFIG.ADS_ID_SIGN_BUQIAN
  395. }
  396. } else {
  397. if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
  398. ad_id = config.WX_CONFIG.ADS_ID_INFINITE_DEGREE_VIDEO
  399. } else if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
  400. ad_id = config.WX_CONFIG.ADS_ID_RELIFE_VIDEO
  401. } else if(ad_type==config.ADS_TYPE.GAME_RESTART) {
  402. ad_id = config.WX_CONFIG.ADS_ID_RESTART
  403. } else if(ad_type==config.ADS_TYPE.GAME_SIGN_DOUBLE) {
  404. ad_id = config.WX_CONFIG.ADS_ID_SIGN_DOUBLE
  405. } else if(ad_type==config.ADS_TYPE.GAME_SIGN_BUQIAN) {
  406. ad_id = config.WX_CONFIG.ADS_ID_SIGN_BUQIAN
  407. }
  408. }
  409. }
  410. return ad_id
  411. }
  412. // 显示激励视频广告
  413. public static showVideoAd(_adUnitId: string, call_back) {
  414. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  415. uiManager.Instance().showLoading()
  416. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  417. }
  418. else if(sys.platform==sys.Platform.WECHAT_GAME) {
  419. call_back({"isEnded":true})
  420. // uiManager.Instance().showLoading()
  421. // if(SdkUtil.KS_GAME) {
  422. // SdkUtil.videoAd = ks.createRewardedVideoAd({adUnitId: _adUnitId});
  423. // } else {
  424. // SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
  425. // }
  426. }
  427. else {
  428. call_back({"isEnded":true})
  429. return
  430. }
  431. if(SdkUtil.videoAd==null){
  432. uiManager.Instance().hideLoading()
  433. return
  434. }
  435. SdkUtil.videoAd.onLoad(() => {
  436. SdkUtil.isLookAd = true
  437. SdkUtil.videoAd.show();
  438. console.log("广告加载完成");
  439. });
  440. SdkUtil.videoAd.onClose((res) => {
  441. // console.log('广告关闭')
  442. uiManager.Instance().hideLoading()
  443. SdkUtil.isLookAd = false
  444. SdkUtil.videoAd.destroy()
  445. call_back(res)
  446. });
  447. SdkUtil.videoAd.onError((res) => {
  448. // console.log('广告错误')
  449. uiManager.Instance().hideLoading()
  450. SdkUtil.isLookAd = false
  451. SdkUtil.videoAd.destroy()
  452. let errorString = res.errCode + '-' + res.errMsg
  453. call_back({isEnded:false,errorString:errorString})
  454. });
  455. SdkUtil.videoAd.load()
  456. }
  457. //------------------------------ 微信相关 ------------------------------//
  458. // 微信检查授权用户信息
  459. public static wxCheckAuthUserInfo(cb) {
  460. wx.getSetting({
  461. success(res) {
  462. if(res.authSetting['scope.userInfo'] === true) { // 已经授权
  463. cb(true)
  464. } else {
  465. cb(false)
  466. }
  467. },
  468. fail(err) {
  469. cb(false)
  470. }
  471. })
  472. }
  473. // 微信分享
  474. public static wxShare(title: string, imageUrl: string='') {
  475. // console.log('wx分享=',title, 'imageUrl=',imageUrl)
  476. wx.showShareMenu({
  477. withShareTicket: true,
  478. // shareAppMessage(可以删除):显示分享给好友选项,shareTimeline(可以删除):显示分享至朋友圈选项
  479. // 可以只开启前者。如果要开启后者,则两者必须都开启才能生效。
  480. // menus: ['shareAppMessage', 'shareTimeline'],
  481. menus: ['shareAppMessage'],
  482. complete: () => {}
  483. });
  484. // 主动分享
  485. wx.shareAppMessage({
  486. title: title,
  487. imageUrl: imageUrl
  488. });
  489. }
  490. //------------------------------ 快手相关 ------------------------------//
  491. // 快手分享
  492. public static ksShare(title: string, imageUrl:string='', success_cb = null, fail_cb = null) {
  493. // console.log('ks分享=',title, 'imageUrl=',imageUrl)
  494. ks.shareAppMessage({
  495. success:(res)=>{ if(success_cb){ success_cb(res) } },
  496. fail:(err)=>{ if(fail_cb){ fail_cb(err) }}
  497. });
  498. }
  499. //------------------------------ 抖音相关 ------------------------------//
  500. // 抖音获取系统信息
  501. public static ttGetSystemInfo(callback) {
  502. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  503. tt.getSystemInfo({
  504. success:(res) => {
  505. // console.log('tt.getSystemInfo=',res)
  506. // appName: "Douyin" appName: "douyin_lite"
  507. SdkUtil.tt_systemInfo = res
  508. callback()
  509. }
  510. })
  511. }
  512. }
  513. // 分享
  514. public static ttShare(title:string, desc:string, sucess_cb=null, fail_cb=null) {
  515. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  516. tt.shareAppMessage({
  517. title: title,
  518. desc: desc,
  519. success() {
  520. console.log("分享成功")
  521. // this.showToast('分享成功')
  522. if(sucess_cb) { sucess_cb() }
  523. },
  524. fail(e) {
  525. console.log("分享失败",e)
  526. if(fail_cb) { fail_cb() }
  527. },
  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_CONFIG.SHARE_RECORD_VIDEO_ID,
  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. }