sdkUtil.ts 34 KB

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