使用uniapp微信小程序获取用户信息

直接使用uni.getUserInfo会出现获取不到用户信息,腾讯已经做出了修改

为优化用户体验,使用 wx.getUserInfo 接口直接弹出授权框的开发方式将逐步不再支持。从2018年4月30日开始,小程序与小游戏的体验版、开发版调用 wx.getUserInfo 接口,将无法弹出授权询问框,默认调用失败。正式版暂不受影响。开发者可使用以下方式获取或展示用户信息:
小游戏与小程序获取用户接口信息调整

按照文档加上 open-type=“getUserInfo” bindgetuserinfo=“bindgetuserinfo”

<button type="primary" open-type="getUserInfo" bindgetuserinfo="bindgetuserinfo" >微信登录</button>

会出现一个问题
does not have a method “bindgetuserinfo” to handle event “getuserinfo”.
修改成如下即可

<button type="primary" open-type="getUserInfo" @getuserinfo="getuserinfo" withCredentials="true">微信登录</button>

然后在methods中

		getuserinfo() {
			// wx登录
			wx.login({
				success(res) {
					if (res.code) {
						//发起网络请求
						const code = res.code;
						// 获取微信用户信息
						wx.getUserInfo({
							success: function(res) {
								//用户信息
								const userInfo = res.userInfo;
								//性别 0:未知、1:男、2:女
								const { nickName, avatarUrl, gender, province, city, country } = userInfo;

								uni.setStorageSync('userInfo', userInfo);
								uni.navigateTo({
									url: './wxinfo/wxinfo'
								});
							},
							fail: res => {
								// 获取失败的去引导用户授权
								uni.showToast({
									title: '您需要授权,才能获取您的信息!'
								});
							}
						});
					} else {
						//
					}
				}
			});
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值