sdkUtil.ts 30 KB

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