sdkUtil.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. import { _decorator, SpriteFrame, sys } from "cc";
  2. import { gameManager } from "./run/gameManager";
  3. import { config } from "./config";
  4. import { statisticsManager } from "./statisticsManager";
  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. public static init() {
  30. this.ttGetSystemInfo(()=> {
  31. this.ttRegisterSidebar()
  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. * 自定义事件统计
  52. *
  53. * @param {string} eventType
  54. * @param {object} objParams
  55. */
  56. public static customEventStatistics(eventType: string, objParams?: any) {
  57. eventType = eventType.toString();
  58. if (!objParams) {
  59. objParams = {};
  60. }
  61. // console.log({'eventType': eventType},{'objParams': objParams});
  62. if (this.platform === 'wx') {
  63. //@ts-ignore
  64. if (window['wx'] && window['wx']['aldSendEvent']) {
  65. //@ts-ignore
  66. window.wx['aldSendEvent'](eventType, objParams);
  67. }
  68. }
  69. //@ts-ignore
  70. if (this.platform === 'cocos' && window.cocosAnalytics && window.cocosAnalytics.isInited()) {
  71. console.log("###统计", eventType, objParams);
  72. //@ts-ignore
  73. window.cocosAnalytics.CACustomEvent.onStarted(eventType, objParams);
  74. }
  75. }
  76. /**
  77. * 抖音/微信分享
  78. *
  79. * @static
  80. * @param {string} title
  81. * @param {string} imageUrl
  82. * @param {string} videoPath
  83. * @returns
  84. * @memberof SdkUtil
  85. */
  86. public static shareGame(title: string, imageUrl: string, videoPath: string) {
  87. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  88. if(videoPath!="") {
  89. this.ttShareScreenRecordVideo(config.gameName,videoPath)
  90. }
  91. } else if(sys.platform == sys.Platform.WECHAT_GAME) {
  92. title = title + '好刺激、好好玩'
  93. this.wxShare(title, imageUrl)
  94. }
  95. }
  96. /**
  97. * 微信分享
  98. *
  99. * @static
  100. * @param {string} title
  101. * @param {string} imageUrl
  102. * @returns
  103. * @memberof SdkUtil
  104. */
  105. public static wxShare(title: string, imageUrl: string) {
  106. // console.log('微信分享=',title, 'imageUrl=',imageUrl)
  107. wx.showShareMenu({
  108. withShareTicket: true,
  109. // shareAppMessage(可以删除):显示分享给好友选项,shareTimeline(可以删除):显示分享至朋友圈选项
  110. // 可以只开启前者。如果要开启后者,则两者必须都开启才能生效。
  111. // menus: ['shareAppMessage', 'shareTimeline'],
  112. menus: ['shareAppMessage'],
  113. complete: () => {}
  114. });
  115. // 主动分享
  116. wx.shareAppMessage({
  117. title: title,
  118. imageUrl: imageUrl
  119. });
  120. // 被动分享
  121. // wx.onShareAppMessage( () => {
  122. // return {
  123. // // 标题,不传则默认使用小游戏的名称
  124. // title: title,
  125. // // 转发链接所显示的图片,比例5:4,资源可以是本地或远程。不传则默认使用游戏截图。
  126. // imageUrl: imageUrl,
  127. // }
  128. // });
  129. //@ts-ignore
  130. // if (!window.wx) {
  131. // return;
  132. // }
  133. //
  134. //@ts-ignore
  135. // wx.showShareMenu({
  136. // withShareTicket: true,
  137. // complete: () => {
  138. // console.log('')
  139. // }
  140. // }
  141. //
  142. //@ts-ignore
  143. // if (wx.aldOnShareAppMessage) {
  144. // //@ts-ignore 被动分享
  145. // wx.aldOnShareAppMessage(function () {
  146. // // 用户点击了“转发”按钮
  147. // return {
  148. // title: title,
  149. // imageUrl: imageUrl,
  150. // };
  151. // });
  152. // } else {
  153. // //@ts-ignore
  154. // wx.onShareAppMessage(function () {
  155. // // 用户点击了“转发”按钮
  156. // return {
  157. // title: title,
  158. // imageUrl: imageUrl,
  159. // };
  160. // });
  161. // }
  162. }
  163. public static getAdId(ad_type = config.AD_TYPE.UNKNOWN):string {
  164. let ad_id = ""
  165. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  166. if(ad_type == config.AD_TYPE.RE_LIFE) {
  167. ad_id = config.TT_REWARD.RE_LIFE
  168. } else if (ad_type == config.AD_TYPE.ANSWER) {
  169. ad_id = config.TT_REWARD.ANSWER
  170. } else if (ad_type == config.AD_TYPE.LOOK_TIPS) {
  171. ad_id = config.TT_REWARD.LOOK_TIPS
  172. } else if (ad_type == config.AD_TYPE.UN_LOCK_24) {
  173. ad_id = config.TT_REWARD.UN_LOCK_24
  174. } else if (ad_type == config.AD_TYPE.UN_LOCK) {
  175. ad_id = config.TT_REWARD.UN_LOCK
  176. } else if (ad_type == config.AD_TYPE.ADD_TIME) {
  177. ad_id = config.TT_REWARD.ADD_TIME
  178. }
  179. } else if (sys.platform == sys.Platform.WECHAT_GAME) {
  180. if(ad_type == config.AD_TYPE.RE_LIFE) {
  181. ad_id = config.WX_REWARD.RE_LIFE
  182. } else if (ad_type == config.AD_TYPE.ANSWER) {
  183. ad_id = config.WX_REWARD.ANSWER
  184. } else if (ad_type == config.AD_TYPE.LOOK_TIPS) {
  185. ad_id = config.WX_REWARD.LOOK_TIPS
  186. } else if (ad_type == config.AD_TYPE.UN_LOCK_24) {
  187. ad_id = config.WX_REWARD.UN_LOCK_24
  188. } else if (ad_type == config.AD_TYPE.UN_LOCK) {
  189. ad_id = config.WX_REWARD.UN_LOCK
  190. } else if (ad_type == config.AD_TYPE.ADD_TIME) {
  191. ad_id = config.WX_REWARD.ADD_TIME
  192. }
  193. }
  194. return ad_id
  195. }
  196. /**
  197. * 抖音/微信激励视频
  198. *
  199. * @static
  200. * @param {string} _adUnitId
  201. * @param {Function} call_back
  202. * @returns
  203. * @memberof SdkUtil
  204. */
  205. public static showVideoAd(_adUnitId: string, call_back) {
  206. if(gameManager.isFreeAds()) {
  207. call_back({"isEnded":true})
  208. return
  209. }
  210. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME||sys.platform==sys.Platform.WECHAT_GAME){
  211. gameManager.Singleton.showLoadingLevel()
  212. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  213. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  214. } else if(sys.platform==sys.Platform.WECHAT_GAME) {
  215. gameManager.Singleton.hideLoadingLevel()
  216. call_back({"isEnded":true})
  217. return
  218. // SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
  219. }
  220. if(SdkUtil.videoAd==null){
  221. gameManager.Singleton.hideLoadingLevel()
  222. return
  223. }
  224. SdkUtil.videoAd.onLoad(() => {
  225. SdkUtil.isLookAd = true
  226. SdkUtil.videoAd.show();
  227. console.log("广告加载完成");
  228. });
  229. SdkUtil.videoAd.onClose((res) => {
  230. call_back(res)
  231. SdkUtil.isLookAd = false
  232. SdkUtil.videoAd.destroy()
  233. gameManager.Singleton.hideLoadingLevel()
  234. });
  235. SdkUtil.videoAd.onError((res) => {
  236. let errorString = res.errCode + '-' + res.errMsg
  237. call_back({isEnded:false,errorString:errorString})
  238. SdkUtil.isLookAd = false
  239. SdkUtil.videoAd.destroy()
  240. gameManager.Singleton.hideLoadingLevel()
  241. });
  242. SdkUtil.videoAd.load()
  243. }
  244. }
  245. /**
  246. * 抖音获取系统信息
  247. */
  248. public static ttGetSystemInfo(callback) {
  249. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  250. tt.getSystemInfo({
  251. success:(res) => {
  252. // console.log('tt.getSystemInfo=',res)
  253. // appName: "Douyin" appName: "douyin_lite"
  254. SdkUtil.tt_systemInfo = res
  255. callback()
  256. }
  257. })
  258. }
  259. }
  260. /**
  261. * 抖音侧边栏
  262. *
  263. * @static
  264. * @memberof SdkUtil
  265. */
  266. public static ttRegisterSidebar() {
  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. }