微信小程序获取用户信息的两种方法wx.getUserInfo&open-data

在此之前,小程序获取微信的头像,昵称之类的用户信息,我用的都是wx.getUserInfo,例如:

onLoad: function (options) {

    var that = this;

    //获取用户信息
    wx.getUserInfo({
        success: function (res) {

            console.log(res);
            that.data.userInfo = res.userInfo;

            that.setData({
                userInfo: that.data.userInfo
            })
        }
    })
},

wx.getUserInfo需要用户授权 scope.userInfo,也就是那个授权弹窗。

但是!!!重点来了,自从微信接口有了新的调整之后 这个wx.getUserInfo()便不再出现授权弹窗了,需要使用button做引导~

<!--wxml-->

<!-- 需要使用 button 来授权登录 -->
<button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授权登录</button>
<view wx:else>请升级微信版本</view>
//js
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function() {
    // 查看是否授权
    wx.getSetting({
      success: function(res){
        if (res.authSetting['scope.userInfo']) {
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function(res) {
              console.log(res.userInfo)
            }
          })
        }
      }
    })
  },
  bindGetUserInfo: function(e) {
    console.log(e.detail.userInfo)
  }
})

这就是等于一步变两步了~现在用button的话 可以在产品上多加引导,不会显得那么突兀的出来一个弹窗了

然鹅,如果你仅仅只是想展示用户信息的话,那便使用open-data 吧,如下:
   

<!-- 如果只是展示用户头像昵称,可以使用 <open-data /> 组件 -->
<open-data type="userAvatarUrl"></open-data>
<open-data type="userNickName"></open-data>

只需要这两行wxml的代码,便可展示微信昵称和头像,是不是很惊喜!羡慕

 

 

  • 16
    点赞
  • 120
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
回答: 在微信小程序中,有多种方式可以获取用户信息。其中一种方式是通过`<button open-type="getUserInfo" bindgetuserinfo="getUserInfo"></button>`来获取用户信息。这是微信小程序推荐的用法,可以直接唤起授权弹窗,返回的`e`中包含了获取到的用户信息。\[1\] 另一种方式是使用`wx.getUserProfile`接口。区别于`wx.getUserInfo`,`wx.getUserProfile`会返回用户授权的头像和昵称,而不仅仅是匿名信息。每次调用`wx.getUserProfile`都会弹窗,用户确认允许后才可以获取用户信息获取到的用户信息可以存储在自己的服务器上,以免弹窗过多影响用户体验。需要注意的是,`wx.getUserProfile`接口返回的`iv`和`encryptedData`无法解密获取到`unionid`,需要使用`wx.login`接口获取`unionid`。\[2\] 此外,还可以通过`<open-data>`组件来获取用户信息。例如,可以使用`<open-data type="userAvatarUrl"></open-data>`来获取用户的头像URL,使用`<open-data type="userNickName"></open-data>`来获取用户的昵称,以及使用其他类型的`<open-data>`来获取用户的国家和城市等信息。\[3\] #### 引用[.reference_title] - *1* *3* [微信小程序三种获取用户信息的方式](https://blog.csdn.net/weixin_43452467/article/details/108969160)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [微信小程序获取用户信息(getUserProfile)](https://blog.csdn.net/weixin_44989478/article/details/116119365)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值