当前位置:网站首页>小程序学习之获取用户信息(getUserProfile and getUserInfo)
小程序学习之获取用户信息(getUserProfile and getUserInfo)
2022-06-24 09:32:00 【.叹服】
题外话:
在学习这个模块的时候是看了b站的一个20年的视频,视频上老师举例时用的getUserInfo接口,但是自己按着老师的敲获取的信息确实匿名的.不得已上网搜结果发现getUserInfo接口在21年的时候功能改了.
getUserInfo的改动:
调整说明
自 2022 年 10 月 25 日 24 时后(以下统称 “生效期” ),用户头像昵称获取规则将进行如下调整:
- 自生效期起,小程序 wx.getUserProfile 接口将被收回:生效期后发布的小程序新版本,通过 wx.getUserProfile 接口获取用户头像将统一返回默认灰色头像,昵称将统一返回 “微信用户”。生效期前发布的小程序版本不受影响,但如果要进行版本更新则需要进行适配。
- 自生效期起,插件通过 wx.getUserInfo 接口获取用户昵称头像将被收回:生效期后发布的插件新版本,通过 wx.getUserInfo 接口获取用户头像将统一返回默认灰色头像,昵称将统一返回 “微信用户”。生效期前发布的插件版本不受影响,但如果要进行版本更新则需要进行适配。通过 wx.login 与 wx.getUserInfo 接口获取 openId、unionId 能力不受影响。
- 「头像昵称填写能力」支持获取用户头像昵称:如业务需获取用户头像昵称,可以使用「头像昵称填写能力」(基础库 2.21.2 版本开始支持),具体实践可见下方《最佳实践》。
- 小程序 wx.getUserProfile 与插件 wx.getUserInfo 接口兼容基础库 2.21.2 以下版本的头像昵称获取需求:上述「头像昵称填写能力」从基础库 2.21.2 版本开始支持(覆盖微信 8.0.16 以上版本)。对于来自更低版本的基础库与微信客户端的访问,小程序通过 wx.getUserProfile 接口将正常返回用户头像昵称,插件通过 wx.getUserInfo 接口将返回用户头像昵称,开发者可继续使用以上能力做向下兼容。
也就是说原本的通过wx.getUserInfo接口获取用户id及头像的功能被getUserProfile接口替代.
getUserProfile
功能描述:
获取用户信息。页面产生点击事件(例如 button
上 bindtap
的回调中)后才可调用,每次请求都会弹出授权窗口,用户同意后返回 userInfo
。该接口用于替换 wx.getUserInfo
,
样例:
js:
// index.js
// 获取应用实例
const app = getApp()
Page({
data: {
motto: '简易计算器',
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
canIUseGetUserProfile:false,
canIUseOpenData: false // 如需尝试获取用户信息可改为false
},
// 事件处理函数
// bindViewTap() {
// wx.navigateTo({
// url: '../logs/logs'
// })
// },
onLoad() {
if (wx.getUserProfile) {
this.setData({
canIUseGetUserProfile: true
})
}
},
getUserProfile(e) {
// 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
wx.getUserProfile({
desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
console.log(res)
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
},
getUserInfo(e) {
// 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
console.log(e)
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
}
wxml:
<!--index.wxml-->
<view class="container">
<view class="userinfo">
<block wx:if="{
{canIUseOpenData}}">
<view class="userinfo-avatar" bindtap="bindViewTap">
<open-data type="userAvatarUrl"></open-data>
</view>
<open-data type="userNickName"></open-data>
</block>
<block wx:elif="{
{!hasUserInfo}}">
<button wx:if="{
{canIUseGetUserProfile}}" bindtap="getUserProfile"> 登录 </button>
<button wx:elif="{
{canIUse}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 获取头像昵称 </button>
<view wx:else> 请使用1.4.4及以上版本基础库 </view>
</block>
<block wx:else>
<image bindtap="bindViewTap" class="userinfo-avatar" src="{
{userInfo.avatarUrl}}" mode="cover"></image>
<text class="userinfo-nickname">{
{userInfo.nickName}}</text>
</block>
</view>
<view class="usermotto">
<button >
<navigator url="/pages/calculator/calculator" open-type="switchTab"> 简易计算器</navigator>
</button>
<button class="change" >
<navigator url="/pages/change_/changes_" open-type="switchTab"> 换算工具</navigator>
</button>
</view>
</view>
边栏推荐
- Talking about the knowledge of digital transformation
- 2021-08-17
- P6117-[joi 2019 final] greedy
- Oracle database listening file configuration
- Oracle viewing data file header SCN information
- How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!
- Thinkphp5 clear the cache cache, temp cache and log cache under runtime
- ssh远程免密登录
- 买的长期理财产品,可以转短吗?
- JCIM|药物发现中基于AI的蛋白质结构预测:影响和挑战
猜你喜欢
Servlet fast foundation building
生产者/消费者模型
IDEA 无法保存设置 源根 D:XXXX在模块XXX中重复
Conseils étonnants pour promouvoir les ventes d'entreprise avec le chat en direct
二叉樹第一部分
Event registration Apache pulsar x kubesphere online meetup hot registration
Learning Tai Chi Maker - esp8226 (XIII) OTA
Why is LNX of e equal to X
Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code
Oracle 12c升级至19c后ORA-28000错误
随机推荐
桌面软件开发框架大赏
如何让社交媒体成为跨境电商驱动力?这款独立站工具不能错过!
Oracle viewing data file header SCN information
P6698-[balticoi 2020 day2] virus [AC automata, DP, SPFA]
NLP-D59-nlp比赛D28—我想,也好—阶段总结—心态调整
PostgreSQL
grpc本地测试联调工具BloomRPC
Amazing tips for using live chat to drive business sales
LeetCode: 377. 组合总和 Ⅳ
20、 Processor scheduling (RR time slice rotation, mlfq multi-level feedback queue, CFS fully fair scheduler, priority reversal; multiprocessor scheduling)
Tnsnames Ora file configuration
带文字的seekbar : 自定义progressDrawable/thumb :解决显示不全
Amendment to VPP implementation policy routing
Detailed explanation of ThinkPHP 5.0 Model Association
port 22: Connection refused
顶刊TPAMI 2022!基于不同数据模态的行为识别:最新综述
SSH Remote Password free login
How to make social media the driving force of cross-border e-commerce? This independent station tool cannot be missed!
el-table表格的拖拽 sortablejs
获取带参数的微信小程序二维码-以及修改二维码LOGO源码分享