sdkUtil.ts 15 KB

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