当前位置:网站首页>微信小程序-用户授权获取信息
微信小程序-用户授权获取信息
2022-06-09 12:03:00 【别盯着我的名字看】
从2018年4月30日开始,小程序与小游戏的体验版、开发版调用 wx.getUserInfo 接口,将无法弹出授权询问框,默认调用失败
(但不意味着 wx.getUserInfo 不能用了)。小程序官网上提供两种方式来获取用户信息。
1、使用 open-data 展示用户基本信息
open-data是小程序提供的不需要用户授权获取用户信息的组件,但是不能通过js来拿值,所以只能作为开发人员快速开发显示用的。
<open-data type="userAvatarUrl"></open-data> 获取用户头像
<open-data type="userNickName"></open-data> 获取用户昵称
...官网文档:open-data开放展示用户信息组件
2、使用 button 组件,并将 open-type 指定为 getUserInfo 类型,获取用户基本信息(重要)
在之前使用wx.getUserInfo不受限制,在js任何地方都可以使用,可能是为了防止滥用它,微信就改成必须通过button组件来获取
用户点击按钮授权
wxml:
<button open-type="getUserInfo" bindgetuserinfo="getUserInfo">登陆</button>js:
getUserInfo: function (e) {
const userInfo = e.detail.userInfo;
console.log(userInfo);
},通过上面的代码就能获取授权弹窗
定义一个方法判断用户是否授权
在上面的代码基础上增加一个判断是否授权的方法wx.getSetting 这是微信提供的API,获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。
Page({
data: {
},
onLoad: function (options) {
const that = this;
that.userAuthorized();
},
//判断用户是否授权
userAuthorized: function (e) {
var that = this;
wx.getSetting({
success: function (data) {
if (data.authSetting["scope.userInfo"]) {
wx.getUserInfo({
success: function (data) {
console.log("用户已经授权")
}
})
}
}
})
},
//点击登陆 授权
getUserInfo: function (e) {
const userInfo = e.detail.userInfo;
console.log(userInfo);
},
})边栏推荐
- GDI+ 中图片的绘制
- Gamefi's new departure, aquanee will log in to gate and bitmart on June 9
- MySQL optimistic lock, pessimistic lock and multi granularity lock
- keil5mdk安装(免费)
- 生词 生词 生词 生词 生词 生词 生词
- [001] 一天一句长难句分析
- What is a hard real-time database system?
- Safari的Favorites项不显示在主页上
- .NET基础知识快速通关8
- 使 QComboBox 下拉一个带复选框的树形列表
猜你喜欢
LeetCode-732. 我的日程安排表 III,差分数组

Range method returns the object conversion method

PostgreSQL weekly news - june1,2022

What is a hard real-time database system?
![Live broadcast preview | database salon – [special session on open source ecology]](/img/e2/abc3922b8f99e52146dd89e2a13122.png)
Live broadcast preview | database salon – [special session on open source ecology]

. Net basic knowledge quick pass 8
Qpprocess class Usage Summary

PostgreSQL每周新闻—2022年6月1日

Endnote | how to quickly share literature with others (including title + Notes +pdf file) | endnote save backup

.NET基础知识快速通关9
随机推荐
Zabbix6.0新功能Geomap 地图标记 你会用吗?
Hype plagiarism, insider fraud common NFT scams and security suggestions on opensea
Rédaction de documents scientifiques
在VsCode中使用LeetCode插件
评“开发人员不喜欢低代码和无代码的8个理由”
JUC-实现Future
似然函数的意义
实验室常用工具 | 实验溶液配制 | 摩尔浓度及分子量计算工具 molarity-calculator
安全编码之基于栈的缓冲区溢出
GDI+ 中图像基本变换
Redisssion use
. Net basic knowledge quick pass 11
How does PostgreSQL speed up the recovery of transaction IDS through the vacuum
ORM框架简介和常用ORM框架
谁说Redis不能存大key
Keil5mdk installation (free)
kolla-ansible openstack登录 证书不可用
Zotero | Literature Association
Detailed explanation of LP mobile mining system development ecosystem
Software project management