sdkUtil.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import { _decorator, SpriteFrame, sys } from "cc";
  2. import { gameManager } from "./run/gameManager";
  3. //管理广告、分享、SDK相关内容的组件
  4. export class SdkUtil {
  5. public static platform: string = 'cocos'; //平台
  6. public static imgAd: SpriteFrame = null!;
  7. public static imgShare: SpriteFrame = null!;
  8. public static isDebugMode: boolean = false;
  9. public static onlineInterval: number = -1;
  10. public static isEnableVibrate: boolean = true;
  11. public static isCheckOffline: boolean = false; //登录后会检查是否展示登录界面,而且只检查一次
  12. public static isWatchVideoAd: boolean = false;//是否正在播放广告
  13. public static isEnableMoving: boolean = false;//是否允许屏幕上下移动
  14. public static isEnableZoom: boolean = false;//是否允许屏幕缩放
  15. public static arrLockDiary = [];//未解锁日记
  16. public static vibrateInterval: number = 100;//两次震动之间的间隔,AppActivity里面的震动间隔也是100
  17. public static vibratePreTime: number = 0;//上次震动时间
  18. public static videoAd:any =null;
  19. public static tt_isSupportSidebar:boolean = false; //抖音_是否支持侧边栏
  20. public static tt_isToEnterFromSidebar:boolean = false; //抖音_是否从侧边栏进入
  21. private static ttGameRecorder:any = null; //抖音游戏录制
  22. private static ttRecordVideoPath:string = ''; //抖音录制视频路径
  23. private static ttTotalRecord:number = 300; //抖音总录制时间
  24. private static ttIsRecording:boolean = false; //抖音是否录制中
  25. /**
  26. * 自定义事件统计
  27. *
  28. * @param {string} eventType
  29. * @param {object} objParams
  30. */
  31. public static customEventStatistics(eventType: string, objParams?: any) {
  32. eventType = eventType.toString();
  33. if (!objParams) {
  34. objParams = {};
  35. }
  36. // console.log({'eventType': eventType},{'objParams': objParams});
  37. if (this.platform === 'wx') {
  38. //@ts-ignore
  39. if (window['wx'] && window['wx']['aldSendEvent']) {
  40. //@ts-ignore
  41. window.wx['aldSendEvent'](eventType, objParams);
  42. }
  43. }
  44. //@ts-ignore
  45. if (this.platform === 'cocos' && window.cocosAnalytics && window.cocosAnalytics.isInited()) {
  46. console.log("###统计", eventType, objParams);
  47. //@ts-ignore
  48. window.cocosAnalytics.CACustomEvent.onStarted(eventType, objParams);
  49. }
  50. }
  51. /**
  52. * 微信分享
  53. *
  54. * @static
  55. * @param {string} title
  56. * @param {string} imageUrl
  57. * @returns
  58. * @memberof SdkUtil
  59. */
  60. public static shareGame(title: string, imageUrl: string) {
  61. //@ts-ignore
  62. if (!window.wx) {
  63. return;
  64. }
  65. //@ts-ignore
  66. wx.showShareMenu({
  67. withShareTicket: true,
  68. complete: () => {
  69. }
  70. });
  71. //@ts-ignore
  72. if (wx.aldOnShareAppMessage) {
  73. //@ts-ignore
  74. wx.aldOnShareAppMessage(function () {
  75. // 用户点击了“转发”按钮
  76. return {
  77. title: title,
  78. imageUrl: imageUrl,
  79. };
  80. });
  81. } else {
  82. //@ts-ignore
  83. wx.onShareAppMessage(function () {
  84. // 用户点击了“转发”按钮
  85. return {
  86. title: title,
  87. imageUrl: imageUrl,
  88. };
  89. });
  90. }
  91. }
  92. /**
  93. * 抖音/微信激励视频
  94. *
  95. * @static
  96. * @param {string} _adUnitId
  97. * @param {Function} call_back
  98. * @returns
  99. * @memberof SdkUtil
  100. */
  101. public static showVideoAd(_adUnitId: string, call_back) {
  102. if(gameManager.isFreeAds()) {
  103. call_back({"isEnded":true})
  104. return
  105. }
  106. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME||sys.platform==sys.Platform.WECHAT_GAME){
  107. gameManager.Singleton.showLoadingLevel()
  108. if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
  109. SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
  110. } else if(sys.platform==sys.Platform.WECHAT_GAME) {
  111. gameManager.Singleton.hideLoadingLevel()
  112. call_back({"isEnded":true})
  113. return
  114. // SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
  115. }
  116. if(SdkUtil.videoAd==null){
  117. return
  118. }
  119. SdkUtil.videoAd.onLoad(() => {
  120. SdkUtil.videoAd.show();
  121. console.log("广告加载完成");
  122. });
  123. SdkUtil.videoAd.onClose((res) => {
  124. call_back(res)
  125. SdkUtil.videoAd.destroy()
  126. gameManager.Singleton.hideLoadingLevel()
  127. });
  128. SdkUtil.videoAd.onError((res) => {
  129. let errorString = res.errCode + '-' + res.errMsg
  130. call_back({isEnded:false,errorString:errorString})
  131. SdkUtil.videoAd.destroy()
  132. gameManager.Singleton.hideLoadingLevel()
  133. });
  134. SdkUtil.videoAd.load()
  135. }
  136. }
  137. /**
  138. * 抖音侧边栏
  139. *
  140. * @static
  141. * @memberof SdkUtil
  142. */
  143. public static ttRegisterSidebar() {
  144. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  145. tt.onShow((res) => {
  146. console.log('tt.onShow =', res)
  147. // console.log('res=',res)
  148. if(res.scene == '021036' || res.scene == '101036') {
  149. SdkUtil.tt_isToEnterFromSidebar = true
  150. }
  151. if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
  152. SdkUtil.tt_isToEnterFromSidebar = true
  153. }
  154. });
  155. tt.checkScene({
  156. scene: "sidebar",
  157. success: (res) => {
  158. console.log("check scene success: ", res.isExist);
  159. if(res.isExist != undefined || res.isExist != null) {
  160. SdkUtil.tt_isSupportSidebar = res.isExist
  161. }
  162. },
  163. fail: (res) => {
  164. console.log("check scene fail:", res);
  165. }
  166. });
  167. let options = tt.getLaunchOptionsSync()
  168. console.log('getLaunchOptionsSync=', options)
  169. if(options.scene == '021036' || options.scene == '101036') {
  170. SdkUtil.tt_isToEnterFromSidebar = true
  171. }
  172. }
  173. }
  174. // 抖音检测是否显示奖励
  175. public static ttCheckSceneShowRewards():boolean {
  176. return SdkUtil.tt_isSupportSidebar
  177. }
  178. // 抖音检测是否从侧边栏进入
  179. public static ttCheckToEnterFromSidebar():boolean {
  180. return SdkUtil.tt_isToEnterFromSidebar
  181. }
  182. // 抖音导航到侧边栏场景
  183. public static ttNavToSidebarScene() {
  184. if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
  185. tt.navigateToScene({
  186. scene: "sidebar",
  187. success: (res) => {
  188. // console.log("navigate to scene success");
  189. },
  190. fail: (res) => {
  191. // console.log("navigate to scene fail: ", res);
  192. },
  193. });
  194. }
  195. }
  196. // 抖音开启屏幕录制
  197. public static ttStartScreenRecording() {
  198. return
  199. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  200. return
  201. }
  202. if(this.ttIsScreenRecording()==true) {
  203. this.ttStopScreenRecording()
  204. }
  205. this.ttIsRecording = true
  206. if(!this.ttGameRecorder) {
  207. this.ttGameRecorder = tt.getGameRecorderManager()
  208. }
  209. this.ttGameRecorder.start({duration: this.ttTotalRecord})
  210. this.ttGameRecorder.onStart(()=> {
  211. console.log('GameRecorder onStart onStart onStart')
  212. })
  213. this.ttGameRecorder.onStop((res)=> {
  214. console.log('GameRecorder onStop onStop onStop=',res)
  215. this.ttIsRecording = false
  216. this.ttRecordVideoPath = res.videoPath
  217. SdkUtil.ttShareScreenRecordVideo('测试测试',res.videoPath)
  218. })
  219. this.ttGameRecorder.onError((e)=> {
  220. console.log('ttGameRecord error:',e)
  221. })
  222. }
  223. // 抖音关闭屏幕录制
  224. public static ttStopScreenRecording() {
  225. return
  226. if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
  227. return
  228. }
  229. if(this.ttGameRecorder == null) {
  230. return
  231. }
  232. this.ttGameRecorder.stop()
  233. }
  234. // 抖音是否屏幕录制
  235. public static ttIsScreenRecording():boolean {
  236. return this.ttIsRecording;
  237. }
  238. // 抖音获取屏幕录制视频文件
  239. public static ttGetScreenRecordingVideoPath():string {
  240. return this.ttRecordVideoPath;
  241. }
  242. // 抖音分享屏幕录制视频
  243. public static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
  244. tt.shareAppMessage({
  245. title: title,
  246. channel: "video",
  247. extra: {
  248. videoTopics: [],
  249. hashtag_list: [],
  250. videoPath: videoPath,
  251. withVideoId: true,
  252. },
  253. success: (res) => {
  254. console.log('抖音分享屏幕录制视频,成功=',res)
  255. onSuccess && onSuccess();
  256. },
  257. fail: (e) => {
  258. console.log('抖音分享屏幕录制视频,失败=',e)
  259. // if(this.checkAppName()){
  260. // onSuccess && onSuccess();
  261. // }else{
  262. // onFail && onFail();
  263. // }
  264. // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
  265. onFail && onFail()
  266. }
  267. })
  268. }
  269. }