Applet micro-channel user information acquired ~ App.js

(1) Code: acquiring the user information section describes

  OnLaunch: function () {
     // showing local storage capability 
    var logs wx.getStorageSync = ( ' logs ' ) || [] 
    logs.unshift (Date.now ()) 
    wx.setStorageSync ( ' logs ' , logs) 

    // login 
    wx.login ({ 
      Success: RES => {
         // send back to the exchange res.code OpenID, a sessionKey, unionid 
      } 
    }) 
    // get user information 
    wx.getSetting ({ 
      Success: RES => {
         IF (res.authSetting [ ' scope.userInfo ']) {
           // has authorized, you can directly call getUserInfo head nickname acquired, can not play the block 
          wx.getUserInfo ({ 
            Success: res => {
               // may be sent back to the decoding res unionid 
              the this .globalData.userInfo = res. the userInfo 

              // Since getUserInfo network request, may not return until Page.onLoad
               // so here is added to prevent this callback 
              IF ( the this .userInfoReadyCallback) {
                 the this .userInfoReadyCallback (RES) 
              } 
            } 
          }) 
        } 
      } 
    } )

    onLaunch small lifecycle --- small program initialization

   

 

(2) the user data acquired

      wx.getSetting ({...}) or wx.openSetting ({...})

     Developers can call wx.openSetting open the settings interface guides the user to open the authorization

     parameter:

    

    obj.success success callback function parameters:

  

wx.openSetting({
    success:function(res){
        
    }
})
或者

wx.openSetting({
    success=>res{

    }
})

AuthSetting

User authorization settings information

scope The corresponding interface description
scope.userInfo wx.getUserInfo User Info
scope.userLocation wx.getLocationwx.chooseLocation Location
scope.address wx.chooseAddress mailing address
scope.invoiceTitle wx.chooseInvoiceTitle Invoice
scope.invoice wx.chooseInvoice Get invoice
scope.werun wx.getWeRunData Movement micro-channel number of steps
scope.record wx.startRecord recording function
scope.writePhotosAlbum wx.saveImageToPhotosAlbumwx.saveVideoToPhotosAlbum save into the album
scope.camera camera  assembly camera

 

Code:

    // obtain user information 
    wx.openSetting ({ 
/ * interface call success callback function * / Success: RES
=> {
/ * Success success callback function parameters: * / authorSetting Object
/ * authSetting one object properties: scope.userInfo authorized user information * /
iF (res.authSetting [ ' scope.userInfo ' ]) { /*scope.userInfo (whether user authorization information, the corresponding interface wx.getUserInfo) * /
wx.getUserInfo ({
}) } } })

 

(3) obtaining user information

    wx.getUserInfo (Object object) to get user information

    parameter:

    

    Callback function parameters object.success

   

    // 获取用户信息
    wx.openSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 必须是在用户已经授权的情况下调用
          wx.getUserInfo({
            success: res => {
              // globalData函数设置全局变量,方便其他页面调用。
              this.globalData.userInfo = res.userInfo
              // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })

    globalData函数设置全局变量,详见下篇文章使用globalData函数设置全局变量

 

 

 

 

 

 

 

.

Guess you like

Origin www.cnblogs.com/jianxian/p/11111484.html