sdkUtil.ts 18 KB

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