sdkUtil.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. import { _decorator, SpriteFrame, sys } from "cc";
  2. import { gameManager } from "./run/gameManager";
  3. import { config } from "./config";
  4. //管理广告、分享、SDK相关内容的组件
  5. export class SdkUtil {
  6. public static platform: string = 'cocos'; //平台
  7. public static imgAd: SpriteFrame = null!;
  8. public static imgShare: SpriteFrame = null!;
  9. public static isDebugMode: boolean = false;
  10. public static onlineInterval: number = -1;
  11. public static isEnableVibrate: boolean = true;
  12. public static isCheckOffline: boolean = false; //登录后会检查是否展示登录界面,而且只检查一次
  13. public static isWatchVideoAd: boolean = false;//是否正在播放广告
  14. public static isEnableMoving: boolean = false;//是否允许屏幕上下移动
  15. public static isEnableZoom: boolean = false;//是否允许屏幕缩放
  16. public static arrLockDiary = [];//未解锁日记
  17. public static vibrateInterval: number = 100;//两次震动之间的间隔,AppActivity里面的震动间隔也是100
  18. public static vibratePreTime: number = 0;//上次震动时间
  19. public static videoAd:any =null;
  20. public static tt_systemInfo: any = null; //抖音_系统信息
  21. private static tt_isSupportSidebar:boolean = false; //抖音_是否支持侧边栏
  22. private static tt_isToEnterFromSidebar:boolean = false; //抖音_是否从侧边栏进入
  23. private static tt_gameRecorder:any = null; //抖音游戏录制
  24. private static tt_recordVideoPath:string = ''; //抖音录制视频路径
  25. private static tt_totalRecord:number = 300; //抖音总录制时间
  26. private static tt_isRecording:boolean = false; //抖音是否录制中
  27. public static init() {
  28. this.ttGetSystemInfo(()=> {
  29. this.ttRegisterSidebar()
  30. })
  31. }
  32. public static iPhoneIsLingdongdao():boolean {
  33. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  34. if(SdkUtil.tt_systemInfo !=null) {
  35. if(SdkUtil.tt_systemInfo.brand=='Apple'||SdkUtil.tt_systemInfo.brand=='devtools')
  36. if(SdkUtil.tt_systemInfo.model=='iPhone 14'||
  37. SdkUtil.tt_systemInfo.model=='iPhone 14 Pro'||
  38. SdkUtil.tt_systemInfo.model=='iPhone 14 Pro Max'||
  39. SdkUtil.tt_systemInfo.model=='iPhone 15'||
  40. SdkUtil.tt_systemInfo.model=='iPhone 15 Pro'||
  41. SdkUtil.tt_systemInfo.model=='iPhone 15 Pro Max') {
  42. return true
  43. }
  44. }
  45. }
  46. return false
  47. }
  48. /**
  49. * 自定义事件统计
  50. *
  51. * @param {string} eventType
  52. * @param {object} objParams
  53. */
  54. public static customEventStatistics(eventType: string, objParams?: any) {
  55. eventType = eventType.toString();
  56. if (!objParams) {
  57. objParams = {};
  58. }
  59. // console.log({'eventType': eventType},{'objParams': objParams});
  60. if (this.platform === 'wx') {
  61. //@ts-ignore
  62. if (window['wx'] && window['wx']['aldSendEvent']) {
  63. //@ts-ignore
  64. window.wx['aldSendEvent'](eventType, objParams);
  65. }
  66. }
  67. //@ts-ignore
  68. if (this.platform === 'cocos' && window.cocosAnalytics && window.cocosAnalytics.isInited()) {
  69. console.log("###统计", eventType, objParams);
  70. //@ts-ignore
  71. window.cocosAnalytics.CACustomEvent.onStarted(eventType, objParams);
  72. }
  73. }
  74. /**
  75. * 抖音/微信分享
  76. *
  77. * @static
  78. * @param {string} title
  79. * @param {string} imageUrl
  80. * @param {string} videoPath
  81. * @returns
  82. * @memberof SdkUtil
  83. */
  84. public static shareGame(title: string, imageUrl: string, videoPath: string) {
  85. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  86. if(videoPath!="") {
  87. this.ttShareScreenRecordVideo(config.gameName,videoPath)
  88. }
  89. } else if(sys.platform == sys.Platform.WECHAT_GAME) {
  90. title = title + '好刺激、好好玩'
  91. this.wxShare(title, imageUrl)
  92. }
  93. }
  94. /**
  95. * 微信分享
  96. *
  97. * @static
  98. * @param {string} title
  99. * @param {string} imageUrl
  100. * @returns
  101. * @memberof SdkUtil
  102. */
  103. public static wxShare(title: string, imageUrl: string) {
  104. // console.log('微信分享=',title, 'imageUrl=',imageUrl)
  105. wx.showShareMenu({
  106. withShareTicket: true,
  107. // shareAppMessage(可以删除):显示分享给好友选项,shareTimeline(可以删除):显示分享至朋友圈选项
  108. // 可以只开启前者。如果要开启后者,则两者必须都开启才能生效。
  109. // menus: ['shareAppMessage', 'shareTimeline'],
  110. menus: ['shareAppMessage'],
  111. complete: () => {}
  112. });
  113. // 主动分享
  114. wx.shareAppMessage({
  115. title: title,
  116. imageUrl: imageUrl
  117. });
  118. // 被动分享
  119. // wx.onShareAppMessage( () => {
  120. // return {
  121. // // 标题,不传则默认使用小游戏的名称
  122. // title: title,
  123. // // 转发链接所显示的图片,比例5:4,资源可以是本地或远程。不传则默认使用游戏截图。
  124. // imageUrl: imageUrl,
  125. // }
  126. // });
  127. //@ts-ignore
  128. // if (!window.wx) {
  129. // return;
  130. // }
  131. //
  132. //@ts-ignore
  133. // wx.showShareMenu({
  134. // withShareTicket: true,
  135. // complete: () => {
  136. // console.log('')
  137. // }
  138. // }
  139. //
  140. //@ts-ignore
  141. // if (wx.aldOnShareAppMessage) {
  142. // //@ts-ignore 被动分享
  143. // wx.aldOnShareAppMessage(function () {
  144. // // 用户点击了“转发”按钮
  145. // return {
  146. // title: title,
  147. // imageUrl: imageUrl,
  148. // };
  149. // });
  150. // } else {
  151. // //@ts-ignore
  152. // wx.onShareAppMessage(function () {
  153. // // 用户点击了“转发”按钮
  154. // return {
  155. // title: title,
  156. // imageUrl: imageUrl,
  157. // };
  158. // });
  159. // }
  160. }
  161. public static getAdId(ad_type = config.AD_TYPE.UNKNOWN):string {
  162. let ad_id = ""
  163. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  164. if(ad_type == config.AD_TYPE.RE_LIFE) {
  165. ad_id = config.TT_REWARD.RE_LIFE
  166. } else if (ad_type == config.AD_TYPE.ANSWER) {
  167. ad_id = config.TT_REWARD.ANSWER
  168. } else if (ad_type == config.AD_TYPE.LOOK_TIPS) {
  169. ad_id = config.TT_REWARD.LOOK_TIPS
  170. } else if (ad_type == config.AD_TYPE.UN_LOCK_24) {
  171. ad_id = config.TT_REWARD.UN_LOCK_24
  172. } else if (ad_type == config.AD_TYPE.UN_LOCK) {
  173. ad_id = config.TT_REWARD.UN_LOCK
  174. }
  175. } else if (sys.platform == sys.Platform.WECHAT_GAME) {
  176. if(ad_type == config.AD_TYPE.RE_LIFE) {
  177. ad_id = config.WX_REWARD.RE_LIFE
  178. } else if (ad_type == config.AD_TYPE.ANSWER) {
  179. ad_id = config.WX_REWARD.ANSWER
  180. } else if (ad_type == config.AD_TYPE.LOOK_TIPS) {
  181. ad_id = config.WX_REWARD.LOOK_TIPS
  182. } else if (ad_type == config.AD_TYPE.UN_LOCK_24) {
  183. ad_id = config.WX_REWARD.UN_LOCK_24
  184. } else if (ad_type == config.AD_TYPE.UN_LOCK) {
  185. ad_id = config.WX_REWARD.UN_LOCK
  186. }
  187. }
  188. return ad_id
  189. }
  190. /**
  191. * 抖音/微信激励视频
  192. *
  193. * @static
  194. * @param {string} _adUnitId
  195. * @param {Function} call_back
  196. * @returns
  197. * @memberof SdkUtil
  198. */
  199. public static showVideoAd(_adUnitId: string, call_back) {
  200. if(gameManager.isFreeAds()) {
  201. call_back({"isEnded":true})
  202. return
  203. }
  204. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME||sys.platform==sys.Platform.WECHAT_GAME){
  205. gameManager.Singleton.showLoadingLevel()
  206. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  207. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  208. } else if(sys.platform==sys.Platform.WECHAT_GAME) {
  209. gameManager.Singleton.hideLoadingLevel()
  210. call_back({"isEnded":true})
  211. return
  212. // SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
  213. }
  214. if(SdkUtil.videoAd==null){
  215. gameManager.Singleton.hideLoadingLevel()
  216. return
  217. }
  218. SdkUtil.videoAd.onLoad(() => {
  219. SdkUtil.videoAd.show();
  220. console.log("广告加载完成");
  221. });
  222. SdkUtil.videoAd.onClose((res) => {
  223. call_back(res)
  224. SdkUtil.videoAd.destroy()
  225. gameManager.Singleton.hideLoadingLevel()
  226. });
  227. SdkUtil.videoAd.onError((res) => {
  228. let errorString = res.errCode + '-' + res.errMsg
  229. call_back({isEnded:false,errorString:errorString})
  230. SdkUtil.videoAd.destroy()
  231. gameManager.Singleton.hideLoadingLevel()
  232. });
  233. SdkUtil.videoAd.load()
  234. }
  235. }
  236. /**
  237. * 抖音获取系统信息
  238. */
  239. public static ttGetSystemInfo(callback) {
  240. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  241. tt.getSystemInfo({
  242. success:(res) => {
  243. // console.log('tt.getSystemInfo=',res)
  244. // appName: "Douyin" appName: "douyin_lite"
  245. SdkUtil.tt_systemInfo = res
  246. callback()
  247. }
  248. })
  249. }
  250. }
  251. /**
  252. * 抖音侧边栏
  253. *
  254. * @static
  255. * @memberof SdkUtil
  256. */
  257. public static ttRegisterSidebar() {
  258. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  259. tt.onShow((res) => {
  260. console.log('tt.onShow =', res)
  261. // console.log('res=',res)
  262. if(res.scene == '021036' || res.scene == '101036') {
  263. SdkUtil.tt_isToEnterFromSidebar = true
  264. }
  265. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  266. SdkUtil.tt_isToEnterFromSidebar = true
  267. }
  268. });
  269. tt.checkScene({
  270. scene: "sidebar",
  271. success: (res) => {
  272. console.log("check scene success: ", res.isExist);
  273. if(res.isExist != undefined || res.isExist != null) {
  274. SdkUtil.tt_isSupportSidebar = res.isExist
  275. }
  276. },
  277. fail: (res) => {
  278. console.log("check scene fail:", res);
  279. }
  280. });
  281. let options = tt.getLaunchOptionsSync()
  282. console.log('getLaunchOptionsSync=', options)
  283. if(options.scene == '021036' || options.scene == '101036') {
  284. SdkUtil.tt_isToEnterFromSidebar = true
  285. }
  286. }
  287. }
  288. // 抖音检测是否显示奖励
  289. public static ttCheckSceneShowRewards():boolean {
  290. return SdkUtil.tt_isSupportSidebar
  291. }
  292. // 抖音检测是否从侧边栏进入
  293. public static ttCheckToEnterFromSidebar():boolean {
  294. return SdkUtil.tt_isToEnterFromSidebar
  295. }
  296. // 抖音导航到侧边栏场景
  297. public static ttNavToSidebarScene() {
  298. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  299. tt.navigateToScene({
  300. scene: "sidebar",
  301. success: (res) => {
  302. // console.log("navigate to scene success");
  303. },
  304. fail: (res) => {
  305. // console.log("navigate to scene fail: ", res);
  306. },
  307. });
  308. }
  309. }
  310. // 抖音开启屏幕录制
  311. public static ttStartScreenRecording() {
  312. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  313. return
  314. }
  315. if(this.tt_systemInfo.platform == 'devtools') {
  316. console.log('抖音模拟器')
  317. return
  318. }
  319. if(this.ttIsScreenRecording()==true) {
  320. this.ttStopScreenRecording()
  321. }
  322. if(!this.tt_gameRecorder) {
  323. this.tt_gameRecorder = tt.getGameRecorderManager()
  324. }
  325. this.tt_gameRecorder.start({duration: this.tt_totalRecord})
  326. this.tt_gameRecorder.onStart(()=> {
  327. this.tt_isRecording = true
  328. // console.log('GameRecorder onStart onStart onStart')
  329. })
  330. this.tt_gameRecorder.onStop((res)=> {
  331. // console.log('GameRecorder onStop onStop onStop=',res)
  332. this.tt_isRecording = false
  333. this.tt_recordVideoPath = res.videoPath
  334. })
  335. this.tt_gameRecorder.onError((e)=> {
  336. console.log('ttGameRecord error:',e)
  337. })
  338. }
  339. // 抖音关闭屏幕录制
  340. public static ttStopScreenRecording(isClearVideoPath:boolean = false) {
  341. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  342. return
  343. }
  344. if(this.tt_gameRecorder == null) {
  345. return
  346. }
  347. if(isClearVideoPath) {
  348. this.tt_recordVideoPath = "";
  349. }
  350. this.tt_gameRecorder.stop()
  351. }
  352. // 抖音是否屏幕录制
  353. public static ttIsScreenRecording():boolean {
  354. return this.tt_isRecording;
  355. }
  356. // 抖音获取屏幕录制视频文件
  357. public static ttGetScreenRecordingVideoPath():string {
  358. return this.tt_recordVideoPath;
  359. }
  360. // 抖音分享屏幕录制视频
  361. private static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
  362. tt.shareAppMessage({
  363. title: title,
  364. templateId: config.TT_SHARE_TEMPLATEID,
  365. channel: "video",
  366. extra: {
  367. videoTopics: [config.gameName],
  368. hashtag_list: ['小游戏','小程序'],
  369. videoPath: videoPath,
  370. withVideoId: true,
  371. },
  372. success: (res) => {
  373. console.log('抖音分享屏幕录制视频,成功=',res)
  374. onSuccess && onSuccess();
  375. },
  376. fail: (e) => {
  377. // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
  378. console.log('抖音分享屏幕录制视频,失败=',e)
  379. onFail && onFail()
  380. }
  381. })
  382. }
  383. }