sdkUtil.ts 29 KB

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