12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034 |
- import { _decorator, error, SpriteFrame, sys } from "cc";
- import { config } from "./config";
- import { uiManager } from "./manager/uiManager";
- import { userInfo } from "./data";
- import { tools } from "./tools";
- //管理广告、分享、SDK相关内容的组件
- export class SdkUtil {
- public static platform: string = 'cocos'; //平台
- public static imgAd: SpriteFrame = null!;
- public static imgShare: SpriteFrame = null!;
- public static isDebugMode: boolean = false;
- public static onlineInterval: number = -1;
- public static isEnableVibrate: boolean = true;
- public static isCheckOffline: boolean = false; //登录后会检查是否展示登录界面,而且只检查一次
- public static isWatchVideoAd: boolean = false;//是否正在播放广告
- public static isEnableMoving: boolean = false;//是否允许屏幕上下移动
- public static isEnableZoom: boolean = false;//是否允许屏幕缩放
- public static arrLockDiary = [];//未解锁日记
- public static vibrateInterval: number = 100;//两次震动之间的间隔,AppActivity里面的震动间隔也是100
- public static vibratePreTime: number = 0;//上次震动时间
- public static videoAd:any =null;
- public static isLookAd:boolean = false; //是否在看广告
- public static gridGamePanel = null; //互推游戏组件
- public static tt_systemInfo: any = null; //抖音_系统信息
- public static wx_systemInfo:any = null; //微信_系统信息
- public static ks_systemInfo:any = null; //ks_系统信息
- private static tt_isSupportSidebar:boolean = false; //抖音_是否支持侧边栏
- private static tt_isToEnterFromSidebar:boolean = false; //抖音_是否从侧边栏进入
- private static tt_gameRecorder:any = null; //抖音游戏录制
- private static tt_recordVideoPath:string = ''; //抖音录制视频路径
- private static tt_totalRecord:number = 4000; //抖音总录制时间
- private static tt_isRecording:boolean = false; //抖音是否录制中
- //------------------------------ 公共 ------------------------------//
- public static init() {
- if(sys.platform==sys.Platform.BYTEDANCE_MINI_GAME) {
- tools.platform = config.Platform.TT
- } else if(sys.platform==sys.Platform.WECHAT_GAME) {
- let isKSGame = typeof KSGameGlobal != 'undefined'
- // console.log('isKSGame=',isKSGame)
- if(isKSGame) {
- tools.platform = config.Platform.KS
- } else {
- tools.platform = config.Platform.WX
- //微信初始化设置显示分享弹窗,右上角'···'才可以转发给朋友,复制链接
- wx.showShareMenu({
- withShareTicket: true,
- menus: ['shareAppMessage', 'shareTimeline']
- })
- }
- }
-
- this.getSystemInfo(()=> {
- this.ttRegisterInfo()
- this.registerGridGamePanel()
- })
- }
- // 获取系统信息
- public static getSystemInfo(callback) {
- switch (tools.platform) {
- case config.Platform.TT:
- tt.getSystemInfo({
- success:(res) => {
- console.log('tt.getSystemInfo=',res)
- // appName: "Douyin" appName: "douyin_lite"
- SdkUtil.tt_systemInfo = res
- callback()
- }
- })
- break;
- case config.Platform.WX:
- wx.getSystemInfo({
- success:(res) => {
- console.log('wx.getSystemInfo=',res)
- SdkUtil.wx_systemInfo = res
- callback && callback()
- }
- })
- break;
- case config.Platform.KS:
- ks.getSystemInfo({
- success:(res) => {
- console.log('ks.getSystemInfo=',res)
- SdkUtil.ks_systemInfo = res
- callback && callback()
- }
- })
- break;
-
- default:
- break;
- }
- }
- // 是否平台开发工具
- public static is_devtools_platform():boolean {
- if(SdkUtil.tt_systemInfo&&SdkUtil.tt_systemInfo.platform=='devtools') {
- return true
- }
- if(SdkUtil.wx_systemInfo&&SdkUtil.wx_systemInfo.platform=='devtools') {
- return true
- }
- if(SdkUtil.ks_systemInfo&&SdkUtil.ks_systemInfo.platform=='devtools') {
- return true
- }
- return false
- }
- // 自定义事件统计
- public static customEventStatistics(eventType: string, objParams?: any) {
- eventType = eventType.toString();
- if (!objParams) { objParams = {}; }
- // console.log({'eventType': eventType},{'objParams': objParams});
- if (this.platform === 'wx') {
- //@ts-ignore
- if (window['wx'] && window['wx']['aldSendEvent']) {
- //@ts-ignore
- window.wx['aldSendEvent'](eventType, objParams);
- }
- }
- //@ts-ignore
- if (this.platform === 'cocos' && window.cocosAnalytics && window.cocosAnalytics.isInited()) {
- console.log("###统计", eventType, objParams);
- //@ts-ignore
- window.cocosAnalytics.CACustomEvent.onStarted(eventType, objParams);
- }
- }
- // 检测授权用户信息
- public static checkAuthUserInfo(cb) {
- if(tools.platform==config.Platform.WX) {
- SdkUtil.wxCheckAuthUserInfo(cb)
- } else {
- cb(true)
- }
- }
-
- // 登录
- public static login(call){
- switch (tools.platform) {
- case config.Platform.TT:
- tt.login({
- force: true,
- success(res) {
- console.log(`tt_login 调用成功:${res.code} ${res.anonymousCode}`);
- call({"code":res.code,"anonymousCode":res.anonymousCode})
- },
- fail(err) {
- console.log(`tt_login 调用失败:${err}`);
- // call(null)
- },
- });
- break;
- case config.Platform.WX:
- wx.login({
- success(res) {
- console.log(`wx_login 调用成功:${res.code}`);
- call({"code":res.code})
- },
- fali(err) {
- console.log(`wx_login 调用失败:${err}`);
- // call(null)
- }
- })
- break;
- case config.Platform.KS:
- ks.login({
- success(res) {
- console.log(`ks_login 调用成功:${res.code}`);
- call({"code":res.code})
- },
- fali(err) {
- console.log(`ks_login 调用失败:${err}`);
- // call(null)
- }
- })
- break;
- default:
- call(null)
- break;
- }
- }
- // 获取用户信息
- public static getUserInfo(call){
- switch (tools.platform) {
- case config.Platform.TT:
- tt.getUserInfo({
- // withCredentials: true,
- // withRealNameAuthenticationInfo: true,
- success(res) {
- console.log(`tt_getUserInfo 调用成功`, res);
- call(res.userInfo)
- },
- fail(err) {
- console.log(`tt_getUserInfo 调用失败`, err);
- call(new userInfo())
- },
- });
- break;
- case config.Platform.WX:
- SdkUtil.wxCheckAuthUserInfo((is_true)=>{
- if(is_true) {
- wx.getUserInfo({
- success(res) {
- console.log(`wx_getUserInfo 调用成功`, res);
- call(res.userInfo)
- },
- fail(err) {
- console.log(`wx_getUserInfo 调用失败`, err);
- call(new userInfo())
- },
- })
- } else {
- console.log(`wx_getUserInfo 调用失败 没有授权用户信息`);
- call(new userInfo())
- }
- })
- break;
- case config.Platform.KS:
- SdkUtil.ksAuthUserInfo((is_true)=>{
- if(is_true) {
- ks.getUserInfo({
- success(res) {
- console.log(`ks_getUserInfo 调用成功`, res);
- call(res.userInfo)
- },
- fail(err) {
- console.log(`ks_getUserInfo 调用失败`, err);
- call(new userInfo())
- }
- })
- } else {
- call(new userInfo())
- }
- })
- break;
- default:
- call(null)
- break;
- }
- }
- // 震动
- public static vibrateShort(){
- switch (tools.platform) {
- case config.Platform.TT:
- tt.vibrateShort({ success(res) {}, fail(err) {} });
- break;
- case config.Platform.WX:
- wx.vibrateShort({ success(res) {}, fail(err) {} });
- break;
- case config.Platform.KS:
- ks.vibrateShort({ success(res) {}, fail(err) {} });
- break;
- default:
- break;
- }
- }
- // 关于loading
- public static showLoading(title:string) {
- if(title.length<=0) { return }
- switch (tools.platform) {
- case config.Platform.TT:
- tt.showLoading({ title: title, success(res) {},fail(err) {} })
- break;
- case config.Platform.WX:
- wx.showLoading({ title: title, success(res) {},fail(err) {} })
- break;
- case config.Platform.KS:
- ks.showLoading({ title: title, success(res) {},fail(err) {} })
- break;
-
- default:
- break;
- }
- }
- public static hideLoading() {
- switch (tools.platform) {
- case config.Platform.TT:
- tt.hideLoading({ success(res) { }, fail(err) { } });
- break;
- case config.Platform.WX:
- wx.hideLoading({ success(res) { }, fail(err) { } });
- break;
- case config.Platform.KS:
- ks.hideLoading({ success(res) { }, fail(err) { } });
- break;
-
- default:
- break;
- }
- }
- // 展示toast
- public static showToast(title:string,duration=2000,success_cb=null, fail_cb=null) {
- switch (tools.platform) {
- case config.Platform.TT:
- tt.showToast({
- title: title,
- duration: duration,
- success(res) { if(success_cb){ success_cb() } },
- fail(err) { if(fail_cb) { fail_cb() } },
- });
- break;
- case config.Platform.WX:
- wx.showToast({
- title: title,
- duration: duration,
- success(res) { if(success_cb){ success_cb() } },
- fail(err) { if(fail_cb) { fail_cb() } },
- });
- break;
- case config.Platform.KS:
- ks.showToast({
- title: title,
- duration: duration,
- success(res) { if(success_cb){ success_cb() } },
- fail(err) { if(fail_cb) { fail_cb() } },
- });
- break;
-
- default:
- break;
- }
- }
- // 展示modal (确定:res.confirm 取消:res.cancel)
- public static showModal(title:string, content:string, suc_cb=null, fail_cb=null, confirmText:string='确定', showCancel:boolean=true, cancelText:string='取消') {
- switch (tools.platform) {
- case config.Platform.TT:
- tt.showModal({
- title: title,
- content: content,
- confirmText: confirmText,
- showCancel: showCancel,
- cancelText: cancelText,
- success(res) { if(suc_cb) { suc_cb(res) } },
- fail(err) { if(fail_cb){ fail_cb(err) } },
- });
- break;
- case config.Platform.WX:
- wx.showModal({
- title: title,
- content: content,
- confirmText: confirmText,
- showCancel: showCancel,
- cancelText: cancelText,
- success(res) { if(suc_cb) { suc_cb(res) } },
- fail(err) { if(fail_cb){ fail_cb(err) } },
- });
- break;
- case config.Platform.KS:
- ks.showModal({
- title: title,
- content: content,
- confirmText: confirmText,
- showCancel: showCancel,
- cancelText: cancelText,
- success(res) { if(suc_cb) { suc_cb(res) } },
- fail(err) { if(fail_cb){ fail_cb(err) } },
- });
- break;
-
- default:
- suc_cb && suc_cb({'confirm':true})
- break;
- }
- }
-
- // 展示actionSheet (点击的索引:`res.tapIndex`)
- public static showActionSheet(item_list:string[], suc_cb=null, fail_cb=null) {
- switch (tools.platform) {
- case config.Platform.TT:
- tt.showActionSheet({
- item_list,
- success(res) { if(suc_cb) { suc_cb(res) } },
- fail(err) { if(fail_cb) { fail_cb(err) } },
- });
- break;
- case config.Platform.WX:
- wx.showActionSheet({
- item_list,
- success(res) { if(suc_cb) { suc_cb(res) } },
- fail(err) { if(fail_cb) { fail_cb(err) } },
- });
- break;
- case config.Platform.KS:
- ks.showActionSheet({
- item_list,
- success(res) { if(suc_cb) { suc_cb(res) } },
- fail(err) { if(fail_cb) { fail_cb(err) } },
- });
- break;
-
- default:
- break;
- }
- }
- // 选择系统相册
- public static choosSystemImage(call_back) {
- switch (tools.platform) {
- case config.Platform.TT:
- tt.chooseImage({
- sourceType: ["album"],
- count:1,
- success:(res)=>{
- // console.log('chooseImage调用成功=${res}')
- if(res.tempFilePaths.length>0) { call_back(res.tempFilePaths[0])}
- },
- fail(err) { console.log(`tt_chooseImage调用失败=${err}`); },
- })
- break;
- case config.Platform.WX:
- wx.chooseMedia({
- mediaType: ["image"],
- sourceType: ["album"],
- count:1,
- success:(res)=>{
- // console.log('chooseImage调用成功=${res}')
- if(res.tempFilePaths.length>0) { call_back(res.tempFilePaths[0])}
- },
- fail(err) { console.log(`wx_chooseImage调用失败=${err}`); },
- })
- break;
- case config.Platform.KS:
- ks.chooseMedia({
- mediaType: ["image"],
- sourceType: ["album"],
- count:1,
- success:(res)=>{
- // console.log('chooseImage调用成功=${res}')
- if(res.tempFilePaths.length>0) { call_back(res.tempFilePaths[0])}
- },
- fail(err) { console.log(`wx_chooseImage调用失败=${err}`); },
- })
- break;
-
- default:
- break;
- }
- }
- // 检测显示添加桌面
- public static checkIsShowAddDesktop():boolean {
- if(tools.platform == config.Platform.TT) {
- if(SdkUtil.tt_systemInfo.appName=='Douyin' || SdkUtil.tt_systemInfo.appName=='douyin_lite') {
- return true
- }
- }
- return false
- }
- // 分享游戏
- public static shareGame(title: string, desc:string, call_back=null) {
- if(desc==undefined||desc==null) { desc = config.gameName}
- switch (tools.platform) {
- case config.Platform.TT:
- SdkUtil.ttShare(title,desc, ()=>{
- call_back(true)
- }, ()=>{
- this.showToast('分享失败')
- })
- break;
- case config.Platform.WX:
- SdkUtil.wxShare(desc, '', ()=>{
- call_back(true)
- },()=>{
- this.showToast('分享失败')
- })
- break;
- case config.Platform.KS:
- SdkUtil.ksShare(desc, '', ()=>{
- call_back(true)
- }, ()=>{
- this.showToast('分享失败')
- })
- break;
-
- default:
- call_back && call_back(true)
- break;
- }
- }
- public static shareGameVideo() {
- if(tools.platform == config.Platform.TT) {
- let videoPath = SdkUtil.ttGetScreenRecordingVideoPath()
- uiManager.Instance().showLoading()
- this.ttShareScreenRecordVideo(config.gameName,videoPath,()=>{
- uiManager.Instance().hideLoading()
- }, ()=>{
- uiManager.Instance().hideLoading()
- })
- }
- }
- // 获取广告id
- public static getAdId(ad_type = config.ADS_TYPE.UNKNOWN):string {
- let ad_id = ""
- switch (tools.platform) {
- case config.Platform.TT:
- if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
- ad_id = config.TT_CONFIG.ADS_ID_INFINITE_DEGREE_VIDEO
- } else if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
- ad_id = config.TT_CONFIG.ADS_ID_RELIFE_VIDEO
- } else if(ad_type==config.ADS_TYPE.GAME_RESTART) {
- ad_id = config.TT_CONFIG.ADS_ID_RESTART
- } else if(ad_type==config.ADS_TYPE.GAME_SIGN_DOUBLE) {
- ad_id = config.TT_CONFIG.ADS_ID_SIGN_DOUBLE
- } else if(ad_type==config.ADS_TYPE.GAME_SIGN_BUQIAN) {
- ad_id = config.TT_CONFIG.ADS_ID_SIGN_BUQIAN
- } else if(ad_type==config.ADS_TYPE.GAME_GET_SUIPIAN) {
- ad_id = config.TT_CONFIG.ADS_ID_GET_SUIPIAN
- }
- break;
- case config.Platform.WX:
- if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
- ad_id = config.WX_CONFIG.ADS_ID_INFINITE_DEGREE_VIDEO
- } else if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
- ad_id = config.WX_CONFIG.ADS_ID_RELIFE_VIDEO
- } else if(ad_type==config.ADS_TYPE.GAME_RESTART) {
- ad_id = config.WX_CONFIG.ADS_ID_RESTART
- } else if(ad_type==config.ADS_TYPE.GAME_SIGN_DOUBLE) {
- ad_id = config.WX_CONFIG.ADS_ID_SIGN_DOUBLE
- } else if(ad_type==config.ADS_TYPE.GAME_SIGN_BUQIAN) {
- ad_id = config.WX_CONFIG.ADS_ID_SIGN_BUQIAN
- } else if(ad_type==config.ADS_TYPE.GAME_GET_SUIPIAN) {
- ad_id = config.WX_CONFIG.ADS_ID_GET_SUIPIAN
- }
- break;
- case config.Platform.KS:
- if(ad_type==config.ADS_TYPE.GAME_INFINITE_DEGREE_VIDEO) {
- ad_id = config.KS_CONFIG.ADS_ID_INFINITE_DEGREE_VIDEO
- } else if(ad_type==config.ADS_TYPE.GAME_RELIFE_VIDEO) {
- ad_id = config.KS_CONFIG.ADS_ID_RELIFE_VIDEO
- } else if(ad_type==config.ADS_TYPE.GAME_RESTART) {
- ad_id = config.KS_CONFIG.ADS_ID_RESTART
- } else if(ad_type==config.ADS_TYPE.GAME_SIGN_DOUBLE) {
- ad_id = config.KS_CONFIG.ADS_ID_SIGN_DOUBLE
- } else if(ad_type==config.ADS_TYPE.GAME_SIGN_BUQIAN) {
- ad_id = config.KS_CONFIG.ADS_ID_SIGN_BUQIAN
- } else if(ad_type==config.ADS_TYPE.GAME_GET_SUIPIAN) {
- ad_id = config.KS_CONFIG.ADS_ID_GET_SUIPIAN
- }
- break;
-
- default:
- break;
- }
- return ad_id
- }
-
- // 显示激励视频广告
- public static showVideoAd(_adUnitId: string, call_back) {
- switch (tools.platform) {
- case config.Platform.TT:
- uiManager.Instance().showLoading()
- SdkUtil.videoAd = tt.createRewardedVideoAd({adUnitId: _adUnitId});
- break;
- case config.Platform.WX:
- uiManager.Instance().showLoading()
- SdkUtil.videoAd = wx.createRewardedVideoAd({adUnitId: _adUnitId});
- break;
- case config.Platform.KS:
- // uiManager.Instance().showLoading()
- // SdkUtil.videoAd = ks.createRewardedVideoAd({adUnitId: _adUnitId});
- call_back && call_back({"isEnded":true})
- return
- break;
-
- default:
- call_back && call_back({"isEnded":true})
- break;
- }
- if(SdkUtil.videoAd==null){
- uiManager.Instance().hideLoading()
- return
- }
- if(tools.platform == config.Platform.WX) {
- // 取消关闭,取消错误,提示重复回调,造成奖励重复发送的问题或错误重复发送的问题
- SdkUtil.videoAd.offClose()
- SdkUtil.videoAd.offError()
- SdkUtil.videoAd.load().then(()=>{
- console.log("wx 广告加载完成");
- SdkUtil.isLookAd = true
- SdkUtil.videoAd.show()
- setTimeout(()=>{
- uiManager.Instance().hideLoading()
- },1000)
- }).catch(err=>{
- console.error('wx 视频广告加载或展示失败:', err);
- let errorString = err.errCode + '-' + err.errMsg
- call_back && call_back({isEnded:false,errorString:errorString})
- SdkUtil.isLookAd = false
- uiManager.Instance().hideLoading()
- })
- }
- if(tools.platform==config.Platform.KS) {
- SdkUtil.videoAd.show().then(()=>{
- console.log("ks 广告加载完成");
- SdkUtil.isLookAd = true
- })
- }
- if(tools.platform==config.Platform.TT) {
- SdkUtil.videoAd.onLoad(() => {
- console.log("tt 广告加载完成");
- SdkUtil.isLookAd = true
- SdkUtil.videoAd.show();
- });
- }
- SdkUtil.videoAd.onClose((res) => {
- console.log('广告关闭=',res)
- uiManager.Instance().hideLoading()
- SdkUtil.isLookAd = false
- if(tools.platform!=config.Platform.WX) {
- SdkUtil.videoAd.destroy() // 微信销毁后,再次获取广告,不会显示
- }
- call_back && call_back(res)
- });
- SdkUtil.videoAd.onError((res) => {
- console.log('广告加载失败=',res)
- uiManager.Instance().hideLoading()
- let errorString = res.errCode + '-' + res.errMsg
- call_back && call_back({isEnded:false,errorString:errorString})
- SdkUtil.isLookAd = false
- if(tools.platform!=config.Platform.WX) {
- SdkUtil.videoAd.destroy() // 微信销毁后,再次获取广告,不会显示
- }
- });
- if(tools.platform==config.Platform.TT) {
- SdkUtil.videoAd.load()
- }
- }
- // 注册游戏互推组件
- public static registerGridGamePanel() {
- if(SdkUtil.is_devtools_platform()==true) { return }
- switch (tools.platform) {
- case config.Platform.TT:
- try{
- let height = SdkUtil.tt_systemInfo.safeArea.bottom
- let gridGamePanel_top = 575
- if(height) { gridGamePanel_top = height - 230 }
- // console.log('gridGamePanel_top=',gridGamePanel_top)
- // 单宫格gridCount: "one",可设置大小和位置 size:"large",position:{top:700,left:20},
- // 四宫格gridCount: "four",可设置大小,设置位置无实际作用 size:"large"
- // 九宫格gridCount: "nine",设置大小和位置无实际作用
- const gridGamePanel = tt.createGridGamePanel({
- gridCount: "one",
- size:"large",
- position:{top:gridGamePanel_top,left:10},
- query: {
- 'tte54693fb7a75fa0f02': "", //神秘的笔记
- },
- });
- SdkUtil.gridGamePanel = gridGamePanel
- } catch(error) {
- console.log('error=',error)
- }
- break;
- case config.Platform.WX:
- break;
- case config.Platform.KS:
- break;
- default:
- break;
- }
- }
- // 显示游戏互推组件
- public static showGridGamePanel() {
- if(SdkUtil.is_devtools_platform()==true) { return }
- if(!SdkUtil.gridGamePanel) {return}
- switch (tools.platform) {
- case config.Platform.TT:
- SdkUtil.gridGamePanel.show().then(() => {
- console.log("tt 展示游戏推荐组件成功");
- }).catch((err) => {
- console.error("展示游戏推荐组件失败", err);
- });
- break;
- case config.Platform.WX:
- break;
- case config.Platform.KS:
- break;
- default:
- break;
- }
- }
- // 隐藏互推游戏组件
- public static hideGridGamePanel() {
- if(SdkUtil.is_devtools_platform()==true) { return }
- if(!SdkUtil.gridGamePanel) {return}
- switch (tools.platform) {
- case config.Platform.TT:
- SdkUtil.gridGamePanel.hide()
- break;
- case config.Platform.WX:
- break;
- case config.Platform.KS:
- break;
- default:
- break;
- }
- }
- //------------------------------ 微信相关 ------------------------------//
- // 微信检查授权用户信息
- public static wxCheckAuthUserInfo(cb) {
- wx.getSetting({
- success(res) {
- if(res.authSetting['scope.userInfo'] === true) { // 已经授权
- cb(true)
- } else {
- cb(false)
- }
- },
- fail(err) {
- cb(false)
- }
- })
- }
- // 微信分享
- public static wxShare(title: string, imageUrl: string='', success_cb, fail_cb) {
- // console.log('wx分享=',title, 'imageUrl=',imageUrl)
- wx.showShareMenu({
- withShareTicket: true,
- // shareAppMessage(可以删除):显示分享给好友选项,shareTimeline(可以删除):显示分享至朋友圈选项
- // 可以只开启前者。如果要开启后者,则两者必须都开启才能生效。
- // menus: ['shareAppMessage', 'shareTimeline'],
- menus: ['shareAppMessage'],
- success:()=>{
- success_cb && success_cb()
- },
- fail:()=>{
- fail_cb && fail_cb()
- },
- });
- // 主动分享
- wx.shareAppMessage({
- title: title,
- imageUrl: imageUrl
- });
- }
- //------------------------------ 快手相关 ------------------------------//
-
- // 快手检查授权用户信息
- public static ksCheckAuthUserInfo(cb) {
- ks.getSetting({
- success(res) {
- if(res.authSetting['scope.userInfo'] === true) { // 已经授权
- cb(true)
- } else {
- cb(false)
- }
- },
- fail(err) {
- cb(false)
- }
- })
- }
- // 快手授权用户信息
- public static ksAuthUserInfo(cb) {
- ks.authorize({
- scope: "scope.userInfo",
- success: () => {
- console.log("ks授权获取用户信息成功");
- cb && cb(true)
- },
- fail: (error) => {
- console.log("ks授权获取用户信息失败: " + JSON.stringify(error));
- cb && cb(false)
- }
- });
- }
- // 快手加载分包
- public static ksLoadSubpackage(name:string,cb) {
- ks.loadSubpackage({
- name: name,
- success: function(res) {
- // console.log(`success->${name}=${res}`)
- cb && cb()
- },
- fail: function(err) {
- // console.log(`err->${name}=${err}`)
- }
- });
- }
- // 快手分享
- public static ksShare(title: string, imageUrl:string='', success_cb = null, fail_cb = null) {
- // console.log('ks分享=',title, 'imageUrl=',imageUrl)
- ks.shareAppMessage({
- success:(res)=>{ if(success_cb){ success_cb(res) } },
- fail:(err)=>{ if(fail_cb){ fail_cb(err) }}
- });
- }
- //------------------------------ 抖音相关 ------------------------------//
- // 分享
- public static ttShare(title:string, desc:string, sucess_cb=null, fail_cb=null) {
- if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
- tt.shareAppMessage({
- title: title,
- desc: desc,
- success() {
- console.log("分享成功")
- // this.showToast('分享成功')
- if(sucess_cb) { sucess_cb() }
- },
- fail(e) {
- console.log("分享失败",e)
- if(fail_cb) { fail_cb() }
- },
- });
- }
- }
- // 抖音添加快捷键(目前仅支持:抖音(Douyin) 和 抖音极速版(douyin_lite))
- public static ttAddShortcut(onSuccess:Function = null, onFail:Function = null) {
- if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
- if(SdkUtil.tt_systemInfo.appName=='Douyin'||SdkUtil.tt_systemInfo.appName=='douyin_lite') {
- if(SdkUtil.tt_systemInfo.brand=='Apple') {
- tt.addShortcut({
- success() {
- console.log("添加桌面成功");
- onSuccess && onSuccess(null)
- },
- fail(err) {
- console.log("添加桌面失败", err.errMsg);
- onFail && onFail(err)
- },
- });
- } else if(SdkUtil.tt_systemInfo.brand=='Android') {
- // 检测只支持安卓
- tt.checkShortcut({
- success(res) {
- console.log("检查快捷方式", res.status);
- if(res.status.exist==false||res.status.needUpdate==true) {
- tt.addShortcut({
- success() {
- console.log("添加桌面成功");
- onSuccess && onSuccess(null)
- },
- fail(err) {
- console.log("添加桌面失败", err.errMsg);
- onFail && onFail(err)
- },
- });
- }
- },
- fail(err) {
- console.log("检查快捷方式失败", err.errMsg);
- onFail && onFail(err)
- },
- });
- }
- }
- }
- }
- // 抖音注册信息
- public static ttRegisterInfo() {
- if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
- tt.onShow((res) => {
- console.log('tt.onShow =', res)
- // console.log('res=',res)
- if(res.scene == '021036' || res.scene == '101036') {
- SdkUtil.tt_isToEnterFromSidebar = true
- }
- if(res.scene.launch_from == 'homepage' && res.scene.location == 'sidebar_card') {
- SdkUtil.tt_isToEnterFromSidebar = true
- }
- });
- tt.onHide(()=>{
- console.log('tt.onHide')
- if(SdkUtil.isLookAd==false) {
- // statisticsManager.uploadRecordUserLevel(false)
- }
- })
-
- tt.checkScene({
- scene: "sidebar",
- success: (res) => {
- console.log("check scene success: ", res.isExist);
- if(res.isExist != undefined || res.isExist != null) {
- SdkUtil.tt_isSupportSidebar = res.isExist
- }
- },
- fail: (res) => {
- console.log("check scene fail:", res);
- }
- });
- let options = tt.getLaunchOptionsSync()
- console.log('getLaunchOptionsSync=', options)
- if(options.scene == '021036' || options.scene == '101036') {
- SdkUtil.tt_isToEnterFromSidebar = true
- }
- }
- }
- // 抖音检测是否显示奖励
- public static ttCheckSceneShowRewards():boolean {
- return SdkUtil.tt_isSupportSidebar
- }
- // 抖音检测是否从侧边栏进入
- public static ttCheckToEnterFromSidebar():boolean {
- return SdkUtil.tt_isToEnterFromSidebar
- }
- // 抖音导航到侧边栏场景
- public static ttNavToSidebarScene() {
- if(sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
- tt.navigateToScene({
- scene: "sidebar",
- success: (res) => {
- // console.log("navigate to scene success");
- },
- fail: (res) => {
- // console.log("navigate to scene fail: ", res);
- },
- });
- }
- }
- // 抖音开启屏幕录制
- public static ttStartScreenRecording() {
- if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
- return
- }
- if(this.tt_systemInfo.platform == 'devtools') {
- console.log('抖音模拟器')
- return
- }
- if(this.tt_isRecording==true) {
- this.ttStopScreenRecording()
- }
-
- if(!this.tt_gameRecorder) {
- this.tt_gameRecorder = tt.getGameRecorderManager()
- }
- this.tt_gameRecorder.start({duration: this.tt_totalRecord})
- this.tt_gameRecorder.onStart(()=> {
- this.tt_isRecording = true
- // console.log('GameRecorder onStart onStart onStart')
- })
- this.tt_gameRecorder.onStop((res)=> {
- // console.log('GameRecorder onStop onStop onStop=',res)
- this.tt_isRecording = false
- this.tt_recordVideoPath = res.videoPath
- })
- this.tt_gameRecorder.onError((e)=> {
- console.log('ttGameRecord error:',e)
- })
-
- }
- // 抖音关闭屏幕录制
- public static ttStopScreenRecording(isClearVideoPath:boolean = false) {
- if(sys.platform!=sys.Platform.BYTEDANCE_MINI_GAME) {
- return
- }
- if(this.tt_gameRecorder == null) {
- return
- }
- if(isClearVideoPath) {
- this.tt_recordVideoPath = "";
- }
-
- this.tt_gameRecorder.stop()
- }
- // 抖音获取屏幕录制视频文件
- public static ttGetScreenRecordingVideoPath():string {
- return this.tt_recordVideoPath;
- }
- // 抖音分享屏幕录制视频
- private static ttShareScreenRecordVideo(title: string, videoPath: string, onSuccess: Function = null, onFail: Function = null) {
- // console.log('tt_录制视频路径=',videoPath)
- if(videoPath.length<=0) {
- return
- }
- tt.shareAppMessage({
- title: title,
- templateId: config.TT_CONFIG.SHARE_RECORD_VIDEO_ID,
- channel: "video",
- extra: {
- videoTopics: [config.gameName],
- hashtag_list: ['小游戏','小程序'],
- videoPath: videoPath,
- withVideoId: true,
- },
- success: (res) => {
- console.log('抖音分享屏幕录制视频,成功=',res)
- onSuccess && onSuccess();
- },
- fail: (e) => {
- // 当前今日头条ios无法获得分享成功回调 if(res.platform === 'ios' && res.appName === 'Toutiao')
- console.log('抖音分享屏幕录制视频,失败=',e)
- onFail && onFail()
- }
- })
- }
- }
|