赶知识网

小程序获取用户信息接口调整

2018-05-17 / 3228次点击 Js/Css/jQuery


    为优化用户体验,使用 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

二. 我对接口修改的理解:
开发者可以根据需要来调取用户信息,而不是用户首次进入小程序就弹出授权的弹框,这样对于用户来说是不友好的。比如可以在用户点击登录的时候再获取用户信息,或者提交表单的时候等等,总之可以根据业务逻辑进行开发。

三. 然而对于我们项目的业务逻辑却是不好的事儿,呜呜。。。因为我们需要在一开始就获取用户的信息入库,相信很多人都会有这样的需求,那就记录一下我们项目的登录。

    首先自己写一个弹框,触发获取信息的内容
    小程序原生组件弹框没有可以编辑的按钮,所以需要我们自己来写
    如图:
    这里写图片描述
    代码如下:

<!-- 自定义弹框开始 -->
  <view wx:if="{{showModel}}" class="model">
    <view class="modelTitle">
      获取微信授权信息
    </view>
    <view class="modelBody">微信登录需要获取您的用户信息,请前往设置</view>
    <view class="btns">
      <button open-type="getUserInfo" class="agree" bindgetuserinfo="agreeGetUser" lang="zh_CN">去设置</button>
    </view>
  </view>
  <view wx:if="{{showModel}}" class="mask"></view>
  <!-- 自定义弹框结束 -->

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12

    判断用户是否授权
    判断是否授权,如果没有授权,那么需要自定义弹框显示,点击“去设置”,然后弹出授权框;如果已经授权,逻辑上就应该不再弹出任何框,这里就需要把用户首次进入小程序授权的用户信息存在本地然后那来使用

 // 登录
    wx.login({
      success: res => {
        app.globalData.code = res.code
        //取出本地存储用户信息,解决需要每次进入小程序弹框获取用户信息
        app.globalData.userInfo = wx.getStorageSync('userInfo')
        //wx.getuserinfo接口不再支持
        wx.getSetting({
          success: (res) => {
            //判断用户已经授权。不需要弹框
            if(!res.authSetting['scope.userInfo']){
              that.setData({
                showModel: true
              })
            }else{//没有授权需要弹框
              that.setData({
                showModel: false
              })
              wx.showLoading({
                title: '加载中...'
              })
              that.getOP(app.globalData.userInfo)
            }
          },
          fail: function () {
            wx.showToast({
              title: '系统提示:网络错误',
              icon: 'warn',
              duration: 1500,
            })
          }
        })
      },
      fail:function () {
        wx.showToast({
          title:'系统提示:网络错误',
          icon: 'warn',
          duration: 1500,
        })
      }
    })
  },
  //获取用户信息新接口
  agreeGetUser:function (e) {
    //设置用户信息本地存储
    try {
      wx.setStorageSync('userInfo', e.detail.userInfo)
    } catch (e) {
      wx.showToast({
        title: '系统提示:网络错误',
        icon: 'warn',
        duration: 1500,
      })
    }
    wx.showLoading({
      title:'加载中...'
    })
    let that = this
    that.setData({
      showModel:false
    })
    that.getOP(e.detail.userInfo)
  },
  getOP: function (res) {//提交用户信息 获取用户id
    let that = this
    let userInfo = res
    app.globalData.userInfo = userInfo
    wx.request({
      url: 'https://xcx.xiancaijun.cn/tigerlogin/tgLogin',
      method: 'post',
      data: {
        "code": app.globalData.code,
        'userInfo': userInfo
      },
      success: function (res) {
        if(res.data.respcode == '0'){
          app.globalData.userId = res.data.uid
          app.globalData.keys = res.data.keys
          app.globalData.access = res.data.access
          that.getBook()
          that.shareInfo()
          if (that.data.cid) {
            wx.navigateTo({
              url: '/pages/course/course?cid=' + that.data.cid
            })
          }
        }else if(res.data.respcode == '15'){
          wx.hideLoading()
          wx.showToast({
            title:'没有授权,不能进入小程序',
            icon:'none',
            duration:2000
          })
        }

      }
    })
  },

Top10

沪ICP备09053415号 © 赶知识网