当前位置:网站首页>获取用户信息-微信小程序
获取用户信息-微信小程序
2022-04-21 22:13:00 【顺其自然lll】
button组件中的open-type
<button open-type=""></button>中的open-type用于提供微信开放能力。open-type有如下合法值:
contact,打开客服会话,如果用户在会话中点击消息卡片后返回小程序,可以从bindcontact 回调中获得具体信息。
share,触发用户转发。
getPhoneNumber,获取用户手机号,可以从bindgetphonenumber回调中获取用户信息。
getUserInfo,获取用户信息,可以从bindgetuserinfo回调中获取用户信息。
launchApp,打开APP。
openSetting,打开授权设置页。
feedback,打开“意见反馈”页面。
<!--index.wxml-->
<button open-type="contact" bindcontact="contact">contact </button>
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">getPhoneNumber</button>
<button open-type="getUserInfo" bindgetuserinfo="getUserInfo">getUserInfo</button>
// index.js
Page({
contact:function(e){
console.log(e.detail);
},
getPhoneNumber:function(e){
console.log(e.detail);
},
getUserInfo:function(e){
console.log(e.detail.userInfo);
}
})
使用单击按钮,提示用户授权,用户授权同意后才可以获取用户信息。
<!--index.wxml-->
<view class="container">
<button wx:if="{
{!hasUserInfo}}" open-type="getUserInfo" bindgetuserinfo="getUserInfo">获取头像昵称</button>
<view class="userinfo" wx:else>
<image src="{
{userInfo.avatarUrl}}"></image>
<text>{
{userInfo.nickName}}</text>
</view>
</view>
// index.js
Page({
data:{
hasUserInfo:false,
userInfo:{}
},
getUserInfo:function(e){
console.log(e);
console.log(e.detail.userInfo);
if(e.detail.userInfo){
this.setData({
userInfo:e.detail.userInfo,
hasUserInfo:true
})
}
}
})
getUserInfo几乎没有给出用户信息。这是因为自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息,
微信提供getUserProfile()来替代getUserInfo()
接口getUserProfile
接口getUserProfile用来替换getUserInfo,以获取用户信息。
页面产生点击事件后才可调用,每次请求都会弹出授权窗口,用户同意后返回userInfo,详见下面的例子。
<!--index.wxml-->
<view class="container">
<button wx:if="{
{!hasUserInfo}}" bindtap="getUserProfile">获取头像昵称</button>
<view class="userinfo">
<image src="{
{userInfo.avatarUrl}}"></image>
<view>{
{userInfo.nickName}}</view>
</view>
</view>
// index.js
Page({
data:{
hasUserInfo:false,
userInfo:{}
},
getUserProfile:function(){
wx.getUserProfile({
desc: '获取用户信息',
success:(res) => {
console.log(res.userInfo);
this.setData({
hasUserInfo:true,
userInfo:res.userInfo
})
}
})
}
})
getUserProfile()的success回调函数的参数res,它包含如下属性,
errMsg,错误信息。
userInfo,用户信息对象,不包含openid等敏感信息。
rawData,不包含敏感信息的原始数据字符串,用于计算签名。
signature,使用sha1(rawData+session_key)得到字符串,用于校验用户信息。
encryptedData,包含敏感数据在内的完整用户信息的加密数据。
iv,加密算法的初始向量。
如果小程序前台需要向小程序后台发送用户信息,可以通过wx.request()来实现
查看授权结果
wx.getSetting获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。看个小例子。
<!--index.wxml-->
<button bindtap="getSetting">查看授权结果</button>
// index.js
Page({
getSetting:function(){
wx.getSetting({
success:(res) => {
console.log(res.authSetting);
if(res.authSetting["scope.userInfo"]){
wx.getUserInfo({
success: (res) => {
console.log(res.userInfo);
}
})
}
}
})
}
})
res.authSetting中包含
scope.address,通讯地址scope.invoice,发票scope.invoiceTitle,发票抬头scope.userInfo,用户信息
组件 open-data用于展示微信开放的数据。所谓“开放”的数据,就是不需要用户授权就可以显示的数据,如用户的头像、昵称、性别等。
开放数据的类型,有如下合法值,
groupName
userNickName,用户昵称
userAvatarUrl,用户头像
userGender,用户性别
userCity,用户所在城市
userProvince,用户所在省份
userCountry,用户所在国家
userLanguage,用户的语言
开放数据的类型,有如下合法值,
groupName
userNickName,用户昵称
userAvatarUrl,用户头像
userGender,用户性别
userCity,用户所在城市
userProvince,用户所在省份
userCountry,用户所在国家
userLanguage,用户的语言
版权声明
本文为[顺其自然lll]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_46965984/article/details/124320405
边栏推荐
- STM32外设GPIO的配置和应用
- 怎么用vbs实现微信自动发送消息功能
- ROS - use OpenCV to send and receive cameras
- 正则表达式
- 软件测试流程与测试模型
- Kotlin crawler app, Android development, interview preparation
- 【ES6】数组的扩展
- [ES6] let and const commands
- How does PHP add an array element to an array
- Oracle cascade delete table (not subject to foreign key constraints)
猜你喜欢

HTTP cache notes

L3-1 那就别担心了 (30 分)——坑点,测试点分析

WPF数据驱动修改绑定的方法

Unity3d import tilt model, etc

【WebGL】简单入门教程

The third floor of leetcode Langya list - the container with the most water

「运维有小邓」企业如何解决AD域弱密码问题?

数据库事务学习总结

ROS robot from starting point to end point (IV) reproduction of blue bridge cloud practice

Interview must brush algorithm top101 knapsack nine lectures top14
随机推荐
LeetCode146-LRU缓存-模拟-双向链表-哈希表-数据结构-操作系统
ROS robot from starting point to end point (IV) reproduction of blue bridge cloud practice
攻防世界 mfw
EventBridge 集成云服务实践
INT 102_TTL 09
【ES6】Iterator和forof循环
Finding a way to combine new technologies with itself is the key to the development of industrial Internet
MYPINPAD和SmartPesa合并,成为移动支付受理领域的全球领导者
what? Your company has not set the JVM initial and maximum heap memory size to the same value?
[canvas] basic drawing and use
云原生微服务的下一站,微服务引擎 MSE 重磅升级
Practice of JVM custom class loader in code extensibility
Record a pit in the split3 Library (table name and field definition cannot use placeholders?)
【ES6】Generator
【ES6】函数的扩展
Mypinpad and smartpesa merged to become the global leader in mobile payment acceptance
[JVM] 10 interview questions
Detailed explanation of redis configuration file
kotlin环境搭建,2021百度Android岗面试真题收录解析
Gd32f103 learning notes (8) -- use of ADC interface