sdkUtil.ts 16 KB

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