<button bindtap="login">登陆</button>login()
login(){
wx.getUserProfile({
desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (file) => { console.log(file)
wx.login({ success: (res) => {
console.log(res);
wx.request({
url: 'code获取openid的接口',
data: {
code: res.code
}, s
uccess: (open) => {
console.log(open.data);
wx.request({
url: '授权登陆接口',
data: {
openid: open.data.openid,
NickName: file.userInfo.nickName,
HeadUrl: file.userInfo.avatarUrl
},
success(data) {
console.log(data.data);
}
})
}
})
}
})
}
})
},
wx.login({
success: function(res) {
var that = this;
header: {
'content-type': 'application/x-www-form-urlencoded',
'token': wx.getStorageSync('token') //读取cookie 拿到登录之后异步保存的token值
};
if (res.code) {
//console.log(res.code)
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
data: {
appid: ' ************* ', //AppID
secret: ' ************************************* ',//密钥
grant_type: 'authorization_code',
js_code: res.code
},
method: 'GET',
header: header,
success: function(res) {
var openid = res.data.openid; //登录之后返回的openid
console.log(openid)
wx.setStorageSync('openid', openid) //储存openid
if (openid != null & openid != undefined) {
wx.getUserInfo({
success: function(res) {
},
fail: function(res) {
//console.info('用户拒绝授权');
}
});
} else {
console.info('获取用户openid失败');
}
},
fail: function(res) {
console.info('获取用户openid失败');
console.log(error);
}
})
}
}
}),