微信小程序授权 getuserinfo 接口调整

  • 使用 button 组件,并将 open-type 指定为 getUserInfo 类型,用户允许授权后,可获取用户基本信息

  • 提示一下

 wx.showModal({
      title: '注意',
      showCancel: false,
      confirmText:'好去授权',
      content: '为了您更好的体验,请先同意授权',
      success: function (res) { 
        wx.navigateTo({
           url: '../userinfo/index'
        });
      }
    })  
<button  open-type="getUserInfo" bindgetuserinfo="bindgetuserinfo">点击授权</button>
  • 没有判断是否授权过,只是测试
  bindgetuserinfo: function (e) {
    var that = this;
    if (e.detail.userInfo) {
      // 发送 res.code 到后台换取 openId, sessionKey, unionId
      wx.login({
        success: res => {
          console.log(res.code, e.detail.iv,e.detail.encryptedData)
          wx.request({
            //后台接口地址
            url: '',
            data: {
              code:res.code,
              iv:e.detail.iv,
              encryptedData:e.detail.encryptedData,
            },
            method: 'GET',
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log('请求成功')
            }
          })
        }
      })  
    } else {
      console.log(333,'执行到这里,说明拒绝了授权')
      wx.showToast({
        title: "为了您更好的体验,请先同意授权",
        icon: 'none',
        duration: 2000
      });
    }
  }
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值