creator微信小游戏的登录及授权其实很简单,简单几步就能完成。
首先是登录:
let exportJson = {};
window.wx.login({
success: (userRes) => {
exportJson.code = userRes.code;//向服务端传递code用于获取微信小游戏的用户唯一标识
},
});
大家都知道,微信修改了wx.getUserInfo接口的策略,需要用户调用wx.createUserInfoButton来创建一个授权按钮进行授权,不再自动弹出。这里可以做成全屏按钮的形式进行授权,简单粗暴又美观。下面上代码:
let exportJson = {};
let sysInfo = window.wx.getSystemInfoSync();
//获取微信界面大小
let width = sysInfo.screenWidth;
let height = sysInfo.screenHeight;
window.wx.getSetting({
success (res) {
console.log(res.authSetting);
if (res.authSetting["scope.userInfo"]) {
console.log("用户已授权");
window.wx.getUserInfo({
success(res){
console.log(res);
exportJson.userInfo = res.userInfo;
//此时可进行登录操作
}
});
}else {
console.log("用户未授权");
let button = window.wx.createUserInfoButton({
type: 'text',
text: '',
style: {
left: 0,
top: 0,
width: width,
height: height,
backgroundColor: '#00000000',//最后两位为透明度
color: '#ffffff',
fontSize: 20,
textAlign: "center",
lineHeight: height,
}
});
button.onTap((res) => {
if (res.userInfo) {
console.log("用户授权:", res);
exportJson.userInfo = res.userInfo;
//此时可进行登录操作
button.destroy();
}else {
console.log("用户拒绝授权:", res);
}
});
}
}
})
网友评论