这个接口只能获得一些非敏感信息,例如用户昵称,用户头像,经过用户授权允许获取的情况下即可获得用户信息,至于openid这些,需要调取wx.login来获取。

index.wxml

<!-- 当已经授权的时候 -->
<view wx:if="{{result == 'ok'}}" class="result">
<view class="headimg">
<image src="{{avatarUrl}}"></image>
</view>
<view class="nickname">{{nickName}}</view>
</view>
<!-- 当未授权的时候 -->
<view wx:else class="result">
<view>未授权</view>
<button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授权登录</button>
</view>

index.js

Page({
data: {
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
onLoad: function() {
var that = this;
// 查看是否授权
wx.getSetting({
success (res){
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
wx.getUserInfo({
success: function(res) {
console.log(res.userInfo)
that.setData({
result:'ok',// 结果
nickName:res.userInfo.nickName,// 微信昵称
avatarUrl:res.userInfo.avatarUrl,// 微信头像
})
}
})
}else{
// 未授权,结果返回null
that.setData({
result:'null',// 结果
})
}
}
})
},
// 请求API授权,获得用户头像和昵称
bindGetUserInfo (e) {
console.log(e.detail.userInfo.nickName)
var that = this;
that.setData({
result:'ok',// 结果
nickName:e.detail.userInfo.nickName,// 微信昵称
avatarUrl:e.detail.userInfo.avatarUrl,// 微信头像
})
}
})

index.wxss

button{
margin:30px auto 0;
}
.result{
width:200px;
margin:20px auto;
text-align: center;
}
.result .headimg{
width:200px;
height: 200px;
border-radius: 100px;
margin-bottom: 20px;
}
.result .headimg image{
width:200px;
height: 200px;
border-radius: 100px;
}

Author:TANKING

Web:http://www.likeyun.cn/

Date:2020-08-19

WeChat:face6009

微信小程序wx.getUserInfo授权获取用户信息(头像、昵称)的更多相关文章

  1. 微信小程序~App.js中获取用户信息

    (1)代码:主要介绍下获取用户信息部分 onLaunch: function () { // 展示本地存储能力 var logs = wx.getStorageSync('logs') || [] l ...

  2. 微信小程序-form表单-获取用户输入文本框的值

    微信小程序-form表单-获取用户输入文本框的值 <input name='formnickname' class="textarea" placeholder=" ...

  3. 微信小程序wx.showActionSheet调用客服信息功能

    微信小程序wx.showActionSheet调用客服消息功能 官方文档的代码: wx.showActionSheet({ itemList: ['A', 'B', 'C'], success (re ...

  4. 小程序app.onLaunch中获取用户信息,index.onLoad初次载入时取不到值的问题

    问题描述: //app.js App({ globalData:{ nickname:'' }, onLaunch: function () { let that=this; //假设已经授权成功 w ...

  5. 微信小程序~wx.getUserInfo逐渐废弃,小程序登录过程将如何优化?

    很多的时候我们在做小程序应用的时候,希望用户在使用小程序前进行登录授权,之前登录后通过wx.getUserInfo直接弹出授权的登录方式官方的意思是将不再支持,而是让用户通过下面的方式授权用户信息 & ...

  6. 微信小程序云开发-数据库-获取用户添加的数据到数据库

    一.列表页面新增[添加商品]按钮 在列表页增加[添加商品]按钮,按钮绑定事件toAdd(),用户点击该按钮跳转到添加商品页面. 在js文件中写toAdd()函数,作用是点击[添加商品]按钮,跳转到[添 ...

  7. 微信小程序wx.request POST获取不到数据解决办法

    get //发起请求     wx.request({       url: 'http://www.xiaochengxu.com/home/index/curd', //仅为示例,并非真实的接口地 ...

  8. 微信小程序 wx.getUserInfo 解密 C# 代码

    花了6小时,弄出来的代码.网上的是PHP代码 public static string DecodeUserInfo(string raw, string signature,string encry ...

  9. 微信小程序 wx.getUserInfo 解密 C# 代码 - 转

    public static string DecodeUserInfo(string raw, string signature,string encryptedData, string iv) { ...

  10. ***小程序wx.getUserInfo不能弹出授权窗口后的解决方案

    微信更新api后,wx.getUserInfo在开发和体验版本都不能弹出授权窗口.微信文档说明: 注意:此接口有调整,使用该接口将不再出现授权弹窗,请使用 <button open-type=& ...

随机推荐

  1. Springcloud开发之OpenFeign调用和认证

    SpringCloud开发cloud具有巨大的灵活性. 在调用其它服务的时候有多种方式,虽然本质一样,但是细节还是有所差异. 一.概述 当a服务调用b服务的时候有多种方式进行: 1.通过openFei ...

  2. cv2 判断图片是冷还是暖

    把图片的颜色空间转为HSV H表示色调(下图横轴), 图片的平均H值可用于区分冷暖

  3. Linux 内核:设备树(3)把device_node转换成platfrom_device

    Linux 内核:设备树(3)把device_node转换成platfrom_device 背景 在上一节中讲到设备树dtb文件中的各个节点转换成device_node的过程(<dtb转换成de ...

  4. 高通平台Android设备各种开机模式的进入与退出

    原文地址:https://www.cnblogs.com/we-hjb/p/9784659.html 高通方案的Android设备主要有以下几种开机模式,Android.EDL.Fastboot.Re ...

  5. Linux上快速安装 RabbitMQ

    1.默认安装最新版,安装erlang apt-get install erlang 2.安装最新版 rabbitmq sudo apt-get update sudo apt-get install ...

  6. Codeforces Round 935 (Div. 3)

    A. Setting up Camp 题目描述 The organizing committee plans to take the participants of the Olympiad on a ...

  7. JavaSE 计算2个List集合中的交集、差集、并集、去重并集

    VideoOrder.java 重写里面的equals和hashCode方法 class VideoOrder { private int price; private String title; p ...

  8. Linux 鉴定故障

    导读 进入linux,输入root账户,密码输入是正确的,提示"鉴定故障",刚开始以为是系统挂了,后来百度说,需要重置root密码,具体步骤如下. 重置root密码 重启Linux ...

  9. Maven Helper插件——实现一键Maven依赖冲突问题

    业余在一个SpringBoot项目集成Swagger2时,启动过程一直出现以下报错信息-- An attempt was made to call a method that does not exi ...

  10. 解决方案 | Chrome/Edge 总是自动修改我的pdf默认打开方式

    1.问题描述 最近我的pdf文件总是被chrome打开(如图1),而且点击属性,更改别的pdf阅读器也不管用(如图2),此时的chrome就像个流氓软件一样. 图1 被chrome劫持 图2 点击属性 ...