cc.Class({
extends: cc.Component,
properties: {
},
onClick() {
let self = this;
let button = wx.createUserInfoButton({
type: 'text',
text: '查看排行榜',
style: {
left: 10,
top: 50,
width: 200,
height: 40,
backgroundColor: '#00ffb7',
color: '#ffffff',
textAlign: 'center',
fontSize: 16,
borderRadius: 4
}
});
button.onTap((res) => {
console.log(res);
if (res.userInfo) {
let userInfo = res.userInfo;
self.wxLogin(userInfo);
button.destroy();
} else {
console.log("用户拒绝授权获取用户信息");
}
});
},
wxLogin(userInfo) {
let icon = cc.find("Canvas/bg-005/icon").getComponent(cc.Sprite);
cc.loader.load({ url: userInfo.avatarUrl, type: "png" }, function (err, texture) {
if (err) {
console.error("加载用户头像失败", err);
} else {
icon.spriteFrame = new cc.SpriteFrame(texture);
}
});
},
start() {
wx.getSetting({
success(res) {
if (res.authSetting['scope.userInfo']) {
wx.getUserInfo({
success(res) {
let userInfo = res.userInfo;
self.wxLogin(userInfo);
}
});
} else {
console.log("用户未授权获取用户信息");
}
}
});
}
});
https://developers.weixin.qq.com/community/develop/doc/00022c683e8a80b29bed2142b56c01