当前位置:网站首页>2022年微信小程序授权登录的最新实现方案wx.getUserProfile
2022年微信小程序授权登录的最新实现方案wx.getUserProfile
2022-07-18 14:45:00 【coderYYY】
微信授权登录
我们的项目开发有时候用到用户的一些信息,比如头像,昵称等。目前小程序为我们提供好了wx.getUserProfile
方法以供获取用户信息,它的使用非常简单。
wx.getUserProfile方法获取用户信息
不推荐使用 wx.getUserInfo
获取用户信息,自2021年4月13日起,getUserInfo
将不再弹出弹窗,并直接返回匿名的用户个人信息
推荐使用 wx.getUserProfile
获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认。
对应的官方文档:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html
简单示例代码:
官网的示例代码写得较为复杂,这里我写了一些简单的代码,以便学习。
<!-- userInfo如果为空证明没有登录 -->
<button wx-if="{
{!userInfo}}" bindtap="login">获取头像昵称</button>
<view wx:else class="userInfo">
<image src="{
{userInfo.avatarUrl}}"></image>
<text>{
{userInfo.nickName}}</text>
</view>
.userInfo{
display: flex;
flex-direction: column;
align-items: center;
}
.userInfo image{
width: 200rpx;
height: 200rpx;
border-radius: 200rpx;
}
Page({
data: {
userInfo: '', //用于存放获取的用户信息
},
login() {
wx.getUserProfile({
desc: '必须授权才能继续使用', // 必填 声明获取用户个人信息后的用途,后续会展示在弹窗中
success:(res)=> {
console.log('授权成功', res);
this.setData({
userInfo:res.userInfo
})
},
fail:(err)=> {
console.log('授权失败', err);
}
})
}
})
退出登录
由于上面用的判断是否登录,是用userInfo是否为空判断的,所以我们退出登录只要把userInfo清空就行了,就是这么简单粗暴!
与本地缓存wx.setStorageSync结合使用
如果没有本地缓存,每次打开小程序都需要再授权一次,太麻烦了,而且本地缓存中的数据其他页面也能使用,不用重复获取。
完整代码:
<!-- userInfo如果为空证明没有登录 -->
<button wx-if="{
{!userInfo}}" bindtap="login">获取头像昵称</button>
<view wx:else class="userInfo">
<image src="{
{userInfo.avatarUrl}}"></image>
<text>{
{userInfo.nickName}}</text>
<button type="warn" bindtap="loginOut">退出登录</button>
</view>
Page({
data: {
userInfo: '', //用于存放获取的用户信息
},
onLoad(){
let user = wx.getStorageSync('user')
this.setData({
userInfo: user
})
},
// 授权登录
login() {
wx.getUserProfile({
desc: '必须授权才能继续使用', // 必填 声明获取用户个人信息后的用途,后续会展示在弹窗中
success:(res)=> {
console.log('授权成功', res);
wx.setStorageSync('user',res.userInfo)
this.setData({
userInfo:res.userInfo
})
},
fail:(err)=> {
console.log('授权失败', err);
}
})
},
// 退出登录
loginOut(){
this.setData({
userInfo:''
})
// 清空缓存
wx.setStorageSync('user',null)
}
})
总结
wx.getUserProfile
用于授权登录,获取用户信息,但它返回的加密数据中不包含 openId
和 unionId
字段,所以需要这两个信息的使用wx.login
获取。
边栏推荐
- Linux上安装Redis详细教程
- 【MySQL存储引擎 InnoDB、MyISAM、Memory区别与特点】
- 智能分层、满足更高工作负载,亚马逊云科技加速云端存储服务创新
- 干货满满,芯智讯“2018生物识别论坛”圆满落幕!
- 同花顺开股票账户安全吗
- 生成函数Ⅰ
- Google上网神器Ghelper
- Redhat8 KVM deployment and implementation of bridging network
- 新增一个复合主键的步骤
- One article speed learning - basis of pyspark data analysis: detailed explanation of pyspark principle
猜你喜欢
ECS load balancing usage
2022 high voltage electrician test questions and simulation examination
Data link comprehensive training document
Advanced embedded application of uni app [day14]
MySQL transaction details - [Database isolation level, dirty read, non repeatable read, phantom read, acid property and redo log and undo log]
美日韩联合研发6G意图反超,然而中国早有准备
全新出品!阿里P5工程师~P8架构师晋升路线揭秘
一文速学-PySpark数据分析基础:PySpark原理详解
其他所得要缴个人所得税有哪九类
AEAI portal v3.5.2 portal integration platform release instructions
随机推荐
阿里云ECS服务器使用教程
干货满满,芯智讯“2018生物识别论坛”圆满落幕!
Bright blind! Nokia rear five camera phone exposure, Zeiss certification!
Qt实现文件传输功能(基于TCP)【客户端传给服务器】
「接口测试入门课」打卡学习 day02:没有任何文档,怎么快速了解接口信息?
The 20th Shanghai University Programming League summer competition (off campus synchronized competition)
OS6-AutoSAR Resource概述和相应API的使用
Linux上安装Redis详细教程
中天微发布全球首款支持物联网安全的RISC-V处理器
vivo小米5G工程机相继曝光,华为祭出麒麟980+巴龙5000方案欲拔头筹!
联发科5G原型机正式曝光,竟然配备了多个风扇!
Introduction to bswm01 bswm mode management
三星芯片工厂发生二氧化碳泄露:工人一死两伤,已非首次
MDM三权分立及分类分级权限说明
Alibaba cloud ECS server tutorial
智能分层、满足更高工作负载,亚马逊云科技加速云端存储服务创新
生成函数Ⅰ
Mysql事务详解-[数据库的隔离级别、脏读、不可重复读、幻读以及ACID性质与redo log与undo log]
美日韩联合研发6G意图反超,然而中国早有准备
醍醐灌顶的金句