97 lines
2.9 KiB
JavaScript
97 lines
2.9 KiB
JavaScript
var auths = {}
|
||
function plusReady() {
|
||
plus.oauth.getServices(function(services) {
|
||
for(var i in services){
|
||
var service=services[i];
|
||
auths[service.id]=service;
|
||
}
|
||
}, function(e) {
|
||
alert("获取登录服务列表失败:" + e.message + " - " + e.code);
|
||
});
|
||
}
|
||
document.addEventListener('plusready',plusReady,false);
|
||
// 登录认证
|
||
function loginInfo(dest){
|
||
var auth=auths[dest];
|
||
console.log(JSON.stringify(auth))
|
||
if(auth){
|
||
var w=null;
|
||
if(plus.os.name=="Android"){
|
||
w=plus.nativeUI.showWaiting();
|
||
}
|
||
document.addEventListener("pause",function(){
|
||
setTimeout(function(){
|
||
w&&w.close();w=null;
|
||
},2000);
|
||
}, false );
|
||
auth.login(function(){
|
||
w&&w.close();w=null;
|
||
userinfo(auth);
|
||
},function(e){
|
||
w&&w.close();w=null;
|
||
$.toast("登录认证失败:["+e.code+"]:"+e.message);
|
||
});
|
||
}else{
|
||
$.toast("无效的登录认证通道!");
|
||
$.toast("无效的登录认证通道!",null,"登录");
|
||
}
|
||
}
|
||
|
||
//wx获取用户信息
|
||
function userinfo(a){
|
||
a.getUserInfo(function(){
|
||
console.log(JSON.stringify(a))
|
||
$.showPreloader();
|
||
if(a.id=='weixin'){
|
||
var nickname=a.userInfo.nickname||a.userInfo.name||a.userInfo.miliaoNick;
|
||
var openid = a.authResult.openid;
|
||
var accessToken = a.authResult.access_token;
|
||
var unionid = a.userInfo.unionid;
|
||
var headImg = a.userInfo.headimgurl;
|
||
login(openid, unionid, accessToken, nickname, headImg, "weixin", "/rest/user/wxLogin")
|
||
}else if(a.id=='qq'){
|
||
var nickname= a.userInfo.nickname;
|
||
var openid = a.authResult.openid;
|
||
var accessToken = a.authResult.access_token;
|
||
var headImg = a.userInfo.headimgurl;
|
||
var unionid = "";
|
||
login(openid, unionid, accessToken, nickname, headImg, "qq", "/rest/user/qqLogin")
|
||
}else{
|
||
//微博信息获取
|
||
var nickname = a.userInfo.name||a.userInfo.screen_name;
|
||
var openid = a.authResult.uid;
|
||
var accessToken = a.authResult.access_token;
|
||
var headImg = a.userInfo.headimgurl;
|
||
var uid = a.authResult.uid;
|
||
var unionid = "";
|
||
login(openid, unionid, accessToken, nickname, headImg, "sinaweibo", "/rest/user/sinaweiboLogin")
|
||
}
|
||
|
||
},function(e){
|
||
$.toast("获取用户信息失败:["+e.code+"]:"+e.message);
|
||
});
|
||
}
|
||
|
||
function login(openid, unionid, accessToken, nickname, headImg, loginFrom, uri) {
|
||
axios.post(uri, {loginFrom:loginFrom,openid:openid, unionid:unionid, accessToken: accessToken, nickname:nickname,headImg:headImg})
|
||
.then(function (response) {
|
||
$.hidePreloader();
|
||
var resData = response.data;
|
||
if(resData.status){
|
||
if(resData.data == ""){
|
||
//跳转绑定手机号码页面
|
||
clicked('../user/bindingMobile.html',true,false,{},{loginFrom:loginFrom,openid:openid, unionid:unionid, accessToken: accessToken, nickname:nickname,headImg:headImg},function(){
|
||
back();
|
||
})
|
||
}else{
|
||
localStorage.setItem("user_token", resData.data);
|
||
localStorage.setItem("user_token_add_time", new Date().getTime());
|
||
back();
|
||
}
|
||
}else{
|
||
//登录失败
|
||
$.toast(resData.message)
|
||
}
|
||
})
|
||
}
|