sdkUtil.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. import { _decorator, error, SpriteFrame, sys } from "cc";
  2. import { config } from "./config";
  3. import { uiManager } from "./manager/uiManager";
  4. import { tools } from "./tools";
  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 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 = 300; //抖音总录制时间
  28. private static tt_isRecording:boolean = false; //抖音是否录制中
  29. //------------------------------ 公共 ------------------------------//
  30. public static init() {
  31. this.ttGetSystemInfo(()=> {
  32. this.ttRegisterInfo()
  33. })
  34. }
  35. // 自定义事件统计
  36. public static customEventStatistics(eventType: string, objParams?: any) {
  37. eventType = eventType.toString();
  38. if (!objParams) { objParams = {}; }
  39. // console.log({'eventType': eventType},{'objParams': objParams});
  40. if (this.platform === 'wx') {
  41. //@ts-ignore
  42. if (window['wx'] && window['wx']['aldSendEvent']) {
  43. //@ts-ignore
  44. window.wx['aldSendEvent'](eventType, objParams);
  45. }
  46. }
  47. //@ts-ignore
  48. if (this.platform === 'cocos' && window.cocosAnalytics && window.cocosAnalytics.isInited()) {
  49. console.log("###统计", eventType, objParams);
  50. //@ts-ignore
  51. window.cocosAnalytics.CACustomEvent.onStarted(eventType, objParams);
  52. }
  53. }
  54. // 登录
  55. public static login(call){
  56. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  57. tt.login({
  58. force: true,
  59. success(res) {
  60. console.log(`login 调用成功${res.code} ${res.anonymousCode}`);
  61. call({"code":res.code,"anonymousCode":res.anonymousCode})
  62. },
  63. fail(res) {
  64. console.log(`login 调用失败`);
  65. call(null)
  66. },
  67. });
  68. }else{
  69. call(null)
  70. }
  71. }
  72. // 获取用户信息
  73. public static getUserInfo(call){
  74. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  75. tt.getUserInfo({
  76. // withCredentials: true,
  77. // withRealNameAuthenticationInfo: true,
  78. success(res) {
  79. console.log(`getUserInfo 调用成功`, res.userInfo);
  80. call(res.userInfo)
  81. },
  82. fail(res) {
  83. console.log(`getUserInfo 调用失败`, res.errMsg);
  84. },
  85. });
  86. }else{
  87. call(null)
  88. }
  89. }
  90. // 震动
  91. public static vibrateShort(){
  92. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  93. tt.vibrateShort({
  94. success(res) {
  95. // console.log(`${res}`);
  96. },
  97. fail(res) {
  98. // console.log(`vibrateShort调用失败`);
  99. },
  100. });
  101. }
  102. }
  103. // 关于loading
  104. public static showLoading(title:string) {
  105. if(title.length<=0) { return }
  106. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  107. tt.showLoading({
  108. title: title,
  109. // success(res) { console.log(`${res}`);},
  110. // fail(err) { console.log(`showLoading调用失败`,err);},
  111. })
  112. }
  113. }
  114. public static hideLoading() {
  115. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  116. tt.hideLoading({
  117. // success(res) { console.log(`${res}`); },
  118. // fail(err) { console.log(`hideLoading调用失败`,err); },
  119. });
  120. }
  121. }
  122. // 展示toast
  123. public static showToast(title:string,duration=2000) {
  124. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  125. tt.showToast({
  126. title: title,
  127. duration: duration,
  128. success(res) {
  129. console.log(`${res}`);
  130. },
  131. fail(res) {
  132. console.log(`showToast调用失败`);
  133. },
  134. });
  135. }
  136. }
  137. // 选择系统相册
  138. public static choosSystemImage(call_back) {
  139. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  140. tt.chooseImage({
  141. sourceType: ["album"],
  142. count:1,
  143. success:(res)=>{
  144. console.log('chooseImage调用成功=',res)
  145. if(res.tempFilePaths.length>0) {
  146. call_back(res.tempFilePaths[0])
  147. }
  148. },
  149. fail(err) {
  150. console.log(`chooseImage调用失败=`,err);
  151. },
  152. })
  153. } else {
  154. // call_back()
  155. }
  156. }
  157. // 检测显示添加桌面
  158. public static checkIsShowAddDesktop():boolean {
  159. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  160. if(SdkUtil.tt_systemInfo.appName=='Douyin' || SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  161. return true
  162. }
  163. }
  164. return false
  165. }
  166. // 分享游戏
  167. public static shareGame(title: string, call_back) {
  168. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  169. if(title.length<=0) { title = config.TT_CONFIG.SHARE_RELIFE_TITLE }
  170. let desc = tools.sys_config.share_des
  171. if(desc==undefined||desc==null) { desc = ''}
  172. tt.shareAppMessage({
  173. title: title,
  174. desc: desc,
  175. success() {
  176. console.log("分享成功")
  177. SdkUtil.showToast('分享成功')
  178. call_back(true)
  179. },
  180. fail(e) {
  181. console.log("分享失败",e)
  182. SdkUtil.showToast('分享失败')
  183. call_back(false)
  184. },
  185. });
  186. } else if(sys.platform == sys.Platform.WECHAT_GAME) {
  187. if(title.length<=0) { title = config.WX_CONFIG.SHARE_RELIFE_TITLE }
  188. }else{
  189. call_back(true)
  190. }
  191. }
  192. // public static shareGame(title: string, imageUrl: string, videoPath: string) {
  193. // if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  194. // if(videoPath!="") {
  195. // // this.ttShareScreenRecordVideo(config.gameName,videoPath)
  196. // }
  197. // } else if(sys.platform == sys.Platform.WECHAT_GAME) {
  198. // title = title + '好刺激、好好玩'
  199. // // this.wxShare(title, imageUrl)
  200. // }
  201. // }
  202. // 获取广告id
  203. public static getAdId(ad_type = config.ADS_TYPE.UNKNOWN):string {
  204. let ad_id = ""
  205. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  206. if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
  207. ad_id = config.TT_CONFIG.ADS_ID_INFINITE_DEGREE_VIDEO
  208. } else if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
  209. ad_id = config.TT_CONFIG.ADS_ID_RELIFE_VIDEO
  210. } else if(ad_type==config.ADS_TYPE.GAME_RESTART) {
  211. ad_id = config.TT_CONFIG.ADS_ID_RESTART
  212. }
  213. } else if (sys.platform == sys.Platform.WECHAT_GAME) {
  214. if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
  215. ad_id = config.WX_CONFIG.ADS_ID_INFINITE_DEGREE_VIDEO
  216. } else if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
  217. ad_id = config.WX_CONFIG.ADS_ID_RELIFE_VIDEO
  218. } else if(ad_type==config.ADS_TYPE.GAME_RESTART) {
  219. ad_id = config.WX_CONFIG.ADS_ID_RESTART
  220. }
  221. // if(SdkUtil.KS_GAME) {
  222. // } else {
  223. // }
  224. }
  225. return ad_id
  226. }
  227. // 显示激励视频广告
  228. public static showVideoAd(_adUnitId: string, call_back) {
  229. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  230. uiManager.Instance().showLoading()
  231. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  232. } else {
  233. call_back({"isEnded":true})
  234. return
  235. }
  236. if(SdkUtil.videoAd==null){
  237. uiManager.Instance().hideLoading()
  238. return
  239. }
  240. SdkUtil.videoAd.onLoad(() => {
  241. SdkUtil.isLookAd = true
  242. SdkUtil.videoAd.show();
  243. console.log("广告加载完成");
  244. });
  245. SdkUtil.videoAd.onClose((res) => {
  246. call_back(res)
  247. SdkUtil.isLookAd = false
  248. SdkUtil.videoAd.destroy()
  249. uiManager.Instance().hideLoading()
  250. });
  251. SdkUtil.videoAd.onError((res) => {
  252. let errorString = res.errCode + '-' + res.errMsg
  253. call_back({isEnded:false,errorString:errorString})
  254. SdkUtil.isLookAd = false
  255. SdkUtil.videoAd.destroy()
  256. uiManager.Instance().hideLoading()
  257. });
  258. SdkUtil.videoAd.load()
  259. }
  260. //------------------------------ 抖音相关 ------------------------------//
  261. // 抖音获取系统信息
  262. public static ttGetSystemInfo(callback) {
  263. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  264. tt.getSystemInfo({
  265. success:(res) => {
  266. // console.log('tt.getSystemInfo=',res)
  267. // appName: "Douyin" appName: "douyin_lite"
  268. SdkUtil.tt_systemInfo = res
  269. callback()
  270. }
  271. })
  272. }
  273. }
  274. // 抖音添加快捷键(目前仅支持:抖音(Douyin) 和 抖音极速版(douyin_lite))
  275. public static ttAddShortcut(onSuccess:Function = null, onFail:Function = null) {
  276. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  277. if(SdkUtil.tt_systemInfo.appName=='Douyin'||SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  278. if(SdkUtil.tt_systemInfo.brand=='Apple') {
  279. tt.addShortcut({
  280. success() {
  281. console.log("添加桌面成功");
  282. onSuccess && onSuccess(null)
  283. },
  284. fail(err) {
  285. console.log("添加桌面失败", err.errMsg);
  286. onFail && onFail(err)
  287. },
  288. });
  289. } else if(SdkUtil.tt_systemInfo.brand=='Android') {
  290. // 检测只支持安卓
  291. tt.checkShortcut({
  292. success(res) {
  293. console.log("检查快捷方式", res.status);
  294. if(res.status.exist==false||res.status.needUpdate==true) {
  295. tt.addShortcut({
  296. success() {
  297. console.log("添加桌面成功");
  298. onSuccess && onSuccess(null)
  299. },
  300. fail(err) {
  301. console.log("添加桌面失败", err.errMsg);
  302. onFail && onFail(err)
  303. },
  304. });
  305. }
  306. },
  307. fail(err) {
  308. console.log("检查快捷方式失败", err.errMsg);
  309. onFail && onFail(err)
  310. },
  311. });
  312. }
  313. }
  314. }
  315. }
  316. // 抖音注册信息
  317. public static ttRegisterInfo() {
  318. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  319. tt.onShow((res) => {
  320. console.log('tt.onShow =', res)
  321. // console.log('res=',res)
  322. if(res.scene == '021036' || res.scene == '101036') {
  323. SdkUtil.tt_isToEnterFromSidebar = true
  324. }
  325. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  326. SdkUtil.tt_isToEnterFromSidebar = true
  327. }
  328. });
  329. tt.onHide(()=>{
  330. console.log('tt.onHide')
  331. if(SdkUtil.isLookAd==false) {
  332. // statisticsManager.uploadRecordUserLevel(false)
  333. }
  334. })
  335. tt.checkScene({
  336. scene: "sidebar",
  337. success: (res) => {
  338. console.log("check scene success: ", res.isExist);
  339. if(res.isExist != undefined || res.isExist != null) {
  340. SdkUtil.tt_isSupportSidebar = res.isExist
  341. }
  342. },
  343. fail: (res) => {
  344. console.log("check scene fail:", res);
  345. }
  346. });
  347. let options = tt.getLaunchOptionsSync()
  348. console.log('getLaunchOptionsSync=', options)
  349. if(options.scene == '021036' || options.scene == '101036') {
  350. SdkUtil.tt_isToEnterFromSidebar = true
  351. }
  352. }
  353. }
  354. // 抖音检测是否显示奖励
  355. public static ttCheckSceneShowRewards():boolean {
  356. return SdkUtil.tt_isSupportSidebar
  357. }
  358. // 抖音检测是否从侧边栏进入
  359. public static ttCheckToEnterFromSidebar():boolean {
  360. return SdkUtil.tt_isToEnterFromSidebar
  361. }
  362. // 抖音导航到侧边栏场景
  363. public static ttNavToSidebarScene() {
  364. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  365. tt.navigateToScene({
  366. scene: "sidebar",
  367. success: (res) => {
  368. // console.log("navigate to scene success");
  369. },
  370. fail: (res) => {
  371. // console.log("navigate to scene fail: ", res);
  372. },
  373. });
  374. }
  375. }
  376. // 抖音开启屏幕录制
  377. public static ttStartScreenRecording() {
  378. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  379. return
  380. }
  381. if(this.tt_systemInfo.platform == 'devtools') {
  382. console.log('抖音模拟器')
  383. return
  384. }
  385. if(this.tt_isRecording==true) {
  386. this.ttStopScreenRecording()
  387. }
  388. if(!this.tt_gameRecorder) {
  389. this.tt_gameRecorder = tt.getGameRecorderManager()
  390. }
  391. this.tt_gameRecorder.start({duration: this.tt_totalRecord})
  392. this.tt_gameRecorder.onStart(()=> {
  393. this.tt_isRecording = true
  394. // console.log('GameRecorder onStart onStart onStart')
  395. })
  396. this.tt_gameRecorder.onStop((res)=> {
  397. // console.log('GameRecorder onStop onStop onStop=',res)
  398. this.tt_isRecording = false
  399. this.tt_recordVideoPath = res.videoPath
  400. })
  401. this.tt_gameRecorder.onError((e)=> {
  402. console.log('ttGameRecord error:',e)
  403. })
  404. }
  405. // 抖音关闭屏幕录制
  406. public static ttStopScreenRecording(isClearVideoPath:boolean = false) {
  407. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  408. return
  409. }
  410. if(this.tt_gameRecorder == null) {
  411. return
  412. }
  413. if(isClearVideoPath) {
  414. this.tt_recordVideoPath = "";
  415. }
  416. this.tt_gameRecorder.stop()
  417. }
  418. // 抖音获取屏幕录制视频文件
  419. public static ttGetScreenRecordingVideoPath():string {
  420. return this.tt_recordVideoPath;
  421. }
  422. // 抖音分享屏幕录制视频
  423. private static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
  424. // tt.shareAppMessage({
  425. // title: title,
  426. // templateId: config.TT_SHARE_TEMPLATEID,
  427. // channel: "video",
  428. // extra: {
  429. // videoTopics: [config.gameName],
  430. // hashtag_list: ['小游戏','小程序'],
  431. // videoPath: videoPath,
  432. // withVideoId: true,
  433. // },
  434. // success: (res) => {
  435. // console.log('抖音分享屏幕录制视频,成功=',res)
  436. // onSuccess && onSuccess();
  437. // },
  438. // fail: (e) => {
  439. // // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
  440. // console.log('抖音分享屏幕录制视频,失败=',e)
  441. // onFail && onFail()
  442. // }
  443. // })
  444. }
  445. }