sdkUtil.ts 18 KB

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