会员页代码示例
//第一授权获取用户信息===》按钮触发
wxGetUserInfo() {
let _this = this;
uni.getUserInfo({
provider: 'weixin',
success: function(infoRes) {
//console.log(infoRes.userInfo)
_this.userInfo = infoRes.userInfo; //昵称
try {
uni.setStorageSync('isCanUse', false);//记录是否第一次授权 false:表示不是第一次授权
_this.isCanUse = false;
_this.updateUserInfo();
} catch (e) {}
},
fail(res) {}
});
},
//登录
login() {
let _this = this;
if(!uni.getStorageSync("sessionid")){
uni.showLoading({
title: '登录中...'
});
}
// 查看是否授权
wx.getSetting({
success (res){
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
wx.getUserInfo({
success: function(infoRes) {
console.log(infoRes.userInfo);
uni.setStorageSync('isCanUse', false);
_this.isCanUse = false;
_this.userInfo = infoRes.userInfo; //昵称
_this.UserInfoDetail();
}
})
}
}
})
},
//向后台更新信息
updateUserInfo() {
//console.log(this.userInfo)
let _this = this;
uni.request({
url: _this.$host + 'index/Member/add',//服务器端地址
method:"POST",
header: {
'cookie': uni.getStorageSync("sessionid")
},
data: {
nickname: _this.userInfo.nickName,
photo: _this.userInfo.avatarUrl,
sex:_this.userInfo.gender,
},
success: (res) => {
console.log(res)
if(res.data.code==200){
_this.one = res.data.data;
}
}
});
},
//向后台更新信息
UserDetail() {
//console.log(this.userInfo)
let _this = this;
uni.request({
url: _this.$host + 'index/Member/detail',//服务器端地址
method:"POST",
header: {
'cookie': uni.getStorageSync("sessionid")
},
success: (res) => {
console.log(res)
if(res.data.code==200){
_this.one = res.data.data;
}
}
});
},