import { I_SDK_UserInfo, I_SDK_UserInfo_Callback } from "./SDKManager" //广告ID const AD_AWARD = "xxxxx" const AD_SPLASH = "xxxxx" const AD_BANNER = "xxxxx" const AD_BANNER_INTERVAL = 30 const AD_BANNER_HEIGHT = 130 export class ByteDanceSDK{ private bd_video:any = null; private bd_splash:any = null; private bd_banner:any = null; private bd_video_callback:Function = null; private bd_video_tag:number = null; //初始化 init(cb: Function = null) { console.log("init 抖音 初始化...") // 保持屏幕常亮 window["tt"].setKeepScreenOn({ keepScreenOn: true, success(res) { }, fail(res) { }, }); this.init_video(); this.init_splash(); this.init_banner(); cb && cb(); } //检查权限 checkAutoSetting(autoKey:string, cb:Function = null) { // cb && cb(true); if (autoKey == "userInfo") { autoKey = "scope.userInfo" } window['tt'].authorize({ scope: autoKey, success(res:any) { console.log("抖音 - 检测权限状态 succ:", JSON.stringify(res)) if (autoKey == "scope.userInfo" && res.data['scope.userInfo']) { cb && cb(true) } else { cb && cb(false) } }, fail(res:any) { console.log("抖音 - 检测权限状态 fail:",JSON.stringify(res)) cb && cb(false) } }) } //获取用户信息 getUserInfo(cb:I_SDK_UserInfo_Callback = null) { // cb && cb({ nickName:"", avatarUrl:"" }); //getUserProfile报错未找到 window["tt"].getUserInfo({ force: false, //当宿主未登录时,是否强制拉起登录框 success(res) { console.log("抖音 - 获取用户信息成功:", res); cb && cb({avatarUrl:res.userInfo.avatarUrl, nickName:res.userInfo.nickName}) //encryptedData }, fail(res) { console.log("抖音 - 获取用户信息失败", res); }, }) } //登录 login(cb: Function = null) { // cb && cb({ platform:3, code: null }); window["tt"].login({ force: false, //未登录时, 是否强制调起登录框 success(res) { console.log(`抖音 - 登录成功 code=${res.code}, anonymousCode=${res.anonymousCode}`); cb && cb({ platform:3, code: res.code }); }, fail(res) { console.log(`抖音 - 登录失败`, res); cb && cb({ platform:3, code: null }); }, }) } private init_banner() { if (!window["tt"] || !window["tt"].createBannerAd) return const systemInfo = window["tt"].getSystemInfoSync(); //if (systemInfo.appName.toUpperCase() == 'DOUYIN') return //if (systemInfo.appName.toUpperCase() == 'TOUTIAO' && systemInfo.platform == "ios") return if (systemInfo.appName.toUpperCase() == 'DOUYIN' || systemInfo.platform == "ios") return const { windowWidth, windowHeight } = window["tt"].getSystemInfoSync(); var targetBannerAdWidth = 128+ 100 var targetBannerAdHeight = AD_BANNER_HEIGHT // 创建一个居于屏幕底部正中的广告 this.bd_banner = window["tt"].createBannerAd({ adUnitId: AD_BANNER, adIntervals: AD_BANNER_INTERVAL, style: { width: targetBannerAdWidth, left:(windowWidth - targetBannerAdWidth) / 2, top: windowHeight - targetBannerAdHeight }, }); // 尺寸调整时会触发回调 // 注意:如果在回调里再次调整尺寸,要确保不要触发死循环!!! this.bd_banner.onResize(size => { this.bd_banner.style.left = (windowWidth - size.width) / 2; this.bd_banner.style.top = windowHeight - size.height; }); // bd_banner.onLoad(() => { // bd_banner // .show() // .then(() => { // console.log("init_banner 广告显示成功"); // }) // .catch((err) => { // console.log("init_banner 广告组件出现问题", err); // }); // }) } private init_splash() { this.preload_splash() } private preload_splash() { if (!window["tt"] || !window["tt"].createInterstitialAd) return if (this.bd_splash) { if (this.bd_splash['custom_show_stautus']) { this.bd_splash.destroy() this.bd_splash = null } else { if (!this.bd_splash['custom_load_stautus']) { this.bd_splash .load() .catch(err => { console.log("preload_splash err:",err); }) } } } if (this.bd_splash) return // 创建插屏广告实例,会自动进行一次load this.bd_splash = window["tt"].createInterstitialAd({ adUnitId: AD_SPLASH }) this.bd_splash.onLoad(function(){ this.bd_splash['custom_load_stautus'] = true }) this.bd_splash.onClose(function(){ this.bd_splash.destroy() this.bd_splash = null this.preload_splash() }) this.bd_splash.onError(function(err){ console.log("bd_splash err:",err) }) } private init_video() { if (!window["tt"] || !window["tt"].createRewardedVideoAd) return // 预加载视频广告 // let global_this = this // function func_preload_wx_video() { // global_this.bd_video // .load() // .catch(err => { // }); // } this.bd_video = window["tt"].createRewardedVideoAd({ adUnitId: AD_AWARD, }); this.bd_video.onClose(res => { if (res.isEnded) { if (this.bd_video_callback) { this.bd_video_callback(this.bd_video_tag); } } //func_preload_wx_video() }); this.bd_video.onError((err) => { //func_preload_wx_video() }); //func_preload_wx_video() } //显示视频广告 show_video(callback:Function, tag:number) { if (!this.bd_video) return this.bd_video_callback = callback; this.bd_video_tag = tag this.bd_video .load() .then(() => { this.bd_video.show() }) .catch(err => { }); } //显示插屏 show_splash() { //已加载 未展示 if (this.bd_splash && this.bd_splash['custom_load_stautus'] && !this.bd_splash['custom_show_stautus']) { this.bd_splash.show().then(() => { this.bd_splash['custom_show_stautus'] = true console.log("插屏广告展示成功"); }) } else { console.log("插屏广告展示失败"); this.preload_splash() } } //显示Banner show_banner() { if (this.bd_banner) { this.bd_banner.show(); //banner 默认隐藏(hide) 要打开 } } //隐藏Banner hide_banner() { if (this.bd_banner) { this.bd_banner.hide(); //banner 默认隐藏(hide) 要打开 } } //移除 clear_banner() { if (this.bd_banner) { this.bd_banner.destroy(); //banner 默认隐藏(hide) 要打开 this.bd_banner = null; } } //主动拉起分享 show_share(rewardCB?:Function) { rewardCB && rewardCB() } //获取设备分辨率 getWindowSize() { return {width:screen.width, height: screen.height}; } //显示游戏圈 show_gameClub(leftRatio, topRatio, widthRatio, heightRatio) { } //隐藏游戏圈 hide_gameClub() { } //检查是否支持base64音频播放 checkSupportBase64Audio() { return false; } //播放base64音频 playBase64Audio(base64, loop) { if (!this.checkSupportBase64Audio()) { return; } let substring = base64; let qianzhui = "data:audio/x-wav;base64," if(base64.startsWith(qianzhui)) { substring = base64.substring(qianzhui.length, base64.length) } const backgroundAudioManager = window['tt'].getBackgroundAudioManager() const audioPath = window['wx'].env.USER_DATA_PATH + '/ordernew.mp3' const fs = window['tt'].getFileSystemManager(); fs.writeFile({ filePath: audioPath, data: substring, encoding: 'base64', success(res) { backgroundAudioManager.title = '回复' backgroundAudioManager.src = audioPath }, }) } }