微信小程序获取用户信息

为优化用户体验,使用 wx.getUserInfo 接口直接弹出授权框的开发方式将逐步不再支持。

oh~my god!!!

开发者可使用以下方式获取或展示用户信息:


一、小程序:

1、使用 button 组件,并将 open-type 指定为 getUserInfo 类型,获取用户基本信息。

详情参考文档:

https://developers.weixin.qq.com/miniprogram/dev/component/button.html


2、使用 open-data 展示用户基本信息。

详情参考文档:

https://developers.weixin.qq.com/miniprogram/dev/component/open-data.html


二、小游戏:

1、使用用户信息按钮 UserInfoButton。

详情参考文档:

https://developers.weixin.qq.com/minigame/dev/document/open-api/user-info/wx.createUserInfoButton.html


2、开放数据域下的展示用户信息。

详细参考文档:

https://developers.weixin.qq.com/minigame/dev/document/open-api/data/wx.getUserInfo.html


用button来获取用户基本信息的代码如下:

<view class="container">
  <view class="userinfo">
<button wx:if="{{ !hasUserInfo && canIUse}}"   open-type="getUserInfo"  bindgetuserinfo="onGetUserinfo"
>授权登录</button>
    <block wx:else>
      <image class="userinfo-avatar" src="{{userInfo.avatarUrl}}" mode="cover"></image>
      <text class="userinfo-nickname">{{userInfo.nickName}}</text>
    </block>
  </view>
</view>
/**index.wxss**/
.userinfo {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.userinfo-avatar {
  width: 128rpx;
  height: 128rpx;
  margin: 20rpx;
  border-radius: 50%;
}

.userinfo-nickname {
  color: #aaa;
}

.usermotto {
  margin-top: 200px;
}
//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {

    // 查看是否授权
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  onGetUserinfo: function(e) {
    console.log(e.detail.userInfo)
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      console.log("允许")
      console.log(e.detail.errMsg)
      console.log(e.detail.userInfo)
      console.log(e.detail.rawData)
      app.globalData.userInfo = e.detail.userInfo
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })
    } else {
      //用户按了拒绝按钮
      console.log("拒绝")
    }
  }
})


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在微信小程序中,有多种方式可以获取用户信息。其中一种方式是通过`<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 ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值