sdkUtil.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. call_back()
  83. } else if(sys.platform == sys.Platform.WECHAT_GAME) {
  84. title = title + '好刺激、好好玩'
  85. }else{
  86. call_back()
  87. }
  88. }
  89. // public static shareGame(title: string, imageUrl: string, videoPath: string) {
  90. // if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  91. // if(videoPath!="") {
  92. // // this.ttShareScreenRecordVideo(config.gameName,videoPath)
  93. // }
  94. // } else if(sys.platform == sys.Platform.WECHAT_GAME) {
  95. // title = title + '好刺激、好好玩'
  96. // // this.wxShare(title, imageUrl)
  97. // }
  98. // }
  99. // 获取广告id
  100. public static getAdId(ad_type = config.ADS_TYPE.UNKNOWN):string {
  101. let ad_id = ""
  102. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  103. if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
  104. ad_id = config.ADS_CONFIG.GAME_RELIFE_VIDEO
  105. } else if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
  106. ad_id = config.ADS_CONFIG.GAME_INFINITE_DEGREE_VIDEO
  107. }
  108. } else if (sys.platform == sys.Platform.WECHAT_GAME) {
  109. // if(SdkUtil.KS_GAME) {
  110. // } else {
  111. // }
  112. }
  113. return ad_id
  114. }
  115. // 显示激励视频广告
  116. public static showVideoAd(_adUnitId: string, call_back) {
  117. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  118. uiManager.Instance().showLoading()
  119. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  120. } else {
  121. call_back({"isEnded":true})
  122. return
  123. }
  124. if(SdkUtil.videoAd==null){
  125. uiManager.Instance().hideLoading()
  126. return
  127. }
  128. SdkUtil.videoAd.onLoad(() => {
  129. SdkUtil.isLookAd = true
  130. SdkUtil.videoAd.show();
  131. console.log("广告加载完成");
  132. });
  133. SdkUtil.videoAd.onClose((res) => {
  134. call_back(res)
  135. SdkUtil.isLookAd = false
  136. SdkUtil.videoAd.destroy()
  137. uiManager.Instance().hideLoading()
  138. });
  139. SdkUtil.videoAd.onError((res) => {
  140. let errorString = res.errCode + '-' + res.errMsg
  141. call_back({isEnded:false,errorString:errorString})
  142. SdkUtil.isLookAd = false
  143. SdkUtil.videoAd.destroy()
  144. uiManager.Instance().hideLoading()
  145. });
  146. SdkUtil.videoAd.load()
  147. }
  148. //------------------------------ 抖音相关 ------------------------------//
  149. // 抖音获取系统信息
  150. public static ttGetSystemInfo(callback) {
  151. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  152. tt.getSystemInfo({
  153. success:(res) => {
  154. // console.log('tt.getSystemInfo=',res)
  155. // appName: "Douyin" appName: "douyin_lite"
  156. SdkUtil.tt_systemInfo = res
  157. callback()
  158. }
  159. })
  160. }
  161. }
  162. // 抖音添加快捷键(目前仅支持:抖音(Douyin) 和 抖音极速版(douyin_lite))
  163. public static ttAddShortcut(onSuccess:Function = null, onFail:Function = null) {
  164. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  165. if(SdkUtil.tt_systemInfo.appName=='Douyin'||SdkUtil.tt_systemInfo.appName=='douyin_lite') {
  166. if(SdkUtil.tt_systemInfo.brand=='Apple') {
  167. tt.addShortcut({
  168. success() {
  169. console.log("添加桌面成功");
  170. onSuccess && onSuccess(null)
  171. },
  172. fail(err) {
  173. console.log("添加桌面失败", err.errMsg);
  174. onFail && onFail(err)
  175. },
  176. });
  177. } else if(SdkUtil.tt_systemInfo.brand=='Android') {
  178. // 检测只支持安卓
  179. tt.checkShortcut({
  180. success(res) {
  181. console.log("检查快捷方式", res.status);
  182. if(res.status.exist==false||res.status.needUpdate==true) {
  183. tt.addShortcut({
  184. success() {
  185. console.log("添加桌面成功");
  186. onSuccess && onSuccess(null)
  187. },
  188. fail(err) {
  189. console.log("添加桌面失败", err.errMsg);
  190. onFail && onFail(err)
  191. },
  192. });
  193. }
  194. },
  195. fail(err) {
  196. console.log("检查快捷方式失败", err.errMsg);
  197. onFail && onFail(err)
  198. },
  199. });
  200. }
  201. }
  202. }
  203. }
  204. // 抖音注册信息
  205. public static ttRegisterInfo() {
  206. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  207. tt.onShow((res) => {
  208. console.log('tt.onShow =', res)
  209. // console.log('res=',res)
  210. if(res.scene == '021036' || res.scene == '101036') {
  211. SdkUtil.tt_isToEnterFromSidebar = true
  212. }
  213. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  214. SdkUtil.tt_isToEnterFromSidebar = true
  215. }
  216. });
  217. tt.onHide(()=>{
  218. console.log('tt.onHide')
  219. if(SdkUtil.isLookAd==false) {
  220. // statisticsManager.uploadRecordUserLevel(false)
  221. }
  222. })
  223. tt.checkScene({
  224. scene: "sidebar",
  225. success: (res) => {
  226. console.log("check scene success: ", res.isExist);
  227. if(res.isExist != undefined || res.isExist != null) {
  228. SdkUtil.tt_isSupportSidebar = res.isExist
  229. }
  230. },
  231. fail: (res) => {
  232. console.log("check scene fail:", res);
  233. }
  234. });
  235. let options = tt.getLaunchOptionsSync()
  236. console.log('getLaunchOptionsSync=', options)
  237. if(options.scene == '021036' || options.scene == '101036') {
  238. SdkUtil.tt_isToEnterFromSidebar = true
  239. }
  240. }
  241. }
  242. // 抖音检测是否显示奖励
  243. public static ttCheckSceneShowRewards():boolean {
  244. return SdkUtil.tt_isSupportSidebar
  245. }
  246. // 抖音检测是否从侧边栏进入
  247. public static ttCheckToEnterFromSidebar():boolean {
  248. return SdkUtil.tt_isToEnterFromSidebar
  249. }
  250. // 抖音导航到侧边栏场景
  251. public static ttNavToSidebarScene() {
  252. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  253. tt.navigateToScene({
  254. scene: "sidebar",
  255. success: (res) => {
  256. // console.log("navigate to scene success");
  257. },
  258. fail: (res) => {
  259. // console.log("navigate to scene fail: ", res);
  260. },
  261. });
  262. }
  263. }
  264. // 抖音开启屏幕录制
  265. public static ttStartScreenRecording() {
  266. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  267. return
  268. }
  269. if(this.tt_systemInfo.platform == 'devtools') {
  270. console.log('抖音模拟器')
  271. return
  272. }
  273. if(this.tt_isRecording==true) {
  274. this.ttStopScreenRecording()
  275. }
  276. if(!this.tt_gameRecorder) {
  277. this.tt_gameRecorder = tt.getGameRecorderManager()
  278. }
  279. this.tt_gameRecorder.start({duration: this.tt_totalRecord})
  280. this.tt_gameRecorder.onStart(()=> {
  281. this.tt_isRecording = true
  282. // console.log('GameRecorder onStart onStart onStart')
  283. })
  284. this.tt_gameRecorder.onStop((res)=> {
  285. // console.log('GameRecorder onStop onStop onStop=',res)
  286. this.tt_isRecording = false
  287. this.tt_recordVideoPath = res.videoPath
  288. })
  289. this.tt_gameRecorder.onError((e)=> {
  290. console.log('ttGameRecord error:',e)
  291. })
  292. }
  293. // 抖音关闭屏幕录制
  294. public static ttStopScreenRecording(isClearVideoPath:boolean = false) {
  295. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  296. return
  297. }
  298. if(this.tt_gameRecorder == null) {
  299. return
  300. }
  301. if(isClearVideoPath) {
  302. this.tt_recordVideoPath = "";
  303. }
  304. this.tt_gameRecorder.stop()
  305. }
  306. // 抖音获取屏幕录制视频文件
  307. public static ttGetScreenRecordingVideoPath():string {
  308. return this.tt_recordVideoPath;
  309. }
  310. // 抖音分享屏幕录制视频
  311. private static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
  312. // tt.shareAppMessage({
  313. // title: title,
  314. // templateId: config.TT_SHARE_TEMPLATEID,
  315. // channel: "video",
  316. // extra: {
  317. // videoTopics: [config.gameName],
  318. // hashtag_list: ['小游戏','小程序'],
  319. // videoPath: videoPath,
  320. // withVideoId: true,
  321. // },
  322. // success: (res) => {
  323. // console.log('抖音分享屏幕录制视频,成功=',res)
  324. // onSuccess && onSuccess();
  325. // },
  326. // fail: (e) => {
  327. // // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
  328. // console.log('抖音分享屏幕录制视频,失败=',e)
  329. // onFail && onFail()
  330. // }
  331. // })
  332. }
  333. //------------------------------ 三方分别处理相关 ------------------------------//
  334. public static login(call){
  335. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  336. tt.login({
  337. force: true,
  338. success(res) {
  339. console.log(`login 调用成功${res.code} ${res.anonymousCode}`);
  340. call({"code":res.code,"anonymousCode":res.anonymousCode})
  341. },
  342. fail(res) {
  343. console.log(`login 调用失败`);
  344. call(null)
  345. },
  346. });
  347. }else{
  348. call(null)
  349. }
  350. }
  351. public static getUserInfo(call){
  352. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  353. tt.getUserInfo({
  354. // withCredentials: true,
  355. // withRealNameAuthenticationInfo: true,
  356. success(res) {
  357. console.log(`getUserInfo 调用成功`, res.userInfo);
  358. call(res.userInfo)
  359. },
  360. fail(res) {
  361. console.log(`getUserInfo 调用失败`, res.errMsg);
  362. },
  363. });
  364. }else{
  365. call(null)
  366. }
  367. }
  368. public static vibrateShort(){
  369. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  370. tt.vibrateShort({
  371. success(res) {
  372. // console.log(`${res}`);
  373. },
  374. fail(res) {
  375. // console.log(`vibrateShort调用失败`);
  376. },
  377. });
  378. }
  379. }
  380. }