sdkUtil.ts 16 KB

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