sdkUtil.ts 29 KB

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