sdkUtil.ts 14 KB

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