sdkUtil.ts 17 KB

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