sdkUtil.ts 16 KB

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