sdkUtil.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. }
  381. } else if (sys.platform == sys.Platform.WECHAT_GAME) {
  382. if(SdkUtil.KS_GAME) {
  383. if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
  384. ad_id = config.KS_CONFIG.ADS_ID_INFINITE_DEGREE_VIDEO
  385. } else if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
  386. ad_id = config.KS_CONFIG.ADS_ID_RELIFE_VIDEO
  387. } else if(ad_type==config.ADS_TYPE.GAME_RESTART) {
  388. ad_id = config.KS_CONFIG.ADS_ID_RESTART
  389. } else if(ad_type==config.ADS_TYPE.GAME_SIGN_DOUBLE) {
  390. ad_id = config.KS_CONFIG.ADS_ID_SIGN_DOUBLE
  391. }
  392. } else {
  393. if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
  394. ad_id = config.WX_CONFIG.ADS_ID_INFINITE_DEGREE_VIDEO
  395. } else if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
  396. ad_id = config.WX_CONFIG.ADS_ID_RELIFE_VIDEO
  397. } else if(ad_type==config.ADS_TYPE.GAME_RESTART) {
  398. ad_id = config.WX_CONFIG.ADS_ID_RESTART
  399. } else if(ad_type==config.ADS_TYPE.GAME_SIGN_DOUBLE) {
  400. ad_id = config.WX_CONFIG.ADS_ID_SIGN_DOUBLE
  401. }
  402. }
  403. }
  404. return ad_id
  405. }
  406. // 显示激励视频广告
  407. public static showVideoAd(_adUnitId: string, call_back) {
  408. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  409. uiManager.Instance().showLoading()
  410. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  411. }
  412. else if(sys.platform==sys.Platform.WECHAT_GAME) {
  413. call_back({"isEnded":true})
  414. // uiManager.Instance().showLoading()
  415. // if(SdkUtil.KS_GAME) {
  416. // SdkUtil.videoAd = ks.createRewardedVideoAd({adUnitId: _adUnitId});
  417. // } else {
  418. // SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
  419. // }
  420. }
  421. else {
  422. call_back({"isEnded":true})
  423. return
  424. }
  425. if(SdkUtil.videoAd==null){
  426. uiManager.Instance().hideLoading()
  427. return
  428. }
  429. SdkUtil.videoAd.onLoad(() => {
  430. SdkUtil.isLookAd = true
  431. SdkUtil.videoAd.show();
  432. console.log("广告加载完成");
  433. });
  434. SdkUtil.videoAd.onClose((res) => {
  435. // console.log('广告关闭')
  436. uiManager.Instance().hideLoading()
  437. SdkUtil.isLookAd = false
  438. SdkUtil.videoAd.destroy()
  439. call_back(res)
  440. });
  441. SdkUtil.videoAd.onError((res) => {
  442. // console.log('广告错误')
  443. uiManager.Instance().hideLoading()
  444. SdkUtil.isLookAd = false
  445. SdkUtil.videoAd.destroy()
  446. let errorString = res.errCode + '-' + res.errMsg
  447. call_back({isEnded:false,errorString:errorString})
  448. });
  449. SdkUtil.videoAd.load()
  450. }
  451. //------------------------------ 微信相关 ------------------------------//
  452. // 微信检查授权用户信息
  453. public static wxCheckAuthUserInfo(cb) {
  454. wx.getSetting({
  455. success(res) {
  456. if(res.authSetting['scope.userInfo'] === true) { // 已经授权
  457. cb(true)
  458. } else {
  459. cb(false)
  460. }
  461. },
  462. fail(err) {
  463. cb(false)
  464. }
  465. })
  466. }
  467. // 微信分享
  468. public static wxShare(title: string, imageUrl: string='') {
  469. // console.log('wx分享=',title, 'imageUrl=',imageUrl)
  470. wx.showShareMenu({
  471. withShareTicket: true,
  472. // shareAppMessage(可以删除):显示分享给好友选项,shareTimeline(可以删除):显示分享至朋友圈选项
  473. // 可以只开启前者。如果要开启后者,则两者必须都开启才能生效。
  474. // menus: ['shareAppMessage', 'shareTimeline'],
  475. menus: ['shareAppMessage'],
  476. complete: () => {}
  477. });
  478. // 主动分享
  479. wx.shareAppMessage({
  480. title: title,
  481. imageUrl: imageUrl
  482. });
  483. }
  484. //------------------------------ 快手相关 ------------------------------//
  485. // 快手分享
  486. public static ksShare(title: string, imageUrl:string='', success_cb = null, fail_cb = null) {
  487. // console.log('ks分享=',title, 'imageUrl=',imageUrl)
  488. ks.shareAppMessage({
  489. success:(res)=>{ if(success_cb){ success_cb(res) } },
  490. fail:(err)=>{ if(fail_cb){ fail_cb(err) }}
  491. });
  492. }
  493. //------------------------------ 抖音相关 ------------------------------//
  494. // 抖音获取系统信息
  495. public static ttGetSystemInfo(callback) {
  496. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  497. tt.getSystemInfo({
  498. success:(res) => {
  499. // console.log('tt.getSystemInfo=',res)
  500. // appName: "Douyin" appName: "douyin_lite"
  501. SdkUtil.tt_systemInfo = res
  502. callback()
  503. }
  504. })
  505. }
  506. }
  507. // 分享
  508. public static ttShare(title:string, desc:string, sucess_cb=null, fail_cb=null) {
  509. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  510. tt.shareAppMessage({
  511. title: title,
  512. desc: desc,
  513. success() {
  514. console.log("分享成功")
  515. // this.showToast('分享成功')
  516. if(sucess_cb) { sucess_cb() }
  517. },
  518. fail(e) {
  519. console.log("分享失败",e)
  520. if(fail_cb) { fail_cb() }
  521. },
  522. });
  523. }
  524. }
  525. // 抖音添加快捷键(目前仅支持:抖音(Douyin) 和 抖音极速版(douyin_lite))
  526. public static ttAddShortcut(onSuccess:Function = null, onFail:Function = null) {
  527. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  528. if(SdkUtil.tt_systemInfo.appName=='Douyin'||SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  529. if(SdkUtil.tt_systemInfo.brand=='Apple') {
  530. tt.addShortcut({
  531. success() {
  532. console.log("添加桌面成功");
  533. onSuccess && onSuccess(null)
  534. },
  535. fail(err) {
  536. console.log("添加桌面失败", err.errMsg);
  537. onFail && onFail(err)
  538. },
  539. });
  540. } else if(SdkUtil.tt_systemInfo.brand=='Android') {
  541. // 检测只支持安卓
  542. tt.checkShortcut({
  543. success(res) {
  544. console.log("检查快捷方式", res.status);
  545. if(res.status.exist==false||res.status.needUpdate==true) {
  546. tt.addShortcut({
  547. success() {
  548. console.log("添加桌面成功");
  549. onSuccess && onSuccess(null)
  550. },
  551. fail(err) {
  552. console.log("添加桌面失败", err.errMsg);
  553. onFail && onFail(err)
  554. },
  555. });
  556. }
  557. },
  558. fail(err) {
  559. console.log("检查快捷方式失败", err.errMsg);
  560. onFail && onFail(err)
  561. },
  562. });
  563. }
  564. }
  565. }
  566. }
  567. // 抖音注册信息
  568. public static ttRegisterInfo() {
  569. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  570. tt.onShow((res) => {
  571. console.log('tt.onShow =', res)
  572. // console.log('res=',res)
  573. if(res.scene == '021036' || res.scene == '101036') {
  574. SdkUtil.tt_isToEnterFromSidebar = true
  575. }
  576. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  577. SdkUtil.tt_isToEnterFromSidebar = true
  578. }
  579. });
  580. tt.onHide(()=>{
  581. console.log('tt.onHide')
  582. if(SdkUtil.isLookAd==false) {
  583. // statisticsManager.uploadRecordUserLevel(false)
  584. }
  585. })
  586. tt.checkScene({
  587. scene: "sidebar",
  588. success: (res) => {
  589. console.log("check scene success: ", res.isExist);
  590. if(res.isExist != undefined || res.isExist != null) {
  591. SdkUtil.tt_isSupportSidebar = res.isExist
  592. }
  593. },
  594. fail: (res) => {
  595. console.log("check scene fail:", res);
  596. }
  597. });
  598. let options = tt.getLaunchOptionsSync()
  599. console.log('getLaunchOptionsSync=', options)
  600. if(options.scene == '021036' || options.scene == '101036') {
  601. SdkUtil.tt_isToEnterFromSidebar = true
  602. }
  603. }
  604. }
  605. // 抖音检测是否显示奖励
  606. public static ttCheckSceneShowRewards():boolean {
  607. return SdkUtil.tt_isSupportSidebar
  608. }
  609. // 抖音检测是否从侧边栏进入
  610. public static ttCheckToEnterFromSidebar():boolean {
  611. return SdkUtil.tt_isToEnterFromSidebar
  612. }
  613. // 抖音导航到侧边栏场景
  614. public static ttNavToSidebarScene() {
  615. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  616. tt.navigateToScene({
  617. scene: "sidebar",
  618. success: (res) => {
  619. // console.log("navigate to scene success");
  620. },
  621. fail: (res) => {
  622. // console.log("navigate to scene fail: ", res);
  623. },
  624. });
  625. }
  626. }
  627. // 抖音开启屏幕录制
  628. public static ttStartScreenRecording() {
  629. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  630. return
  631. }
  632. if(this.tt_systemInfo.platform == 'devtools') {
  633. console.log('抖音模拟器')
  634. return
  635. }
  636. if(this.tt_isRecording==true) {
  637. this.ttStopScreenRecording()
  638. }
  639. if(!this.tt_gameRecorder) {
  640. this.tt_gameRecorder = tt.getGameRecorderManager()
  641. }
  642. this.tt_gameRecorder.start({duration: this.tt_totalRecord})
  643. this.tt_gameRecorder.onStart(()=> {
  644. this.tt_isRecording = true
  645. // console.log('GameRecorder onStart onStart onStart')
  646. })
  647. this.tt_gameRecorder.onStop((res)=> {
  648. // console.log('GameRecorder onStop onStop onStop=',res)
  649. this.tt_isRecording = false
  650. this.tt_recordVideoPath = res.videoPath
  651. })
  652. this.tt_gameRecorder.onError((e)=> {
  653. console.log('ttGameRecord error:',e)
  654. })
  655. }
  656. // 抖音关闭屏幕录制
  657. public static ttStopScreenRecording(isClearVideoPath:boolean = false) {
  658. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  659. return
  660. }
  661. if(this.tt_gameRecorder == null) {
  662. return
  663. }
  664. if(isClearVideoPath) {
  665. this.tt_recordVideoPath = "";
  666. }
  667. this.tt_gameRecorder.stop()
  668. }
  669. // 抖音获取屏幕录制视频文件
  670. public static ttGetScreenRecordingVideoPath():string {
  671. return this.tt_recordVideoPath;
  672. }
  673. // 抖音分享屏幕录制视频
  674. private static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
  675. console.log('tt_录制视频路径=',videoPath)
  676. if(videoPath.length<=0) {
  677. return
  678. }
  679. tt.shareAppMessage({
  680. title: title,
  681. templateId: config.TT_CONFIG.SHARE_RECORD_VIDEO_ID,
  682. channel: "video",
  683. extra: {
  684. videoTopics: [config.gameName],
  685. hashtag_list: ['小游戏','小程序'],
  686. videoPath: videoPath,
  687. withVideoId: true,
  688. },
  689. success: (res) => {
  690. console.log('抖音分享屏幕录制视频,成功=',res)
  691. onSuccess && onSuccess();
  692. },
  693. fail: (e) => {
  694. // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
  695. console.log('抖音分享屏幕录制视频,失败=',e)
  696. onFail && onFail()
  697. }
  698. })
  699. }
  700. }