【项目问题】微信小程序通过用户授权获取信息 getUserInfo?getUserProfile?

本文介绍微信小程序在新版本中getUserInfo接口变化及getUserProfile的使用,提供如何在不同版本间进行适配的示例代码,确保在获取用户信息时兼容老版本和新需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

微信小程序通过用户授权获取信息

getUserInfo

<!--template-->
<button type="primary" open-type="getUserInfo" @getuserinfo="getUserInfo">获取用户信息</button>
//e => 2021年4月28日24时后发布的小程序新版本:获取到用户的匿名数据;旧版本获取的是用户的真实数据。(如果要更新版本那就需要做适配)
getUserInfo(e){
	console.log('getUserInfo',e)
}

getUserProfile

获取用户信息。页面产生点击事件(例如 button 上 bindtap 的回调中)后才可调用,每次请求都会弹出授权窗口,用户同意后返回 userInfo。该接口用于替换 wx.getUserInfo。

<!--template-->
<button type="primary" @click="getUserProfile" v-if="!hasUserInfo">获取用户信息</button>
getUserProfile() {
	wx.getUserProfile({
		desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
		success: (res) => {
			console.log('getUserProfile',res)
		}
	})
},

如何做适配?

在OnLoad中判断当前版本有没有 getUserProfile 这个方法,再去逻辑判断用哪种方式

<!--template-->
<button type="primary" @click="getUserProfile" v-if="canIUseGetUserProfile">获取用户信息</button>
<button type="primary" open-type="getUserInfo" @getuserinfo="getUserInfo" v-else>获取用户信息</button>
onLoad() {
	if(wx.getUserProfile){
		this.canIUseGetUserProfile = true
	}
},
### 微信小程序获取用户信息的方法教程 微信小程序获取用户信息的方式随着官方政策的调整而不断变化。以下是目前实现获取用户头像、昵称等信息的常用方法及代码示例。 #### 方法一:通过 `chooseAvatar` 接口获取用户头像 微信小程序提供了 `chooseAvatar` 接口,允许用户主动选择头像并上传到小程序中[^2]。该接口适用于需要用户手动选择头像的场景。 ```html <button class="mine_avatar_wrapper" open-type="chooseAvatar" @chooseavatar="onChooseAvatar"> <image class="mine_image" :src="avatarUrl"></image> </button> ``` 在 JavaScript 中处理回调函数: ```javascript Page({ data: { avatarUrl: '' // 用户头像URL }, onChooseAvatar(e) { const { avatarUrl } = e.detail; this.setData({ avatarUrl }); } }); ``` #### 方法二:通过 `getUserProfile` 替代方案获取用户信息 由于 `wx.getUserProfile` 接口已被废弃,官方推荐使用 `wx.getSetting` 和 `wx.authorize` 配合 `wx.getUserInfo` 实现获取用户信息的功能[^3]。以下是一个完整的流程: 1. **判断是否授权**:通过 `wx.getSetting` 检查用户是否已授权。 2. **请求授权**:如果未授权,调用 `wx.authorize` 请求授权。 3. **获取用户信息**:调用 `wx.getUserInfo` 获取头像、昵称等信息。 WXML 示例代码: ```html <view class="banner"> <view class="topContainer"> <view catchtap="showBcgImgArea"> <image class="userinfo-avatar" mode="aspectFill" src="{{userinfo.avatarUrl}}"></image> </view> <view> <text class="userinfo-nickname">{{userinfo.nickName}}</text> </view> </view> <button wx:if="{{!hasUserInfo && canIUseGetUserProfile}}" open-type="getUserInfo" bindtap="getUserProfile" class="userLogin">点击登录</button> </view> ``` JavaScript 示例代码: ```javascript Page({ data: { userinfo: {}, hasUserInfo: false, canIUseGetUserProfile: true }, onLoad() { if (wx.canIUse('button.open-type.getUserInfo')) { this.setData({ canIUseGetUserProfile: true }); } else { this.setData({ canIUseGetUserProfile: false }); } }, getUserProfile() { wx.getSetting({ success(res) { if (!res.authSetting['scope.userInfo']) { wx.authorize({ scope: 'scope.userInfo', success() { wx.getUserInfo({ success(res) { const userInfo = res.userInfo; this.setData({ userinfo: userInfo, hasUserInfo: true }); }.bind(this) }); }.bind(this) }); } else { wx.getUserInfo({ success(res) { const userInfo = res.userInfo; this.setData({ userinfo: userInfo, hasUserInfo: true }); }.bind(this) }); } }.bind(this) }); } }); ``` #### 方法三:获取用户手机号码 如果需要获取用户的手机号码,可以使用 `getPhoneNumber` 接口[^2]。前端通过按钮触发 `getPhoneNumber`,后端解密获取真实手机号。 WXML 示例代码: ```html <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机号</button> ``` JavaScript 示例代码: ```javascript Page({ getPhoneNumber(e) { console.log(e.detail.errMsg); console.log(e.detail.iv); console.log(e.detail.encryptedData); // 将 encryptedData 和 iv 发送到后端进行解密 } }); ``` 后端解密逻辑需要根据微信官方文档实现,具体参考微信开放平台提供的加密解密算法[^2]。 --- ### 注意事项 1. 用户信息获取需遵循微信官方的隐私政策和权限规则,确保用户知情并同意。 2. 如果用户拒绝授权,无法获取其个人信息,需提供替代功能或引导用户重新授权。 3. 获取用户手机号时,务必保证数据的安全性和合法性。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@Dai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值