sdkUtil.ts 16 KB

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