微信小程序Vant获取用户信息及手机号

十年河东,十年河西,莫欺少年穷

学无止境,精益求精

wxml: 开放能力:获取用户信息 及 获取用户手机号

<van-dialog id="van-dialog" bind:confirm="confirm" bind:getuserinfo="getuserinfo"  bind:getphonenumber="getphonenumber"/>

page.json 引入组件

{
  "usingComponents": {
    "van-dialog": "@vant/weapp/dialog/index"
  }
}

page.JS 

// pages/userInfo/userInfo.js/
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   this.GetUser();
  
  },
GetUser(){
  let that=this;
  Dialog.alert({
    title: '登录',
    message: '是否公开您的信息',
    confirmButtonOpenType:"getUserInfo", // 按钮的微信开放能力
    showCancelButton:true,
    confirmButtonText:"授权登录"
  }).then(() => {
  
  }).catch(()=>{
    
  });
},

  confirm(){
    console.log('confirm')
  },
  
  /**授权-新接口 */
  getuserinfo(e) {
    console.log(e)
    wx.setStorageSync('userInfo', e.detail.userInfo)
    // wx.getUserInfo的返回兼容
    wx.setStorageSync('encryptedData', e.detail.encryptedData)
    wx.setStorageSync('iv', e.detail.iv)
    //拿到用户信息后 获取 用户手机号
    this.GetUserPhone();
  },

  
GetUserPhone(){
  let that=this;
  Dialog.alert({
    title: '登录',
    message: '是否允许获取您的手机号',
    confirmButtonOpenType:"getPhoneNumber", // 按钮的微信开放能力
    showCancelButton:true,
    confirmButtonText:"授权"
  }).then(() => {
   
  }).catch(()=>{
    
  });
},

getphonenumber(e){
  console.log(e)
}
})

@天才卧龙的博客

原文地址:https://www.cnblogs.com/chenwolong/p/15568762.html