sdkUtil.ts 13 KB

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