sdkUtil.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. }
  177. } else if (sys.platform == sys.Platform.WECHAT_GAME) {
  178. if(ad_type == config.AD_TYPE.RE_LIFE) {
  179. ad_id = config.WX_REWARD.RE_LIFE
  180. } else if (ad_type == config.AD_TYPE.ANSWER) {
  181. ad_id = config.WX_REWARD.ANSWER
  182. } else if (ad_type == config.AD_TYPE.LOOK_TIPS) {
  183. ad_id = config.WX_REWARD.LOOK_TIPS
  184. } else if (ad_type == config.AD_TYPE.UN_LOCK_24) {
  185. ad_id = config.WX_REWARD.UN_LOCK_24
  186. } else if (ad_type == config.AD_TYPE.UN_LOCK) {
  187. ad_id = config.WX_REWARD.UN_LOCK
  188. }
  189. }
  190. return ad_id
  191. }
  192. /**
  193. * 抖音/微信激励视频
  194. *
  195. * @static
  196. * @param {string} _adUnitId
  197. * @param {Function} call_back
  198. * @returns
  199. * @memberof SdkUtil
  200. */
  201. public static showVideoAd(_adUnitId: string, call_back) {
  202. if(gameManager.isFreeAds()) {
  203. call_back({"isEnded":true})
  204. return
  205. }
  206. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME||sys.platform==sys.Platform.WECHAT_GAME){
  207. gameManager.Singleton.showLoadingLevel()
  208. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  209. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  210. } else if(sys.platform==sys.Platform.WECHAT_GAME) {
  211. gameManager.Singleton.hideLoadingLevel()
  212. call_back({"isEnded":true})
  213. return
  214. // SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
  215. }
  216. if(SdkUtil.videoAd==null){
  217. gameManager.Singleton.hideLoadingLevel()
  218. return
  219. }
  220. SdkUtil.videoAd.onLoad(() => {
  221. SdkUtil.isLookAd = true
  222. SdkUtil.videoAd.show();
  223. console.log("广告加载完成");
  224. });
  225. SdkUtil.videoAd.onClose((res) => {
  226. call_back(res)
  227. SdkUtil.isLookAd = false
  228. SdkUtil.videoAd.destroy()
  229. gameManager.Singleton.hideLoadingLevel()
  230. });
  231. SdkUtil.videoAd.onError((res) => {
  232. let errorString = res.errCode + '-' + res.errMsg
  233. call_back({isEnded:false,errorString:errorString})
  234. SdkUtil.isLookAd = false
  235. SdkUtil.videoAd.destroy()
  236. gameManager.Singleton.hideLoadingLevel()
  237. });
  238. SdkUtil.videoAd.load()
  239. }
  240. }
  241. /**
  242. * 抖音获取系统信息
  243. */
  244. public static ttGetSystemInfo(callback) {
  245. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  246. tt.getSystemInfo({
  247. success:(res) => {
  248. // console.log('tt.getSystemInfo=',res)
  249. // appName: "Douyin" appName: "douyin_lite"
  250. SdkUtil.tt_systemInfo = res
  251. callback()
  252. }
  253. })
  254. }
  255. }
  256. /**
  257. * 抖音侧边栏
  258. *
  259. * @static
  260. * @memberof SdkUtil
  261. */
  262. public static ttRegisterSidebar() {
  263. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  264. tt.onShow((res) => {
  265. console.log('tt.onShow =', res)
  266. // console.log('res=',res)
  267. if(res.scene == '021036' || res.scene == '101036') {
  268. SdkUtil.tt_isToEnterFromSidebar = true
  269. }
  270. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  271. SdkUtil.tt_isToEnterFromSidebar = true
  272. }
  273. });
  274. tt.onHide(()=>{
  275. console.log('tt.onHide')
  276. if(SdkUtil.isLookAd==false) {
  277. statisticsManager.uploadRecordUserLevel(false)
  278. }
  279. })
  280. tt.checkScene({
  281. scene: "sidebar",
  282. success: (res) => {
  283. console.log("check scene success: ", res.isExist);
  284. if(res.isExist != undefined || res.isExist != null) {
  285. SdkUtil.tt_isSupportSidebar = res.isExist
  286. }
  287. },
  288. fail: (res) => {
  289. console.log("check scene fail:", res);
  290. }
  291. });
  292. let options = tt.getLaunchOptionsSync()
  293. console.log('getLaunchOptionsSync=', options)
  294. if(options.scene == '021036' || options.scene == '101036') {
  295. SdkUtil.tt_isToEnterFromSidebar = true
  296. }
  297. }
  298. }
  299. // 抖音检测是否显示奖励
  300. public static ttCheckSceneShowRewards():boolean {
  301. return SdkUtil.tt_isSupportSidebar
  302. }
  303. // 抖音检测是否从侧边栏进入
  304. public static ttCheckToEnterFromSidebar():boolean {
  305. return SdkUtil.tt_isToEnterFromSidebar
  306. }
  307. // 抖音导航到侧边栏场景
  308. public static ttNavToSidebarScene() {
  309. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  310. tt.navigateToScene({
  311. scene: "sidebar",
  312. success: (res) => {
  313. // console.log("navigate to scene success");
  314. },
  315. fail: (res) => {
  316. // console.log("navigate to scene fail: ", res);
  317. },
  318. });
  319. }
  320. }
  321. // 抖音开启屏幕录制
  322. public static ttStartScreenRecording() {
  323. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  324. return
  325. }
  326. if(this.tt_systemInfo.platform == 'devtools') {
  327. console.log('抖音模拟器')
  328. return
  329. }
  330. if(this.tt_isRecording==true) {
  331. this.ttStopScreenRecording()
  332. }
  333. if(!this.tt_gameRecorder) {
  334. this.tt_gameRecorder = tt.getGameRecorderManager()
  335. }
  336. this.tt_gameRecorder.start({duration: this.tt_totalRecord})
  337. this.tt_gameRecorder.onStart(()=> {
  338. this.tt_isRecording = true
  339. // console.log('GameRecorder onStart onStart onStart')
  340. })
  341. this.tt_gameRecorder.onStop((res)=> {
  342. // console.log('GameRecorder onStop onStop onStop=',res)
  343. this.tt_isRecording = false
  344. this.tt_recordVideoPath = res.videoPath
  345. })
  346. this.tt_gameRecorder.onError((e)=> {
  347. console.log('ttGameRecord error:',e)
  348. })
  349. }
  350. // 抖音关闭屏幕录制
  351. public static ttStopScreenRecording(isClearVideoPath:boolean = false) {
  352. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  353. return
  354. }
  355. if(this.tt_gameRecorder == null) {
  356. return
  357. }
  358. if(isClearVideoPath) {
  359. this.tt_recordVideoPath = "";
  360. }
  361. this.tt_gameRecorder.stop()
  362. }
  363. // 抖音获取屏幕录制视频文件
  364. public static ttGetScreenRecordingVideoPath():string {
  365. return this.tt_recordVideoPath;
  366. }
  367. // 抖音分享屏幕录制视频
  368. private static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
  369. tt.shareAppMessage({
  370. title: title,
  371. templateId: config.TT_SHARE_TEMPLATEID,
  372. channel: "video",
  373. extra: {
  374. videoTopics: [config.gameName],
  375. hashtag_list: ['小游戏','小程序'],
  376. videoPath: videoPath,
  377. withVideoId: true,
  378. },
  379. success: (res) => {
  380. console.log('抖音分享屏幕录制视频,成功=',res)
  381. onSuccess && onSuccess();
  382. },
  383. fail: (e) => {
  384. // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
  385. console.log('抖音分享屏幕录制视频,失败=',e)
  386. onFail && onFail()
  387. }
  388. })
  389. }
  390. }