微信小程序获取openID和userInfo

1. 获取openID(在app.js):

/**
   * 全局变量
   */
  globalData: {
    userInfo: null,
    appid: 'wx08**********2796', //appid
    secret: '64cd85a****************2c75dfd5', //secret
    amapKey: '6ef95**********************92021bdf14', //高德地图key
  },
/**获取OpenID */
  getOpenId: function () {
    var that = this
    wx.login({
      success: function (res) {
        if (res.code) {
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo);
              wx.setStorageSync('userInfo', res.userInfo); //存储userInfo
            }
          });
          //这里存储了appid、secret、token串  
          var url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' + that.globalData.appid + '&secret=' + that.globalData.secret + '&js_code=' + res.code + '&grant_type=authorization_code';
          wx.request({
            url: url,
            data: {},
            method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT  
            // header: {}, // 设置请求的 header  
            success: function (res) {
              var obj = {};
              obj.openid = res.data.openid;
              obj.expires_in = Date.now() + res.data.expires_in;
              console.log(obj);
              wx.setStorageSync('user', obj); //存储openid  
              that.globalData.openid = res.data.openid;
            }
          });
        } else {
          console.log('获取用户登录态失败!' + res.errMsg)
        }
      }
    });
  }

2. 获取userInfo:

/**获取用户信息 */
  getUserInfo: function () {
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              console.log("userInfo", res.userInfo)
              this.globalData.userInfo = res.userInfo;

              // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  }

另外,小程序官方对获取用户信息的说明:为优化用户体验,使用wx.getUserInfo 接口直接弹出授权框的开发方式将逐步不再支持。从2018年4月30日开始,小程序与小游戏的体验版、开发版调用 wx.getUserInfo接口,将无法弹出授权询问框,默认调用失败。正式版暂不受影响。开发者可使用以下方式获取或展示用户信息:

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

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
微信小程序userInfoopenid是用于标识用户的信息。其中openid是每个用户在小程序中的唯一标识,而userInfo则包含了用户的基本信息,如昵称、头像等。 要获取用户的userInfoopenid,需要进行微信授权。具体步骤如下: 1. 在小程序中引入微信登录模块: ``` var app = getApp(); var util = require('../../utils/util.js'); var api = require('../../config/api.js'); Page({ data: {}, onLoad: function(options) { // 获取用户信息 wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) { wx.getUserInfo({ success: res => { // 可以将 res 发送给后台解码出 unionId this.globalData.userInfo = res.userInfo // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 if (this.userInfoReadyCallback) { this.userInfoReadyCallback(res) } } }) } } }) }, }) ``` 2. 在小程序中添加授权按钮: ``` <button type="primary" open-type="getUserInfo" bindgetuserinfo="onGotUserInfo">授权登录</button> ``` 3. 在小程序中获取openid: ``` wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId wx.request({ url: api.AuthLoginByWeixin, data: { code: res.code }, success: function(res) { if (res.statusCode === 200) { // 将openid存储到本地 wx.setStorageSync('openid', res.data.openid); } else { console.log('请求失败') } } }) } }) ``` 通过以上步骤,就可以获取到用户的userInfoopenid了。其中,openid可以通过wx.getStorageSync('openid')获取

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

梦游人布拿拿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值