新版的获取用户信息授权
包含用户昵称,头像等
<button bindtap="bindGetUserInfo" class="btnPhone {{wechat_name !=''?'btnPhoneActive':''}}">授权登录</button>
// 弹出授权 开始
bindGetUserInfo: function(res) {
var that = this
// getUserProfile
wx.getUserProfile({
desc: '展示用户信息', //不能为空
success(res){
console.log("xinban获取用户信息",res)
that.setData({
wechat_name:res.userInfo.nickName,
headimgurl:res.userInfo.avatarUrl,
province:res.userInfo.province,
country:res.userInfo.country,
gender:res.userInfo.gender,
city:res.userInfo.city,
})
that.postInfo()
wx.showToast({
title: '授权成功!', // 标题
icon: 'success', // 图标类型,默认success
duration: 1500 // 提示窗停留时间,默认1500ms
})
}
})
console.log(res);
},
// 弹出授权 结束
微信小程序 获取手机号授权
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" class="btnPhone {{mobile !=''?'btnPhoneActive':''}}">获取手机号</button>
// 获取手机号开始
getPhoneNumber: function (e) {
console.log(e)
var ivObj = e.detail.iv
var telObj = e.detail.encryptedData
var codeObj = "";
var that = this;
//------执行Login---------
wx.login({
success: res => {
console.log('code转换', res.code);
//用code传给服务器调换session_key
// userGetMobile
requestApi.userGetMobile({
vcode: res.code,
encryptedData: telObj,
iv: ivObj
}).then(res => {
console.log("获取手机号成功",res)
console.log("res.data.data.phoneNumber",res.data.data.phoneNumber)
that.setData({
mobile:res.data.data.phoneNumber,
openid:res.data.data.openid,
unionid:res.data.data.unionid,
})
// console.log("手机号=", res.data.data.phoneNumber)
// post请求
that.postInfo()
wx.showToast({
title: '获取手机号成功!', // 标题
icon: 'success', // 图标类型,默认success
duration: 1500 // 提示窗停留时间,默认1500ms
})
})
//-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
if (e.detail.errMsg == 'getPhoneNumber:user deny') { //用户点击拒绝
// wx.navigateTo({
// url: '../index/index',
// })
} else { //允许授权执行跳转
// wx.navigateTo({
// url: '../test/test',
// })
}
}
});
},
// 获取手机号 结束
小程序地理位置授权
<button bindtap="getLocation" class="btnPhone {{lng !=''?'btnPhoneActive':''}}">获取地理位置</button>
// 获取地理位置开始
getLocation() {
var that = this
wx.getLocation({
success: function(res) {
console.log("位置信息",res)
app.globalData.lat = res.latitude;
app.globalData.lon = res.longitude;
that.setData({
lng:res.longitude,
lat:res.latitude
})
// post请求
that.postInfo()
wx.showToast({
title: '获取位置成功!', // 标题
icon: 'success', // 图标类型,默认success
duration: 1500 // 提示窗停留时间,默认1500ms
})
},
fail() {
wx.showModal({
title: '提醒',
content: '您拒绝了位置授权,将无法使用大部分功能,点击确定重新获取授权',
success(res) {
//如果点击确定
if (res.confirm) {
wx.openSetting({
success(res) {
//如果同意了位置授权则userLocation=true
if (res.authSetting["scope.userLocation"]) {
// that.onLoad()
}
}
})
}
}
})
}
})
},
// 获取地理位置结束
判断用户是否已经授权
//查看是否授权
wx.getSetting({
success: function(res) {
if (res.authSetting['scope.userInfo']) {
console.log("用户授权了");
console.log(res);
wx.getUserInfo({
success: res => {
console.log("用户授权信息",res)
app.globalData.userInfo = res.userInfo
//业务代码
}
})
} else {
//用户没有授权
console.log("用户没有授权");
}
console.log("res.authSetting",res.authSetting)
if (res.authSetting['scope.userLocation']) {
console.log("用户已经授权地理位置");
} else {
console.log("用户没有授权地理位置");
}
}
});
// 授权结束