358 lines
10 KiB
Plaintext
358 lines
10 KiB
Plaintext
import { randomRange, view } from "cc";
|
|
import GlobalValue from "../Common/GlobalValue";
|
|
import SubManager from "../../Sub/SubManager";
|
|
import { I_SDK_UserInfo_Callback } from "./SDKManager";
|
|
|
|
//广告ID
|
|
const wx_banner_ad_unit_id = 'test'
|
|
const wx_splash_ad_unit_id = 'test'
|
|
const wx_video_ad_unit_id = 'test'
|
|
|
|
export class KSSDK{
|
|
|
|
static g_InGameScene = null; //游戏场景值
|
|
|
|
private wx_video:any = null;
|
|
private wx_splash:any = null;
|
|
private wx_banner:any = null;
|
|
private wx_video_callback:Function = null;
|
|
private wx_video_tag:number = null;
|
|
private wx_gameClub:any = null; //游戏圈按钮
|
|
|
|
private ShareSTime: any;
|
|
private ShareCb: any; //分享回调
|
|
private shareTxt = ["分享失败", "请分享到一个群", "请分享给一位好友", "分享过于频繁"]
|
|
|
|
//初始化
|
|
init(cb: Function = null) {
|
|
// 保持屏幕常亮
|
|
// window['ks'].setKeepScreenOn({ keepScreenOn: true });
|
|
|
|
//获取小游戏冷启动时的参数 热启动参数通过 ks.onShow 接口获取。
|
|
let launchOption = window['ks'].getLaunchOptionsSync();
|
|
KSSDK.g_InGameScene = launchOption.from;
|
|
console.log('init 快手 - 进入游戏的场景值', launchOption);
|
|
|
|
// //显示分享按钮
|
|
// window['ks'].showShareMenu({
|
|
// withShareTicket: true,
|
|
// menus: ["shareAppMessage", "shareTimeline"],
|
|
// });
|
|
// window['ks'].onShareAppMessage(() => {
|
|
// return this.shareInfo();
|
|
// });
|
|
// window['ks'].onShareTimeline(() => {
|
|
// return this.shareInfo();
|
|
// })
|
|
|
|
//小游戏回到前台
|
|
window['ks'].onShow(this.onWxShow.bind(this));
|
|
//小游戏被隐藏 记个时间
|
|
window['ks'].onHide(() => {
|
|
if (this.ShareCb) {
|
|
this.ShareSTime = new Date().getTime();
|
|
console.log("分享时间差 记录:", this.ShareSTime);
|
|
}
|
|
});
|
|
|
|
this.init_video(); //初始化视频广告
|
|
this.init_splash(); //初始化插屏广告
|
|
this.init_banner(); //初始化Banner广告 弹窗广告
|
|
cb && cb();
|
|
}
|
|
|
|
|
|
//小游戏回到前台
|
|
onWxShow(data: any) {
|
|
console.log("快手 onShow回调:", data)
|
|
if (data) {
|
|
//启动小游戏时传入的参数 object
|
|
if (data.query) {
|
|
GlobalValue.ShareTocusid = data.query.ShareCode
|
|
}
|
|
//游戏启动场景 string
|
|
if (data.from) {
|
|
|
|
}
|
|
}
|
|
|
|
if (this.ShareCb) {
|
|
if (this.ShareSTime) {
|
|
let nT = new Date().getTime();
|
|
console.log("分享时间差 计算:",nT, this.ShareSTime);
|
|
if (nT - this.ShareSTime > 1500) {
|
|
let cb = this.ShareCb.succ
|
|
cb && cb()
|
|
} else {
|
|
this.tostErr()
|
|
}
|
|
}
|
|
this.ShareCb = null;
|
|
}
|
|
}
|
|
//分享失败
|
|
tostErr() {
|
|
let rint = Math.floor( randomRange(0, this.shareTxt.length - 1) )
|
|
SubManager.ShowPrompt(this.shareTxt[rint])
|
|
if (this.ShareCb) {
|
|
let cb = this.ShareCb.fail
|
|
cb && cb()
|
|
}
|
|
}
|
|
|
|
|
|
/**获取用户授权状态
|
|
* @param autoKey string 授权类型 userInfo=用户信息,对应接口wx.getUserInfo
|
|
*/
|
|
checkAutoSetting(autoKey:string, cb:Function = null) {
|
|
window['ks'].getSetting({
|
|
success(res:any) {
|
|
console.log("快手 - 检测权限状态 succ:", res)
|
|
if (autoKey == "userInfo" && res.authSetting['scope.userInfo']) {
|
|
cb && cb(true)
|
|
} else {
|
|
cb && cb(false)
|
|
}
|
|
},
|
|
fail(res:any) {
|
|
console.log("快手 - 检测权限状态 fail:", res)
|
|
cb && cb(false)
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 获取个人信息
|
|
*/
|
|
getUserInfo(cb:I_SDK_UserInfo_Callback = null) {
|
|
var self = this;
|
|
window['ks'].getSetting({
|
|
success(res:any) {
|
|
console.log("快手 - 获取授权信息成功:", res);
|
|
let isScope = res.authSetting['scope.userInfo']
|
|
if (isScope) {
|
|
window['ks'].getUserInfo({
|
|
success: (res2) => {
|
|
console.log("快手 - 获取用户信息成功:", res2);
|
|
cb && cb({avatarUrl:res2.userInfo.avatarUrl, nickName:res2.userInfo.nickName})
|
|
},
|
|
fail: (error) => {
|
|
console.log("快手 - 获取用户信息失败:", error);
|
|
}
|
|
})
|
|
} else {
|
|
//向用户发起授权请求
|
|
window['ks'].authorize({
|
|
scope: 'scope.userInfo',
|
|
success: (res) => {
|
|
console.log("快手 - 发起授权请求成功:", res);
|
|
self.getUserInfo(cb)
|
|
},
|
|
fail: (error) => {
|
|
console.log("快手 - 发起授权请求失败: ", error);
|
|
}
|
|
})
|
|
}
|
|
},
|
|
fail: (error) => {
|
|
console.log("快手 - 获取授权信息失败: ", error);
|
|
// cb && cb({avatarUrl:"", nickName:""})
|
|
}
|
|
})
|
|
}
|
|
|
|
|
|
//微信登录
|
|
login(cb: Function = null) {
|
|
window['ks'].login({
|
|
success: (res) => {
|
|
console.log('快手 - 登录成功', res.code);
|
|
cb && cb({ platform:1, code: res.code });
|
|
},
|
|
fail(res: any) {
|
|
console.log(`快手 login 调用失败`, res);
|
|
cb && cb({ platform:1, code: null });
|
|
},
|
|
});
|
|
}
|
|
|
|
//初始化视频广告
|
|
private init_video() {
|
|
this.wx_video = window['ks'].createRewardedVideoAd({
|
|
adUnitId: wx_video_ad_unit_id
|
|
});
|
|
this.wx_video.onError((err) => {
|
|
console.log('onError event emit', err);
|
|
});
|
|
this.wx_video.onClose(res => {
|
|
console.log('onClose event emit',res);
|
|
// 用户点击了【关闭广告】按钮
|
|
// 小于 2.1.0 的基础库版本,res 是一个 undefined
|
|
if ((res && res.isEnded) || res === undefined) {
|
|
// 正常播放结束,可以下发游戏奖励
|
|
if (this.wx_video_callback) {
|
|
this.wx_video_callback(this.wx_video_tag);
|
|
}
|
|
}
|
|
else {
|
|
// 播放中途退出,不下发游戏奖励
|
|
//wx_video_callback(-1);
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
//初始化插屏广告
|
|
private init_splash() {
|
|
// 创建插屏广告实例,提前初始化
|
|
if (window['ks'].createInterstitialAd){
|
|
this.wx_splash = window['ks'].createInterstitialAd({
|
|
adUnitId: wx_splash_ad_unit_id
|
|
});
|
|
this.wx_splash.onError((err) => {
|
|
console.log('splash onError event emit', err);
|
|
});
|
|
this.wx_splash.onClose(res => {
|
|
console.log('splash onClose event emit', res);
|
|
});
|
|
}
|
|
}
|
|
|
|
//初始化Banner
|
|
private init_banner() {
|
|
|
|
}
|
|
|
|
//显示视频广告
|
|
show_video(callback:Function, tag:number) {
|
|
if (wx_video_ad_unit_id == 'test') {
|
|
console.log('未接入广告ID,视频广告请求直接返回');
|
|
callback && callback();
|
|
return;
|
|
}
|
|
if (this.wx_video) {
|
|
this.wx_video_callback = callback;
|
|
this.wx_video_tag = tag;
|
|
let p = this.wx_video.show()
|
|
p.then(function(result){
|
|
// 激励视频展示成功
|
|
console.log(`快手 show rewarded video ad success, result is ${result}`)
|
|
}).catch(function(error){
|
|
// 激励视频展示失败
|
|
console.log(`快手 show rewarded video ad failed, error is ${error}`)
|
|
})
|
|
|
|
// this.wx_video.load()
|
|
// .then(() => {
|
|
// this.wx_video.show()
|
|
// .catch(err => {
|
|
// this.wx_video.load()
|
|
// .then(() => this.wx_video.show())
|
|
// })
|
|
// })
|
|
}
|
|
}
|
|
|
|
//显示插屏
|
|
show_splash() {
|
|
|
|
if (this.wx_splash) {
|
|
let p = this.wx_splash.show()
|
|
p.then(function(result){
|
|
// 插屏广告展示成功
|
|
console.log(`快手 show interstitial ad success, result is ${result}`)
|
|
}).catch(function(error){
|
|
// 插屏广告展示失败
|
|
console.log(`快手 show interstitial ad failed, error is ${error}`)
|
|
if (error.code === -10005) {
|
|
// 表明当前app版本不支持插屏广告,可以提醒用户升级app版本
|
|
}
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
//显示Banner
|
|
show_banner() {
|
|
if (this.wx_banner) {
|
|
this.wx_banner.show(); //banner 默认隐藏(hide) 要打开
|
|
}
|
|
}
|
|
|
|
//隐藏Banner
|
|
hide_banner() {
|
|
if (this.wx_banner) {
|
|
this.wx_banner.hide(); //banner 默认隐藏(hide) 要打开
|
|
}
|
|
}
|
|
|
|
|
|
//分享信息
|
|
shareInfo() {
|
|
return {
|
|
templateId: undefined, //分享模版id,不传走默认分享文案
|
|
//查询字符串,从这条转发消息进入后,可通过 ks.getLaunchOptionsSync() 或 ks.onShow() 获取启动参数中的 query。必须是 key1=val1&key2=val2 的格式。
|
|
query: "ShareCode=" + "user_default", //ModelPlayer.I.getPlayerId(),
|
|
}
|
|
}
|
|
//主动拉起分享
|
|
show_share(rewardCB?:Function) {
|
|
console.log("主动拉起分享");
|
|
let info:any = this.shareInfo();
|
|
info.succ = rewardCB;
|
|
info.success = null;
|
|
info.fail = null;
|
|
this.ShareCb = info;
|
|
window['ks'].shareAppMessage(info);
|
|
}
|
|
|
|
//获取设备分辨率
|
|
getWindowSize() {
|
|
let windowinfo = window['ks'].getSystemInfoSync();
|
|
return {width:windowinfo.screenWidth, height: windowinfo.screenHeight};
|
|
}
|
|
|
|
//显示游戏圈
|
|
show_gameClub(leftRatio, topRatio, widthRatio, heightRatio) {
|
|
if (true) {
|
|
console.log("显示游戏圈,快手暂不支持");
|
|
return
|
|
}
|
|
// if (this.wx_gameClub) {
|
|
// this.wx_gameClub.show();
|
|
// }else {
|
|
// let windowinfo = window['ks'].getSystemInfoSync();
|
|
// let x = windowinfo.screenWidth * leftRatio;
|
|
// let y = windowinfo.screenHeight * topRatio;
|
|
// let w = windowinfo.screenWidth * widthRatio; //按钮宽高
|
|
// let h = windowinfo.screenHeight * heightRatio;
|
|
|
|
// console.log("游戏圈按钮配置:", windowinfo, leftRatio, topRatio, widthRatio, heightRatio, x, y, w, h);
|
|
// this.wx_gameClub = window['ks'].createGameClubButton({
|
|
// type: 'string',
|
|
// text: '',
|
|
// // type: 'image',
|
|
// icon: 'green',
|
|
// style: {
|
|
// left: x, //这个坐标对应UI上的按钮时,按钮要勾选适配
|
|
// top: y,
|
|
// width: w,
|
|
// height: h,
|
|
// },
|
|
// })
|
|
// }
|
|
}
|
|
//隐藏游戏圈
|
|
hide_gameClub() {
|
|
console.log("隐藏游戏圈");
|
|
if (this.wx_gameClub) {
|
|
this.wx_gameClub.hide();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|