declare var tt: any; import BaseSdk, { RecorderState } from "./BaseSdk"; import { find, Node, Size, UITransform, v3, Vec3 } from "cc"; export default class TTSdk extends BaseSdk { private videoAd: any; private callback: any; private target: any; private bannerAd: any; private interstitialAd: any; private customAd: any; private sidebarExist:boolean=false; private fromSidebar:boolean=false; /** * 初始化 * @param callback 初始化成功后回调 * @param target 监听对象 */ public init(callback: Function, target: Object): void { console.log("TTSdk init"); // tt.onShow((res) => { // console.log("启动参数:", res?.query); // console.log("来源信息:", res?.refererInfo); // console.log("场景值:", res?.scene); // console.log("启动场景字段:", res.launch_from, ", ", res.location); // this.fromSidebar = res.launch_from=='homepage' && res.location=='sidebar_card'; // callback?.call(target,res); // }); // tt.checkScene({ // scene: "sidebar", // success: (res) => { // console.log("check scene success: ", res); // //成功回调逻辑 // this.sidebarExist=res.isExist; // }, // fail: (res) => { // console.log("check scene fail:", res); // //失败回调逻辑 // this.sidebarExist=false; // } // }); } public goToSidebar():void{ tt.navigateToScene({ scene: "sidebar", success: (res) => { console.log("navigate to scene success"); // 跳转成功回调逻辑 }, fail: (res) => { console.log("navigate to scene fail: ", res); // 跳转失败回调逻辑 }, }); } public checkHasSidebar():boolean{ return this.sidebarExist; } public checkFromSidebar():boolean{ return this.fromSidebar; } /** * 登录 * @param callback 登录成功后回调 * @param target 监听对象 */ public login(callback: Function, target: Object): void { console.log("ttSdk login"); } /** * 登出 */ public logout(): void { console.log("ttSdk logout"); } /** * 退出 */ public exit(): void { console.log("ttSdk exit"); } /** * 切换账号 */ public switchLogin(): void { console.log("ttSdk switchLogin"); } public setOpenScore(score:number):void{ } public getOpenRank():void{ } /** * 数据上报 * @param param 参数 */ public report(...param: any[]): void { console.log("ttSdk report"); } /** * 数据上报 * @param param 参数 */ public pay(...param: any[]): void { console.log("ttSdk pay"); } showLoading(mess={title: "请稍后..."}){ tt.showLoading(mess); } hideLoading(param ={ success(res) { console.log(`${res}`); }, fail(res) { console.log(`hideLoading调用失败`); }, }) { tt.hideLoading(param); } /** * 播放广告 */ playRewardAd(callback: Function, target: any): void { this.callback = callback; this.target = target; this.createRewardAd(); //用户触发广告后,显示激励视频广告 this.videoAd.show().catch(() => { // 失败重试 this.videoAd.load() .then(() =>{ this.videoAd.show() } ) .catch(err => { tt.hideLoading({ success(res) { console.log(`${res}`); }, fail(res) { console.log(`hideLoading调用失败`); }, }); console.warn('激励视频 广告显示失败') this.onRewardAdCallback(false); }) }) } createRewardAd() { if (!this.videoAd) { this.videoAd = tt.createRewardedVideoAd({ adUnitId: "2d5f6gan71lm1bjf1l",//广告位 id,后续可以在平台基于广告位id看数 multiton:false,//是否开启再得广告模式(只支持安卓系统的抖音和抖音极速版) multitonRewardMsg:['更多奖励1', '更多奖励2', '更多奖励3'],//再得广告的奖励文案,玩家每看完一个广告都会展示multiton为true时必填 multitonRewardTimes:0,//额外观看广告的次数,合法的数据范围为1-4,multiton为true时必填 progressTip:false,//是否开启进度提醒,开启时广告文案为【再看N个获得xx】,关闭时为【 再看1个获得xx】。 N表示玩家当前还需额外观看广告的次数。 }) this.videoAd.onClose(res => { tt.hideLoading({ success(res) { console.log(`${res}`); }, fail(res) { console.log(`hideLoading调用失败`); }, }); // 用户点击了【关闭广告】按钮 if (res && res.isEnded || res === undefined) { console.log(res); // 正常播放结束,可以下发游戏奖励 this.onRewardAdCallback(true); //if (res.count) { //在支持多例模式的版本上会返回该字段,并且是否返回该字段与multiton是否为true无关 //判断观看了几次广告 //} } else { // 播放中途退出,不下发游戏奖励 this.onRewardAdCallback(false); } }) this.videoAd.onError(err => { console.warn(err); tt.hideLoading({ success(res) { console.log(`${res}`); }, fail(res) { console.log(`hideLoading调用失败`); }, }); this.onRewardAdCallback(false); }) } } onRewardAdCallback(success) { if (this.callback) { this.callback.call(this.target, success); if (success) { this.videoAd.load(); } this.callback = null; } } /** * 显示banner广告 */ showBannerAd(show: boolean): void { if (show) { // 在适合的场景显示 Banner 广告 this.createBannerAd(); this.bannerAd.show(); } else { this.bannerAd.hide(); this.bannerAd.destroy(); this.bannerAd = null; this.createBannerAd(); } } createBannerAd() { if (!this.bannerAd) { let sysInfo = tt.getSystemInfoSync(); //创建 Banner 广告实例,提前初始化 var width = 320; this.bannerAd = tt.createBannerAd({ adUnitId: 'adunit-', adIntervals: 30, // 自动刷新频率不能小于30秒 style: { left: sysInfo.screenWidth / 2 - width / 2, top: sysInfo.screenHeight, width: width } }) this.bannerAd.onResize(res => { this.bannerAd.style.top = sysInfo.screenHeight - this.bannerAd.style.realHeight; }) this.bannerAd.onError(err => { console.log(err) }) } } createInterstitialAd() { // 创建插屏广告实例,提前初始化 if (tt.createInterstitialAd && !this.interstitialAd) { this.interstitialAd = tt.createInterstitialAd({ adUnitId: 'adunit-' }) this.interstitialAd.onClose(res => { this.interstitialAd = null; this.createInterstitialAd(); }) } } showInterstitialAd() { // 在适合的场景显示插屏广告 this.createInterstitialAd(); if (this.interstitialAd) { this.interstitialAd.show().catch((err) => { console.error(err) }) } } /** * 显示自定义广告 */ showCustomAd(show, top?, left?): void { // console.log(top, left); if (!this.customAd) { this.customAd = tt.createCustomAd({ adIntervals: 30, adUnitId: "adunit-", style: { fixed: true, left: left, top: top, }, }) } if (show) { this.customAd.show(); } else { this.customAd.destroy(); this.customAd = null; } } /** * 主动拉起转发 */ shareAppMessage(title: string = "", imageUrlId: string = "", imageUrl: string = "", query: any = null, shareCallback: any = null): void { var data = { channel:"invite", title: title, imageUrlId: imageUrlId, imageUrl: imageUrl, query: "", success() { console.log("分享成功"); tt.showModal({ title: "分享成功", content: "", }); }, fail(e) { console.log("分享失败"); }, }; tt.shareAppMessage(data); } // recorderStart(duration:number=30):void{ if(this.recordState!=RecorderState.STOP) return; const recorder = tt.getGameRecorderManager(); recorder.onStart((res) => { console.log("录屏开始"); this._recordState=RecorderState.START; }); recorder.onStop((res) => { // if(this.recordState != RecorderState.STOP){ // recorder.start({ // duration: duration, // isMarkOpen: false // }); // }else{ console.log("录屏结束"); this._videoPath=res.videoPath||this._videoPath; console.log(res.videoPath); // } }); recorder.start({ duration: duration, isMarkOpen: false }); } recorderResume():void{ if(this._recordState !=RecorderState.PAUSE ) return; console.log("录屏继续"); this._recordState = RecorderState.START const recorder = tt.getGameRecorderManager(); recorder.resume(); } private _videoPath:string=""; recorderStop():void{ if(this._recordState == RecorderState.STOP)return; console.log("录屏结束",tt.getGameRecorderManager); this._recordState = RecorderState.STOP if(tt.getGameRecorderManager){ const recorder = tt.getGameRecorderManager(); recorder.stop(); } } recorderPause(){ if(this._recordState != RecorderState.START ) return; console.log("录屏暂停"); this._recordState=RecorderState.PAUSE const recorder = tt.getGameRecorderManager(); recorder.pause(); } shareRecord(title: string = "",desc:string="",path:string="",topics:string[]=["话题1","话题2"]):void{ tt.shareAppMessage({ channel: "video", title: title, desc: desc, imageUrl: "", templateId: "", // 替换成通过审核的分享ID query: "", extra: { videoPath:path=="" ? this._videoPath:path, // 可替换成录屏得到的视频地址 videoTopics: topics, withVideoId: true, }, success(res) { console.log(res.videoId); console.log("分享视频成功"); /*tt.showModal({ title: "分享成功", content: "", });*/ }, fail(e) { console.log("分享视频失败"); /*tt.showModal({ title: "分享失败", content: JSON.stringify(e), });*/ }, }); } // vibrateShort(param: any) { tt.vibrateShort({ success:param.success, fail:param.fail, complete:param.complete }) } posMessge(messge?) { // let mess = { event: 'getRank', type: 'engine' } // if (messge) { // messge = { ...mess, ...messge } // } // messge = messge || mess // console.log("messge") // console.log(messge) // console.log(tt.postMessage) // console.log("messge__end") // const openDataContext = tt.getOpenDataContext(); // console.log("openDataContext--->",openDataContext) // openDataContext.postMessage(messge) // let env = wx // if (env) { // console.log('Message posted'); // env.getOpenDataContext().postMessage({ // event:'setValue',type:'engine' // }); // } } showRankList(param){ tt.getImRankList(param); } setImRankData(param: any){ tt.setImRankData(param); } getMenuButtonBoundingClientRect() { return tt.getMenuButtonLayout() } openSetting(res){ // tt.showDouyinOpenAuth({ // scopes: { // im: 0, // 用户无法取消 // "im.media": 2, // }, // success(res) { // console.log("success", res); // }, // fail(res) { // console.log("fail", res); // }, // complete(res) { // console.log("complete", res); // }, // }); tt.openSetting(res) } getUserInfo(reportFun:any, node?: Node,btnCb?: any) { console.log("======================") tt.getSetting({ success(res) { if (res.authSetting['scope.userInfo'] === true) { tt.getUserInfo({ success(res) { reportFun(res) }, fail(res) { console.log(`getUserInfo 调用失败`, res.errMsg); }, }); } else { console.log(">>>>>>>>>>>>>>>>>>>") tt.authorize({ scope: "scope.userInfo", success() { // 用户同意授权用户信息 tt.getUserInfo({ success(res) { reportFun(res) }, fail(res) { console.log(`getUserInfo 调用失败`, res.errMsg); }, }); }, }); } }, }); } // url = "https://analytics.oceanengine.com/api/v2/conversion" // options // sendAnalytics(param:{event_type:string,outer_event_id?:number}){ // let url = this.url // // 上报数据 // if(!this.options){ // this.options = tt.getLaunchOptionsSync(); // } // var options = this.options||{} // console.log("options==>",options) // let body = { // "context": { // "ad": { // "callback": options?.query?.clickid,//callback 这里需要填写的就是从启动参数里获取的 clickid // } // }, // "timestamp": ch.date.now(), // ...param, // } // request({url:url, method: 'POST',data:body}).then((res)=>{ // console.log("res==>",res) // }); // } awemeUserProfile(callback: Function){ tt.openAwemeUserProfile({ success: (res) => { callback(true,res) }, fail: (res) => { callback(false,res) } }) } checkFollowAwemeState(callback: Function){ tt.checkFollowAwemeState({ success(res) { callback(true,res) }, fail(res) { callback(false,res) }, }); } getWindowInfo() { tt.getSystemInfoSync() } }