sdkUtil.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. if(SdkUtil.KS_GAME) {
  176. gameManager.Singleton.showLoadingLevel()
  177. SdkUtil.videoAd = ks.createRewardedVideoAd({adUnitId: _adUnitId});
  178. }
  179. else {
  180. console.log('wx ad')
  181. // gameManager.Singleton.showLoadingLevel()
  182. // SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
  183. return
  184. }
  185. }
  186. if(SdkUtil.videoAd==null){
  187. gameManager.Singleton.hideLoadingLevel()
  188. return
  189. }
  190. if(SdkUtil.KS_GAME) {
  191. SdkUtil.videoAd.show().then(()=>{
  192. console.log("ks 广告加载完成");
  193. SdkUtil.isLookAd = true
  194. })
  195. } else {
  196. SdkUtil.videoAd.load()
  197. SdkUtil.videoAd.onLoad(() => {
  198. console.log("广告加载完成");
  199. SdkUtil.isLookAd = true
  200. SdkUtil.videoAd.show();
  201. });
  202. }
  203. SdkUtil.videoAd.onClose((res) => {
  204. console.log('广告关闭=',res)
  205. call_back(res)
  206. SdkUtil.isLookAd = false
  207. SdkUtil.videoAd.destroy()
  208. gameManager.Singleton.hideLoadingLevel()
  209. });
  210. SdkUtil.videoAd.onError((res) => {
  211. console.log('广告加载失败=',res)
  212. let errorString = res.errCode + '-' + res.errMsg
  213. call_back({isEnded:false,errorString:errorString})
  214. SdkUtil.isLookAd = false
  215. SdkUtil.videoAd.destroy()
  216. gameManager.Singleton.hideLoadingLevel()
  217. });
  218. }
  219. //------------------------------ 微信相关 ------------------------------//
  220. // 微信分享
  221. public static wxShare(title: string, imageUrl: string) {
  222. // console.log('wx分享=',title, 'imageUrl=',imageUrl)
  223. wx.showShareMenu({
  224. withShareTicket: true,
  225. // shareAppMessage(可以删除):显示分享给好友选项,shareTimeline(可以删除):显示分享至朋友圈选项
  226. // 可以只开启前者。如果要开启后者,则两者必须都开启才能生效。
  227. // menus: ['shareAppMessage', 'shareTimeline'],
  228. menus: ['shareAppMessage'],
  229. complete: () => {}
  230. });
  231. // 主动分享
  232. wx.shareAppMessage({
  233. title: title,
  234. imageUrl: imageUrl
  235. });
  236. }
  237. //------------------------------ 快手相关 ------------------------------//
  238. // 快手分享
  239. public static ksShare(title: string, imageUrl: string) {
  240. // console.log('ks分享=',title, 'imageUrl=',imageUrl)
  241. ks.shareAppMessage({
  242. success:(res)=>{ console.log('ks success=',res) },
  243. fail:(err)=>{ console.log('ks fail=',err) }
  244. });
  245. }
  246. //------------------------------ 抖音相关 ------------------------------//
  247. // 抖音获取系统信息
  248. public static ttGetSystemInfo(callback) {
  249. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  250. tt.getSystemInfo({
  251. success:(res) => {
  252. // console.log('tt.getSystemInfo=',res)
  253. // appName: "Douyin" appName: "douyin_lite"
  254. SdkUtil.tt_systemInfo = res
  255. callback()
  256. }
  257. })
  258. }
  259. }
  260. // 抖音添加快捷键(目前仅支持:抖音(Douyin) 和 抖音极速版(douyin_lite))
  261. public static ttAddShortcut(onSuccess:Function = null, onFail:Function = null) {
  262. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  263. if(SdkUtil.tt_systemInfo.appName=='Douyin'||SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  264. if(SdkUtil.tt_systemInfo.brand=='Apple') {
  265. tt.addShortcut({
  266. success() {
  267. console.log("添加桌面成功");
  268. onSuccess && onSuccess(null)
  269. },
  270. fail(err) {
  271. console.log("添加桌面失败", err.errMsg);
  272. onFail && onFail(err)
  273. },
  274. });
  275. } else if(SdkUtil.tt_systemInfo.brand=='Android') {
  276. // 检测只支持安卓
  277. tt.checkShortcut({
  278. success(res) {
  279. console.log("检查快捷方式", res.status);
  280. if(res.status.exist==false||res.status.needUpdate==true) {
  281. tt.addShortcut({
  282. success() {
  283. console.log("添加桌面成功");
  284. onSuccess && onSuccess(null)
  285. },
  286. fail(err) {
  287. console.log("添加桌面失败", err.errMsg);
  288. onFail && onFail(err)
  289. },
  290. });
  291. }
  292. },
  293. fail(err) {
  294. console.log("检查快捷方式失败", err.errMsg);
  295. onFail && onFail(err)
  296. },
  297. });
  298. }
  299. }
  300. }
  301. }
  302. // 抖音注册信息
  303. public static ttRegisterInfo() {
  304. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  305. tt.onShow((res) => {
  306. console.log('tt.onShow =', res)
  307. // console.log('res=',res)
  308. if(res.scene == '021036' || res.scene == '101036') {
  309. SdkUtil.tt_isToEnterFromSidebar = true
  310. }
  311. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  312. SdkUtil.tt_isToEnterFromSidebar = true
  313. }
  314. });
  315. tt.onHide(()=>{
  316. console.log('tt.onHide')
  317. if(SdkUtil.isLookAd==false) {
  318. statisticsManager.uploadRecordUserLevel(false)
  319. }
  320. })
  321. tt.checkScene({
  322. scene: "sidebar",
  323. success: (res) => {
  324. console.log("check scene success: ", res.isExist);
  325. if(res.isExist != undefined || res.isExist != null) {
  326. SdkUtil.tt_isSupportSidebar = res.isExist
  327. }
  328. },
  329. fail: (res) => {
  330. console.log("check scene fail:", res);
  331. }
  332. });
  333. let options = tt.getLaunchOptionsSync()
  334. console.log('getLaunchOptionsSync=', options)
  335. if(options.scene == '021036' || options.scene == '101036') {
  336. SdkUtil.tt_isToEnterFromSidebar = true
  337. }
  338. }
  339. }
  340. // 抖音检测是否显示奖励
  341. public static ttCheckSceneShowRewards():boolean {
  342. return SdkUtil.tt_isSupportSidebar
  343. }
  344. // 抖音检测是否从侧边栏进入
  345. public static ttCheckToEnterFromSidebar():boolean {
  346. return SdkUtil.tt_isToEnterFromSidebar
  347. }
  348. // 抖音导航到侧边栏场景
  349. public static ttNavToSidebarScene() {
  350. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  351. tt.navigateToScene({
  352. scene: "sidebar",
  353. success: (res) => {
  354. // console.log("navigate to scene success");
  355. },
  356. fail: (res) => {
  357. // console.log("navigate to scene fail: ", res);
  358. },
  359. });
  360. }
  361. }
  362. // 抖音开启屏幕录制
  363. public static ttStartScreenRecording() {
  364. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  365. return
  366. }
  367. if(this.tt_systemInfo.platform == 'devtools') {
  368. console.log('抖音模拟器')
  369. return
  370. }
  371. if(this.tt_isRecording==true) {
  372. this.ttStopScreenRecording()
  373. }
  374. if(!this.tt_gameRecorder) {
  375. this.tt_gameRecorder = tt.getGameRecorderManager()
  376. }
  377. this.tt_gameRecorder.start({duration: this.tt_totalRecord})
  378. this.tt_gameRecorder.onStart(()=> {
  379. this.tt_isRecording = true
  380. // console.log('GameRecorder onStart onStart onStart')
  381. })
  382. this.tt_gameRecorder.onStop((res)=> {
  383. // console.log('GameRecorder onStop onStop onStop=',res)
  384. this.tt_isRecording = false
  385. this.tt_recordVideoPath = res.videoPath
  386. })
  387. this.tt_gameRecorder.onError((e)=> {
  388. console.log('ttGameRecord error:',e)
  389. })
  390. }
  391. // 抖音关闭屏幕录制
  392. public static ttStopScreenRecording(isClearVideoPath:boolean = false) {
  393. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  394. return
  395. }
  396. if(this.tt_gameRecorder == null) {
  397. return
  398. }
  399. if(isClearVideoPath) {
  400. this.tt_recordVideoPath = "";
  401. }
  402. this.tt_gameRecorder.stop()
  403. }
  404. // 抖音获取屏幕录制视频文件
  405. public static ttGetScreenRecordingVideoPath():string {
  406. return this.tt_recordVideoPath;
  407. }
  408. // 抖音分享屏幕录制视频
  409. private static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
  410. tt.shareAppMessage({
  411. title: title,
  412. templateId: config.TT_SHARE_TEMPLATEID,
  413. channel: "video",
  414. extra: {
  415. videoTopics: [config.gameName],
  416. hashtag_list: ['小游戏','小程序'],
  417. videoPath: videoPath,
  418. withVideoId: true,
  419. },
  420. success: (res) => {
  421. console.log('抖音分享屏幕录制视频,成功=',res)
  422. onSuccess && onSuccess();
  423. },
  424. fail: (e) => {
  425. // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
  426. console.log('抖音分享屏幕录制视频,失败=',e)
  427. onFail && onFail()
  428. }
  429. })
  430. }
  431. }