当前位置:网站首页>小程序学习之获取用户信息(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>



边栏推荐
- Codeforces Round #392 (Div. 2) D. Ability To Convert
- PTA猴子选大王(约瑟夫环问题)
- Tnsnames Ora file configuration
- WindowManager 简单悬浮框的实现
- 观察者模式
- Learning Tai Chi Maker - esp8226 (XIII) OTA
- 桌面软件开发框架大赏
- Implementation of simple floating frame in WindowManager
- Baidu AI template for knowledge understanding
- 最新Windows下Go语言开发环境搭建+GoLand配置
猜你喜欢

Oracle 12c升级至19c后ORA-28000错误

Latex formula and table recognition

tp5 使用post接收数组数据时报variable type error: array错误的解决方法

有关二叉树 的基本操作

Handling method of Oracle data file header SCN inconsistency

编程题(持续更新)

如何让社交媒体成为跨境电商驱动力?这款独立站工具不能错过!

impdp导schema报ORA-31625异常处理

Grpc local test joint debugging tool bloomrpc

ThinkPHP5多语言切换项目实战
随机推荐
In depth analysis of Apache bookkeeper series: Part 3 - reading principle
数组无缝滚动demo
LeetCode: 240. 搜索二维矩阵 II
ThinkPHP5多语言切换项目实战
Prct-1400: failed to execute getcrshome resolution
Threejs point light + ambient light
SSH Remote Password free login
Amazing tips for using live chat to drive business sales
PTA猴子选大王(约瑟夫环问题)
Oracle database listening file configuration
20、 Processor scheduling (RR time slice rotation, mlfq multi-level feedback queue, CFS fully fair scheduler, priority reversal; multiprocessor scheduling)
Arbre binaire partie 1
Implementation of simple floating frame in WindowManager
NLP-D59-nlp比赛D28—我想,也好—阶段总结—心态调整
Five heart matchmaker
LeetCode: 377. Combined sum IV
June 13-19, 2022 AI industry weekly (issue 102): career development
针对《VPP实现策略路由》的修正
正则匹配邮箱
P6117-[joi 2019 final] greedy