<template>
<view>
<view>用户昵称:{{nickName?nickName:"没有获取到用户昵称"}}</view>
<button type="primary" size="mini" open-type="getUserInfo" @getuserinfo="getuserinfo">获取用户信息</button>
<button type="primary" size="mini" open-type="openSetting" @opensetting="opensetting">授权设置</button>
</view>
</template>
<script>
export default {
data() {
return {
nickName: ""
};
},
onShow() {
uni.getSetting({
success: res => {
if ( res && res.authSetting && res.authSetting.hasOwnProperty("scope.userInfo") ) {
//scope.userInfo字段存在
if (res.authSetting["scope.userInfo"]) {
//scope.userInfo:true为真
// 获取用户信息
uni.getUserInfo({
provider: "weixin",
success: infoRes => {
this.nickName = infoRes.userInfo.nickName;
}
});
} else {
// scope.userInfo:false
// 提醒用户开启授权
uni.showToast({
icon: "none",
title: "请开启获取用户信息授权"
});
this.nickName = "";
}
} else {
//scope.userInfo字段不存在
// 提醒用户开启授权
uni.showToast({
icon: "none",
title: "请开启获取用户信息授权"
});
}
}
});
},
methods: {
opensetting(res) {
console.log("00", res.detail.authSetting);
},
getuserinfo() {
// 获取用户信息
uni.getUserInfo({
provider: "weixin",
success: infoRes => {
console.log("用户昵称为:" + infoRes.userInfo.nickName);
this.nickName = infoRes.userInfo.nickName;
},
fail() {
uni.showToast({
icon: "none",
title: "请开启获取用户信息授权"
});
}
});
}
}
};
</script>