参考:https://blog.csdn.net/duan003387/article/details/83089765
参考:https://www.jianshu.com/p/56a9ac861c60
getUserInfo(left, bottom) {
return new Promise((resolve, reject) => {
let sysInfo = wx.getSystemInfoSync();
let sdkVersion = sysInfo.SDKVersion;
sdkVersion = sdkVersion.replace(/\./g, "");
sdkVersion = sdkVersion.substr(0, 3);
let sdkVersionNum = parseInt(sdkVersion);
//判断用户是否授权过
wx.getSetting({
success(res) {
if (sdkVersionNum >= 201 && !res.authSetting['scope.userInfo']) {
var button = wx.createUserInfoButton({
type: 'image',
image: 'http://www.oneh5.com/iptv/h5/egret/billiard/resource/game/jiqiu.png',
style: {
left: 118,
top: 263,
width: 140,
height: 140
}
});
button.onTap((res) => {
if (res.userInfo) {
var userInfo = res.userInfo;
var nickName = userInfo.nickName;
var avatarUrl = userInfo.avatarUrl;
var gender = userInfo.gender; //性别 0:未知、1:男、2:女
var province = userInfo.province;
var city = userInfo.city;
var country = userInfo.country;
button.destroy();
resolve(userInfo);
}
else{
button.destroy();
resolve(userInfo);
}
});
} else {
wx.getUserInfo({
withCredentials: true,
success: res => {
var userInfo = res.userInfo;
var nickName = userInfo.nickName;
var avatarUrl = userInfo.avatarUrl;
var gender = userInfo.gender; //性别 0:未知、1:男、2:女
var province = userInfo.province;
var city = userInfo.city;
var country = userInfo.country;
resolve(userInfo);
},
fail: res => {
wx.showModal({
title: '友情提醒',
content: '请允许微信获得授权!',
confirmText: "授权",
showCancel: false,
success: res => {
resolve(null);
}
});
}
});
}
}
})
});
}
第二种
class WxgamePlatform {
name = 'wxgame'
getPlatformSign() {
return {
platform: "wx"
};
}
login() {
return new Promise((resolve, reject) => {
wx.login({
success: (res) => {
resolve(res)
}
})
})
}
getUserInfo() {
let sysInfo = wx.getSystemInfoSync();
//获取微信界面大小
let width = sysInfo.screenWidth;
let height = sysInfo.screenHeight;
let xPercent = (720 - 262) / 2 / 720;
let yPercent = (1280 - 98) / 2 / 1280;
let wPercent = (260 / 720);
let hPercent = (98 / 1280);
return new Promise((resolve, reject) => {
let sysInfo = wx.getSystemInfoSync();
let sdkVersion = sysInfo.SDKVersion;
//sdkVersion = sdkVersion.replace(/\./g, "");
//sdkVersion = sdkVersion.substr(0, 3);
//let sdkVersionNum = parseInt(sdkVersion);
//console.log("platform获取用户授权:", sdkVersionNum);
//if (sdkVersionNum >= 201) {
if (sdkVersion >= "2.0.1") {
egret.log("in 1");
var button = wx.createUserInfoButton({
type: 'text',
text: ' ',
//image: "resource/assets_game/main/button_wx_getuserinfo.png",
style: {
left: width * xPercent,
top: height * yPercent,
width: width * wPercent,
height: height * hPercent,
// backgroundColor: '#ff0000',
// color: '#ffffff',
}
});
egret.log("in 2");
button.onTap((res) => {
egret.log("onTap ", res.userInfo);
if (res.userInfo) {
console.log("用户授权:", res);
var userInfo = res.userInfo;
var nickName = userInfo.nickName;
var avatarUrl = userInfo.avatarUrl;
var gender = userInfo.gender; //性别 0:未知、1:男、2:女
var province = userInfo.province;
var city = userInfo.city;
var country = userInfo.country;
button.destroy();
resolve(userInfo);
}
});
} else {
wx.getUserInfo({
withCredentials: true,
success: res => {
var userInfo = res.userInfo;
var nickName = userInfo.nickName;
var avatarUrl = userInfo.avatarUrl;
var gender = userInfo.gender; //性别 0:未知、1:男、2:女
var province = userInfo.province;
var city = userInfo.city;
var country = userInfo.country;
resolve(userInfo);
},
fail: res => {
wx.showModal({
title: '友情提醒',
content: '请允许微信获得授权!',
confirmText: "授权",
showCancel: false,
success: res => {
resolve(null);
}
});
}
});
}
})
}
openDataContext = new WxgameOpenDataContext();
}