sdkUtil.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. import { _decorator, error, SpriteFrame, sys } from "cc";
  2. import { config } from "./config";
  3. import { uiManager } from "./manager/uiManager";
  4. //管理广告、分享、SDK相关内容的组件
  5. export class SdkUtil {
  6. public static platform: string = 'cocos'; //平台
  7. public static imgAd: SpriteFrame = null!;
  8. public static imgShare: SpriteFrame = null!;
  9. public static isDebugMode: boolean = false;
  10. public static onlineInterval: number = -1;
  11. public static isEnableVibrate: boolean = true;
  12. public static isCheckOffline: boolean = false; //登录后会检查是否展示登录界面,而且只检查一次
  13. public static isWatchVideoAd: boolean = false;//是否正在播放广告
  14. public static isEnableMoving: boolean = false;//是否允许屏幕上下移动
  15. public static isEnableZoom: boolean = false;//是否允许屏幕缩放
  16. public static arrLockDiary = [];//未解锁日记
  17. public static vibrateInterval: number = 100;//两次震动之间的间隔,AppActivity里面的震动间隔也是100
  18. public static vibratePreTime: number = 0;//上次震动时间
  19. public static videoAd:any =null;
  20. public static isLookAd:boolean = false; //是否在看广告
  21. public static tt_systemInfo: any = null; //抖音_系统信息
  22. private static tt_isSupportSidebar:boolean = false; //抖音_是否支持侧边栏
  23. private static tt_isToEnterFromSidebar:boolean = false; //抖音_是否从侧边栏进入
  24. private static tt_gameRecorder:any = null; //抖音游戏录制
  25. private static tt_recordVideoPath:string = ''; //抖音录制视频路径
  26. private static tt_totalRecord:number = 300; //抖音总录制时间
  27. private static tt_isRecording:boolean = false; //抖音是否录制中
  28. //------------------------------ 公共 ------------------------------//
  29. public static init() {
  30. this.ttGetSystemInfo(()=> {
  31. this.ttRegisterInfo()
  32. })
  33. }
  34. // 苹果手机是否有灵动岛
  35. public static iPhoneIsLingdongdao():boolean {
  36. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  37. if(SdkUtil.tt_systemInfo !=null) {
  38. if(SdkUtil.tt_systemInfo.brand=='Apple'||SdkUtil.tt_systemInfo.brand=='devtools')
  39. if(SdkUtil.tt_systemInfo.model=='iPhone 14'||
  40. SdkUtil.tt_systemInfo.model=='iPhone 14 Pro'||
  41. SdkUtil.tt_systemInfo.model=='iPhone 14 Pro Max'||
  42. SdkUtil.tt_systemInfo.model=='iPhone 15'||
  43. SdkUtil.tt_systemInfo.model=='iPhone 15 Pro'||
  44. SdkUtil.tt_systemInfo.model=='iPhone 15 Pro Max') {
  45. return true
  46. }
  47. }
  48. }
  49. return false
  50. }
  51. // 自定义事件统计
  52. public static customEventStatistics(eventType: string, objParams?: any) {
  53. eventType = eventType.toString();
  54. if (!objParams) { objParams = {}; }
  55. // console.log({'eventType': eventType},{'objParams': objParams});
  56. if (this.platform === 'wx') {
  57. //@ts-ignore
  58. if (window['wx'] && window['wx']['aldSendEvent']) {
  59. //@ts-ignore
  60. window.wx['aldSendEvent'](eventType, objParams);
  61. }
  62. }
  63. //@ts-ignore
  64. if (this.platform === 'cocos' && window.cocosAnalytics && window.cocosAnalytics.isInited()) {
  65. console.log("###统计", eventType, objParams);
  66. //@ts-ignore
  67. window.cocosAnalytics.CACustomEvent.onStarted(eventType, objParams);
  68. }
  69. }
  70. // 检测显示添加桌面
  71. public static checkIsShowAddDesktop():boolean {
  72. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  73. if(SdkUtil.tt_systemInfo.appName=='Douyin' || SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  74. return true
  75. }
  76. }
  77. return false
  78. }
  79. // 分享游戏
  80. public static shareGame(title: string, call_back) {
  81. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  82. tt.shareAppMessage({
  83. title: title,
  84. desc: "测试描述",
  85. success() { console.log("分享成功") },
  86. fail(e) { console.log("分享失败",e) },
  87. });
  88. call_back()
  89. } else if(sys.platform == sys.Platform.WECHAT_GAME) {
  90. title = title + '好刺激、好好玩'
  91. }else{
  92. call_back()
  93. }
  94. }
  95. // public static shareGame(title: string, imageUrl: string, videoPath: string) {
  96. // if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  97. // if(videoPath!="") {
  98. // // this.ttShareScreenRecordVideo(config.gameName,videoPath)
  99. // }
  100. // } else if(sys.platform == sys.Platform.WECHAT_GAME) {
  101. // title = title + '好刺激、好好玩'
  102. // // this.wxShare(title, imageUrl)
  103. // }
  104. // }
  105. // 获取广告id
  106. public static getAdId(ad_type = config.ADS_TYPE.UNKNOWN):string {
  107. let ad_id = ""
  108. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  109. if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
  110. ad_id = config.ADS_CONFIG.GAME_RELIFE_VIDEO
  111. } else if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
  112. ad_id = config.ADS_CONFIG.GAME_INFINITE_DEGREE_VIDEO
  113. }
  114. } else if (sys.platform == sys.Platform.WECHAT_GAME) {
  115. // if(SdkUtil.KS_GAME) {
  116. // } else {
  117. // }
  118. }
  119. return ad_id
  120. }
  121. // 显示激励视频广告
  122. public static showVideoAd(_adUnitId: string, call_back) {
  123. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  124. uiManager.Instance().showLoading()
  125. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  126. } else {
  127. call_back({"isEnded":true})
  128. return
  129. }
  130. if(SdkUtil.videoAd==null){
  131. uiManager.Instance().hideLoading()
  132. return
  133. }
  134. SdkUtil.videoAd.onLoad(() => {
  135. SdkUtil.isLookAd = true
  136. SdkUtil.videoAd.show();
  137. console.log("广告加载完成");
  138. });
  139. SdkUtil.videoAd.onClose((res) => {
  140. call_back(res)
  141. SdkUtil.isLookAd = false
  142. SdkUtil.videoAd.destroy()
  143. uiManager.Instance().hideLoading()
  144. });
  145. SdkUtil.videoAd.onError((res) => {
  146. let errorString = res.errCode + '-' + res.errMsg
  147. call_back({isEnded:false,errorString:errorString})
  148. SdkUtil.isLookAd = false
  149. SdkUtil.videoAd.destroy()
  150. uiManager.Instance().hideLoading()
  151. });
  152. SdkUtil.videoAd.load()
  153. }
  154. //------------------------------ 抖音相关 ------------------------------//
  155. // 抖音获取系统信息
  156. public static ttGetSystemInfo(callback) {
  157. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  158. tt.getSystemInfo({
  159. success:(res) => {
  160. // console.log('tt.getSystemInfo=',res)
  161. // appName: "Douyin" appName: "douyin_lite"
  162. SdkUtil.tt_systemInfo = res
  163. callback()
  164. }
  165. })
  166. }
  167. }
  168. // 抖音添加快捷键(目前仅支持:抖音(Douyin) 和 抖音极速版(douyin_lite))
  169. public static ttAddShortcut(onSuccess:Function = null, onFail:Function = null) {
  170. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  171. if(SdkUtil.tt_systemInfo.appName=='Douyin'||SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  172. if(SdkUtil.tt_systemInfo.brand=='Apple') {
  173. tt.addShortcut({
  174. success() {
  175. console.log("添加桌面成功");
  176. onSuccess && onSuccess(null)
  177. },
  178. fail(err) {
  179. console.log("添加桌面失败", err.errMsg);
  180. onFail && onFail(err)
  181. },
  182. });
  183. } else if(SdkUtil.tt_systemInfo.brand=='Android') {
  184. // 检测只支持安卓
  185. tt.checkShortcut({
  186. success(res) {
  187. console.log("检查快捷方式", res.status);
  188. if(res.status.exist==false||res.status.needUpdate==true) {
  189. tt.addShortcut({
  190. success() {
  191. console.log("添加桌面成功");
  192. onSuccess && onSuccess(null)
  193. },
  194. fail(err) {
  195. console.log("添加桌面失败", err.errMsg);
  196. onFail && onFail(err)
  197. },
  198. });
  199. }
  200. },
  201. fail(err) {
  202. console.log("检查快捷方式失败", err.errMsg);
  203. onFail && onFail(err)
  204. },
  205. });
  206. }
  207. }
  208. }
  209. }
  210. // 抖音注册信息
  211. public static ttRegisterInfo() {
  212. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  213. tt.onShow((res) => {
  214. console.log('tt.onShow =', res)
  215. // console.log('res=',res)
  216. if(res.scene == '021036' || res.scene == '101036') {
  217. SdkUtil.tt_isToEnterFromSidebar = true
  218. }
  219. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  220. SdkUtil.tt_isToEnterFromSidebar = true
  221. }
  222. });
  223. tt.onHide(()=>{
  224. console.log('tt.onHide')
  225. if(SdkUtil.isLookAd==false) {
  226. // statisticsManager.uploadRecordUserLevel(false)
  227. }
  228. })
  229. tt.checkScene({
  230. scene: "sidebar",
  231. success: (res) => {
  232. console.log("check scene success: ", res.isExist);
  233. if(res.isExist != undefined || res.isExist != null) {
  234. SdkUtil.tt_isSupportSidebar = res.isExist
  235. }
  236. },
  237. fail: (res) => {
  238. console.log("check scene fail:", res);
  239. }
  240. });
  241. let options = tt.getLaunchOptionsSync()
  242. console.log('getLaunchOptionsSync=', options)
  243. if(options.scene == '021036' || options.scene == '101036') {
  244. SdkUtil.tt_isToEnterFromSidebar = true
  245. }
  246. }
  247. }
  248. // 抖音检测是否显示奖励
  249. public static ttCheckSceneShowRewards():boolean {
  250. return SdkUtil.tt_isSupportSidebar
  251. }
  252. // 抖音检测是否从侧边栏进入
  253. public static ttCheckToEnterFromSidebar():boolean {
  254. return SdkUtil.tt_isToEnterFromSidebar
  255. }
  256. // 抖音导航到侧边栏场景
  257. public static ttNavToSidebarScene() {
  258. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  259. tt.navigateToScene({
  260. scene: "sidebar",
  261. success: (res) => {
  262. // console.log("navigate to scene success");
  263. },
  264. fail: (res) => {
  265. // console.log("navigate to scene fail: ", res);
  266. },
  267. });
  268. }
  269. }
  270. // 抖音开启屏幕录制
  271. public static ttStartScreenRecording() {
  272. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  273. return
  274. }
  275. if(this.tt_systemInfo.platform == 'devtools') {
  276. console.log('抖音模拟器')
  277. return
  278. }
  279. if(this.tt_isRecording==true) {
  280. this.ttStopScreenRecording()
  281. }
  282. if(!this.tt_gameRecorder) {
  283. this.tt_gameRecorder = tt.getGameRecorderManager()
  284. }
  285. this.tt_gameRecorder.start({duration: this.tt_totalRecord})
  286. this.tt_gameRecorder.onStart(()=> {
  287. this.tt_isRecording = true
  288. // console.log('GameRecorder onStart onStart onStart')
  289. })
  290. this.tt_gameRecorder.onStop((res)=> {
  291. // console.log('GameRecorder onStop onStop onStop=',res)
  292. this.tt_isRecording = false
  293. this.tt_recordVideoPath = res.videoPath
  294. })
  295. this.tt_gameRecorder.onError((e)=> {
  296. console.log('ttGameRecord error:',e)
  297. })
  298. }
  299. // 抖音关闭屏幕录制
  300. public static ttStopScreenRecording(isClearVideoPath:boolean = false) {
  301. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  302. return
  303. }
  304. if(this.tt_gameRecorder == null) {
  305. return
  306. }
  307. if(isClearVideoPath) {
  308. this.tt_recordVideoPath = "";
  309. }
  310. this.tt_gameRecorder.stop()
  311. }
  312. // 抖音获取屏幕录制视频文件
  313. public static ttGetScreenRecordingVideoPath():string {
  314. return this.tt_recordVideoPath;
  315. }
  316. // 抖音分享屏幕录制视频
  317. private static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
  318. // tt.shareAppMessage({
  319. // title: title,
  320. // templateId: config.TT_SHARE_TEMPLATEID,
  321. // channel: "video",
  322. // extra: {
  323. // videoTopics: [config.gameName],
  324. // hashtag_list: ['小游戏','小程序'],
  325. // videoPath: videoPath,
  326. // withVideoId: true,
  327. // },
  328. // success: (res) => {
  329. // console.log('抖音分享屏幕录制视频,成功=',res)
  330. // onSuccess && onSuccess();
  331. // },
  332. // fail: (e) => {
  333. // // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
  334. // console.log('抖音分享屏幕录制视频,失败=',e)
  335. // onFail && onFail()
  336. // }
  337. // })
  338. }
  339. //------------------------------ 三方分别处理相关 ------------------------------//
  340. public static login(call){
  341. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  342. tt.login({
  343. force: true,
  344. success(res) {
  345. console.log(`login 调用成功${res.code} ${res.anonymousCode}`);
  346. call({"code":res.code,"anonymousCode":res.anonymousCode})
  347. },
  348. fail(res) {
  349. console.log(`login 调用失败`);
  350. call(null)
  351. },
  352. });
  353. }else{
  354. call(null)
  355. }
  356. }
  357. public static getUserInfo(call){
  358. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  359. tt.getUserInfo({
  360. // withCredentials: true,
  361. // withRealNameAuthenticationInfo: true,
  362. success(res) {
  363. console.log(`getUserInfo 调用成功`, res.userInfo);
  364. call(res.userInfo)
  365. },
  366. fail(res) {
  367. console.log(`getUserInfo 调用失败`, res.errMsg);
  368. },
  369. });
  370. }else{
  371. call(null)
  372. }
  373. }
  374. public static vibrateShort(){
  375. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  376. tt.vibrateShort({
  377. success(res) {
  378. // console.log(`${res}`);
  379. },
  380. fail(res) {
  381. // console.log(`vibrateShort调用失败`);
  382. },
  383. });
  384. }
  385. }
  386. }