小程序获取用户信息

是这样的,因为后台要求获取openid和unionid
这个的话只能通过 wx.getUserInfo进行获取,但是微信后期不授权了, 所以你要获取用户的信息,那么就要先wx.login在获取信息,然后要获取这两个id,还需要后台看微信文档给你一个接口,你把
vi:加密算法的初始向量
encryptedData:包括敏感数据在内的完整用户信息的加密数据
code:登录时获取的 code
这几个参数传给后台就可以了,然后后台根据这个参数进行把unionid传给你
可以进行代码先测试一下:

index.wxml

<button  open-type="getUserInfo"  bindgetuserinfo="getUserInfo">授权</button>

index.js

  getUserInfo() {
    // 登录
    wx.login({
      success: res => {
        console.log("success");
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        this.code = res.code
        // 获取用户信息
        wx.getSetting({
          success: res => {
            if (res.authSetting['scope.userInfo']) {
              // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
              wx.getUserInfo({
                success: res => {
                  var data = {
                    encryptedData: res.encryptedData,
                    iv: res.iv
                  }
                  // console.log("getuserinfo里面");
                  // console.log(res.userInfo);
                  this.wxLogin(data, res.userInfo)
                  // // 可以将 res 发送给后台解码出 unionId
                  this.globalData.userInfo = res.userInfo
                  console.log(this.globalData.userInfo)
                  // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
                  // 所以此处加入 callback 以防止这种情况
                  if (this.userInfoReadyCallback) {
                    this.userInfoReadyCallback(res)
                  }
                }
              })
            } else {
              // wx.hideTabBar()
            }
          }
        })
      }
    })
  },

  wxLogin(data, user, callback) {
    if (this.code) {
      data.code = this.code;
    }
    // console.log("data的数据");
    // import Request from '../../utils/request.js'
    // BjjSutrasList/getLessonInf
    Request.request({ method: "POST", url: 'BjjUser/wxlogin', data:data }).then(res => {
      console.log("res的数据");
      console.log(res);
    })



    // Request.request('https://bo.ibodhi.cn/bojingji/api/BjjUser/wxlogin', data, res => {
    //   this.code = null
    //   console.log('wxLogin', res.data)
    //   if (callback) {
    //     callback(res)
    //   }
    //   console.log(res);
    //   // if (res.data.code == 200) {
    //   //   this.globalData.userid = res.data.data.userid
    //   //   this.globalData.token = res.data.data.token
    //   //   var pages = getCurrentPages() //获取加载的页面 
    //   //   var currentPage = pages[pages.length - 1] //获取当前页面的对象 
    //   //   console.log(currentPage.route)
    //   //   var url = currentPage.route //当前页面 
    //   //   if (wx.getStorageSync('user').userid != res.data.data.userid) {
    //   //     wx.reLaunch({
    //   //       url: '/' + url + '?id=' + this.globalData.ac_id
    //   //     })
    //   //   }
    //   //   user.userid = res.data.data.userid
    //   //   user.email = res.data.data.email

    //   //   wx.setStorageSync('user', user)
    //   //   wx.showTabBar()

    //   //   this.wxRequest('api/jingturike/coursefenbiao/noReadNoticeCount', {
    //   //     userid: res.data.data.userid
    //   //   }, res => {
    //   //     // res.data.data = 3
    //   //     if (res.data.data > 0) {
    //   //       wx.showTabBarRedDot({
    //   //         index: 2,
    //   //         fail: function (res) {
    //   //           // console.log(res)
    //   //           setTimeout(function () {
    //   //             wx.showTabBarRedDot({
    //   //               index: 2,
    //   //               complete: function (res) {
    //   //                 console.log(res)
    //   //               }
    //   //             })
    //   //           }, 2000)
    //   //         }
    //   //       })
    //   //     }
    //   //   })
    //   // } else {
    //   //   if (res.data.code == -9) {
    //   //     var pages = getCurrentPages() //获取加载的页面 
    //   //     var currentPage = pages[pages.length - 1] //获取当前页面的对象 
    //   //     console.log(currentPage.route)
    //   //     var url = currentPage.route //当前页面 
    //   //     wx.reLaunch({
    //   //       url: '/pages/binding/binding?id=' + res.data.unionid + '&page=' + url
    //   //     })
    //   //     wx.showToast({
    //   //       title: res.data.msg,
    //   //       icon: 'none',
    //   //       mask: true
    //   //     })
    //   //   }
    //   //   wx.setStorageSync('user', user)
    //   //   wx.showTabBar()
    //   // }
    // })
  },
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值