小程序弹出授权用户信息和手机号

授权用户信息和授权手机号,可分开使用,必须用按钮触发

<button bindgetuserinfo="login" open-type="getUserInfo">获取授权</button>
 //获取授权
    login:function(){
        var that=this;
        wx.getSetting({
            success: function(a) {
                a.authSetting["scope.userInfo"] && wx.getUserInfo({
                    success: function(a) {
                        wx.showToast({title: '授权成功'})
                    }
                });
            }
        });
    },
<button bindgetphonenumber="phone" open-type="getPhoneNumber">手机号授权</button>
//授权手机号
phone:function(e){
  console.log("授权用户手机号");
  console.log(e);
        var that = this;
        if(e.detail.errMsg=="getPhoneNumber:fail user deny"){
                wx.showToast({title: '已取消授权'})
        }
        if(e.detail.errMsg=="getPhoneNumber:ok"){
                var iv=e.detail.iv;
                var encryptedData=e.detail.encryptedData;
                app.util.request({      //手机号解密
                      url: "auth/session/mobile",
                      method:'post',
                      data: {
                          version: app.globalData.version,
                          encryptedData:encryptedData,
                          iv:iv
                      },
                      success: function(a) {          
                          if(a.data.errno==0){
                            wx.showToast({title: '授权成功'})
                          }                                           
                      }                
                });
        }
},

你可能感兴趣的:(前端,小程序)