sdkUtil.ts 38 KB

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