laya小游戏平台SDK——login篇

平台:微信、头条、qq都是获取code后,使用qg.request方法请求接口;OPPO和VIVO获取token,其中VIVO使用qg.request请求接口,OPPO使用XMLHttpRequest请求接口(注意不能将方法封装,封装后无法请求)

具体实现:

const APPID = '30247892'; // 头条:ttb920cf9d260afcd1;微信:wx9c403e6edfea35c8; OPPO: 30247892
const GAME_VERSION = '1.1.4';
const VERSION = 1;
const ALIAS = 'vivo'; // 头条:toutiao; 微信:wx; OPPO: oppo; VIVO: vivo

var Yw_wechat = {
server_ini_data:{},
jump_out:function (obj) {
let operate = get_operate('statistic.jump_out');
let data = {
'operate':operate,
'to_appid':obj.toAppid,
'appid':obj._id,
'send_time':Date.now()
};
ajax_post({
data:data
});
},
start:function (obj,__callBack) {
console.log('调用start')
let operate = get_operate('statistic.start');
let data = {
'operate':operate,
'token':Yw_wechat.server_ini_data.token,
'appid':APPID,
'send_time':Date.now(),
'game_version':GAME_VERSION,
};
console.log('start参数',data)
ajax_post({
data:data
// success:__callBack
});
},
login:function(obj,__callBack) {
console.log('login_obj',obj)
obj.login({
success: (res) => {
console.log("登录成功", res);
switch (ALIAS) {
case "oppo":
loginOppo(obj,res,__callBack)
break;
case "vivo":
loginVivo(obj,res,__callBack);
break;
default:
loginYw(obj,res.code,__callBack);
break;
}

},
fail: (res) => {
if (__callBack) __callBack(false, res);
console.log("登录失败", res);
}
})
},
init_app:function (obj,__callBack) {
if (!this.server_ini_data.token) {
method = function (_obj) {
Yw_wechat.start();
__callBack(_obj);
}
this.login(obj,method);
} else {
this.start();
}
},
switch:function (__callBack) {
let operate = get_operate('game.switch');
let data = {
'operate':operate,
'appid':APPID,
'send_time':Date.now(),
'game_version':GAME_VERSION,
};
let re = ajax_post({
data : data,
success:__callBack
});
},
customise:function (__callBack) {
let operate = get_operate('game.customise');
let data = {
'operate':operate,
'appid':APPID,
'send_time':Date.now(),
'game_version':GAME_VERSION,
};
let re = ajax_post({
data : data,
success:__callBack
});
},
get_switch:function () {
return this.server_ini_data.switch;
console.log('get_switch', this.server_ini_data.switch)
},
get_customise:function () {
return this.server_ini_data.customise;
},
code_login:function(obj,__callBack){
obj.login({
success: (res) => {
console.log("登录成功", res);
loginYw(obj,res.code,__callBack)
},
fail: (res) => {
if (callback) callback(false, res);
console.log("登录失败", res);
}
})
}
};

window.Yw_wechat = Yw_wechat;
/**登录Yw*/
function loginYw(obj,code,__callBack) {
let operate = get_operate('user.login');
let data = {
code: code,
operate: operate,
appid: APPID,
send_time:Date.now(),
game_version:GAME_VERSION,
}
if (obj) {
obj.request({
url: "",// 请求接口地址
data: data,
method: "post",
success(res) {
console.log('调用成功',res)
if (res.data.data.token) {
Yw_wechat.server_ini_data.token = res.data.data.token;
Yw_wechat.server_ini_data.switch = res.data.data.init.switch;
Yw_wechat.server_ini_data.customise = res.data.data.init.customise;
__callBack(Yw_wechat);
}
}
})
}
}
function loginVivo(obj,res,__callBack) {
let operate = get_operate('user.login');
let data = {
"token":res.token,
"operate":operate,
"appid":APPID,
"send_time":Date.now(),
"game_version":GAME_VERSION,
}
console.log('vivo参数',data)
if (obj) {
obj.request({
url: "",// 请求接口地址
data: data,
method: "post",
success(res) {
console.log('vivo调用成功',res)
if (res.data.data.token) {
console.log("res.data.data.token:", res.data.data.token)
console.log('init数据',Yw_wechat.server_ini_data)
Yw_wechat.server_ini_data.token = res.data.data.token;
Yw_wechat.server_ini_data.switch = res.data.data.init.switch;
Yw_wechat.server_ini_data.customise = res.data.data.init.customise;
__callBack(Yw_wechat);
}
}
})
}
}

function loginOppo(obj,res,__callBack) {
let operate = get_operate('user.login');
let data = {
"token":res.token,
"operate":operate,
"appid":APPID,
"send_time":Date.now(),
"game_version":GAME_VERSION,
}
console.log('OPPO参数',data)
if (obj) {
var url = '' // 请求接口地址
var xhr = new XMLHttpRequest();
xhr.open('post', url)
xhr.setRequestHeader('content-type', 'application/json');
xhr.send(JSON.stringify(data));
xhr.onreadystatechange = function() {
console.log('请求成功了',xhr.readyState);
if (xhr.readyState == 4 && xhr.status == 200) {
//如果可以进入if,则表示请求成功并且数据可用
console.log('请求成功了,可使用',xhr.responseText,'result',xhr.response);
let res = JSON.parse(xhr.responseText)
Yw_wechat.server_ini_data.token = res.data.token;
Yw_wechat.server_ini_data.switch = res.data.init.switch;
Yw_wechat.server_ini_data.customise = res.data.init.customise;
__callBack()
}
}
}
}

function get_operate(action) {
return ALIAS + "." + VERSION + "." + action;
}

function ajax_post(obj) {
if (typeof(obj.url) == 'undefined') {
url = API_URL
}

if (typeof(obj.data) == 'undefined') {
return false;
}

var xhr = new XMLHttpRequest();

xhr.open('POST', url, true); //第三个参数决定是否采用异步的方式
xhr.setRequestHeader('content-type', 'application/json');
xhr.send(JSON.stringify(obj.data));

xhr.onreadystatechange = function(re){
if (xhr.status === 200 && xhr.readyState === 4) {
if(typeof(obj.success) != "undefined") {
let json = JSON.parse(xhr.responseText);
return obj.success(json);
}
} else {
if(typeof(obj.error) != "undefined") {
return obj.error();
}
}
};
}
Yw_wechat.get_switch()

猜你喜欢

转载自www.cnblogs.com/LindaBlog/p/12799809.html