0
点赞
收藏
分享

微信扫一扫

wx.getUserInfo 与 getUserProfile

罗蓁蓁 2022-03-24 阅读 53

以前wx.getUserInfo可以获得 微信唯一认证ID(openID),微信信息(头像,微信名字,注册程序),后来一些原因,官方改成:

wx.getUserInfo获得:通过后台解密获得openID,不需要通过弹框

getUserProfile获得: 微信信息(头像,微信名字,注册程序),必须要通过弹框

    //2、调用获取用户信息接口
              wx.getUserInfo({
                success: function (res) { 
                  console.log("到这里来了吗")
                  console.log(res.userInfo)
                  console.log(res.encryptedData)
                  console.log(res.iv)
                  wx.setStorageSync('dl_encryptedData', res.encryptedData)
                  wx.setStorageSync('dl_iv', res.iv)
                  getApp().globalData.userInfo = res.userInfo 
                  //3.请求自己的服务器,解密用户信息 获取unionId等加密信息
                  wx.request({
                    url: 'https://app.601.cn/601_dj/user.asmx/ProcessRequest',//自己的服务接口地址
                    method: 'post',
                    header: {
                      'content-type': 'application/x-www-form-urlencoded'
                    },
                    data: {
                      encryptedData: res.encryptedData,
                      iv: res.iv,
                      code: code
                    },
                    success: function (data) {
                      console.log('开始存储') 
                      console.log(data.data)
                      console.log('看看工号的值')
                      console.log(data.data[0].gonghao)             
                      wx.setStorageSync('openId', data.data[0].OpenId)
                      wx.setStorageSync('nickName', getApp().globalData.userInfo.nickName)
                      wx.setStorageSync('avatarUrl',getApp().globalData.userInfo.AvatarUrl)
                      wx.setStorageSync('gonghao', data.data[0].gonghao)
                      wx.setStorageSync('sjh', data.data[0].sjh) 
                      wx.setStorageSync('bmid', data.data[0].bmid) 
                    },
                    fail: function () {
                      console.log('系统错误')
                    }
                  })
                }
              })
<button  bindtap="getUserProfile"> 获取头像昵称 </button>

    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
      success: (res) => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })

个人在实际应用中wx.getUserInfo一般必然用到,特别给企业做外围应用的时候,企业一般有自己的人员数据表,这时候只要通过第一次验证,绑定openid就能完成“下次登录记忆,知道你是谁”,即使没有自己的人员信息表,要保存用户,也需要调用wx.getUserInfo来获得openid.

getUserProfile获得微信用户名,头像在一些用“微信名注册会员”的场景中可能用到,它会弹出弹框,而且用户还有可能选择否,不是很友好,为了不每次都弹框,需要把获得的信息保存下来(但是这里用户更换了头像,是无法用原来保存的路径显示的),这种情况,个人更加喜欢填充一个用户名,头像,用户自己可以完善修改,不太喜欢用getUserProfile。

举报

相关推荐

0 条评论