微信小程序获取用户信息相关问题

获取用户信息

在微信小程序中,我们想要获取用户的信息时,我们可以通过微信开发文档中微信开放能力(open-type)中的合法值进行获取相应的用户数据
在这里插入图片描述

版本问题

在目前的最新稳定版的开发者工具中,使用open-type无法出现授权弹窗,这中方法只有在调试器版本在2.16以下才可以使用。
所以在最新版中只能使用wx.getUserProfile方法,实例代码如下:
在这里插入图片描述

实现例子

一、最新稳定版的使用wx.getUserProfile方法

在index.wxml文件中

<view class="box">
  <image class="images" src="{{userInfo.avatarUrl}}" alt="" srcset=""/>
  <view>{{userInfo.nickName}}</view>
  <!-- 关键代码 -->
  <button type="default" bindtap="getUserInfo" class="getBtn">获取用户信息</button>
  <text class="title">{{msg}}</text>
  <view class="content" catchtap="tologs">
    <text>hello world</text>
  </view>
</view>

在index.js文件中

Page({
  /**
   * 页面的初始数据
   */
  data: {
    msg:'初始化测试数据',
    userInfo:{
      nickName:'用户名',
      avatarUrl:'/public/images/logo_海龟.png'
    }
  },
  getUserInfo(){
    wx.getUserProfile({
      desc: '用来完善用户个人信息',//展示的消息
      success:(res)=>{
        console.log(res);
        this.setData({
          userInfo:res.userInfo      
        })
      }
    })
  },
})

二、使用open-type中getUserInfo

在index.wxml文件中

<view class="box">
  <image class="images" src="{{userInfo.avatarUrl}}" alt="" srcset=""/>
  <view>{{userInfo.nickName}}</view>
  <!-- 最新版本不支持,这种是2.16以下的 -->
  <!--获取用户信息,可以从 bindgetuserinfo 回调中获取到用户信息-->
  <button bindgetuserinfo="getUserInfo" open-type="getUserInfo">获取用户信息</button>
  <text class="title">{{msg}}</text>
  <view class="content" catchtap="tologs">
    <text>hello world</text>
  </view>
</view>

在index.js文件中

Page({
  /**
   * 页面的初始数据
   */
  data: {
    msg:'初始化测试数据',
    userInfo:{
      nickName:'用户名',
      avatarUrl:'/public/images/logo_海龟.png'
    }
  },
  getUserInfo(res){
    console.log(res);
    this.setData({
      userInfo:res.detail.userInfo
    })
  },
})

运行结果
在这里插入图片描述
在这里插入图片描述 在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龟中的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值