sdkUtil.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. /**
  140. * 抖音/微信激励视频
  141. *
  142. * @static
  143. * @param {string} _adUnitId
  144. * @param {Function} call_back
  145. * @returns
  146. * @memberof SdkUtil
  147. */
  148. public static showVideoAd(_adUnitId: string, call_back) {
  149. if(gameManager.isFreeAds()) {
  150. call_back({"isEnded":true})
  151. return
  152. }
  153. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME||sys.platform==sys.Platform.WECHAT_GAME){
  154. gameManager.Singleton.showLoadingLevel()
  155. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  156. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  157. } else if(sys.platform==sys.Platform.WECHAT_GAME) {
  158. gameManager.Singleton.hideLoadingLevel()
  159. call_back({"isEnded":true})
  160. return
  161. // SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
  162. }
  163. if(SdkUtil.videoAd==null){
  164. return
  165. }
  166. SdkUtil.videoAd.onLoad(() => {
  167. SdkUtil.videoAd.show();
  168. console.log("广告加载完成");
  169. });
  170. SdkUtil.videoAd.onClose((res) => {
  171. call_back(res)
  172. SdkUtil.videoAd.destroy()
  173. gameManager.Singleton.hideLoadingLevel()
  174. });
  175. SdkUtil.videoAd.onError((res) => {
  176. let errorString = res.errCode + '-' + res.errMsg
  177. call_back({isEnded:false,errorString:errorString})
  178. SdkUtil.videoAd.destroy()
  179. gameManager.Singleton.hideLoadingLevel()
  180. });
  181. SdkUtil.videoAd.load()
  182. }
  183. }
  184. /**
  185. * 抖音侧边栏
  186. *
  187. * @static
  188. * @memberof SdkUtil
  189. */
  190. public static ttRegisterSidebar() {
  191. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  192. tt.onShow((res) => {
  193. console.log('tt.onShow =', res)
  194. // console.log('res=',res)
  195. if(res.scene == '021036' || res.scene == '101036') {
  196. SdkUtil.tt_isToEnterFromSidebar = true
  197. }
  198. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  199. SdkUtil.tt_isToEnterFromSidebar = true
  200. }
  201. });
  202. tt.checkScene({
  203. scene: "sidebar",
  204. success: (res) => {
  205. console.log("check scene success: ", res.isExist);
  206. if(res.isExist != undefined || res.isExist != null) {
  207. SdkUtil.tt_isSupportSidebar = res.isExist
  208. }
  209. },
  210. fail: (res) => {
  211. console.log("check scene fail:", res);
  212. }
  213. });
  214. let options = tt.getLaunchOptionsSync()
  215. console.log('getLaunchOptionsSync=', options)
  216. if(options.scene == '021036' || options.scene == '101036') {
  217. SdkUtil.tt_isToEnterFromSidebar = true
  218. }
  219. }
  220. }
  221. // 抖音检测是否显示奖励
  222. public static ttCheckSceneShowRewards():boolean {
  223. return SdkUtil.tt_isSupportSidebar
  224. }
  225. // 抖音检测是否从侧边栏进入
  226. public static ttCheckToEnterFromSidebar():boolean {
  227. return SdkUtil.tt_isToEnterFromSidebar
  228. }
  229. // 抖音导航到侧边栏场景
  230. public static ttNavToSidebarScene() {
  231. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  232. tt.navigateToScene({
  233. scene: "sidebar",
  234. success: (res) => {
  235. // console.log("navigate to scene success");
  236. },
  237. fail: (res) => {
  238. // console.log("navigate to scene fail: ", res);
  239. },
  240. });
  241. }
  242. }
  243. // 抖音开启屏幕录制
  244. public static ttStartScreenRecording() {
  245. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  246. return
  247. }
  248. if(this.ttIsScreenRecording()==true) {
  249. this.ttStopScreenRecording()
  250. }
  251. if(!this.tt_gameRecorder) {
  252. this.tt_gameRecorder = tt.getGameRecorderManager()
  253. }
  254. this.tt_gameRecorder.start({duration: this.tt_totalRecord})
  255. this.tt_gameRecorder.onStart(()=> {
  256. this.tt_isRecording = true
  257. // console.log('GameRecorder onStart onStart onStart')
  258. })
  259. this.tt_gameRecorder.onStop((res)=> {
  260. // console.log('GameRecorder onStop onStop onStop=',res)
  261. this.tt_isRecording = false
  262. this.tt_recordVideoPath = res.videoPath
  263. })
  264. this.tt_gameRecorder.onError((e)=> {
  265. console.log('ttGameRecord error:',e)
  266. })
  267. }
  268. // 抖音关闭屏幕录制
  269. public static ttStopScreenRecording(isClearVideoPath:boolean = false) {
  270. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  271. return
  272. }
  273. if(this.tt_gameRecorder == null) {
  274. return
  275. }
  276. if(isClearVideoPath) {
  277. this.tt_recordVideoPath = "";
  278. }
  279. this.tt_gameRecorder.stop()
  280. }
  281. // 抖音是否屏幕录制
  282. public static ttIsScreenRecording():boolean {
  283. return this.tt_isRecording;
  284. }
  285. // 抖音获取屏幕录制视频文件
  286. public static ttGetScreenRecordingVideoPath():string {
  287. return this.tt_recordVideoPath;
  288. }
  289. // 抖音分享屏幕录制视频
  290. private static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
  291. tt.shareAppMessage({
  292. title: title,
  293. templateId: config.TT_SHARE_TEMPLATEID,
  294. channel: "video",
  295. extra: {
  296. videoTopics: [config.gameName],
  297. hashtag_list: ['小游戏','小程序'],
  298. videoPath: videoPath,
  299. withVideoId: true,
  300. },
  301. success: (res) => {
  302. console.log('抖音分享屏幕录制视频,成功=',res)
  303. onSuccess && onSuccess();
  304. },
  305. fail: (e) => {
  306. // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
  307. console.log('抖音分享屏幕录制视频,失败=',e)
  308. onFail && onFail()
  309. }
  310. })
  311. }
  312. }