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