小程序獲取用戶信息的不同方式


使用標簽open-data ,和屬性type(wxml)

可在wxml顯示用戶信息(自己的), 只能顯示,但不能獲取信息

type:

  userAvatarUrl(用戶頭像)、

  userNickName(用戶昵稱)、

   userCountry(用戶國家)、

  userCity(用戶城市

例如:

 

<open-data type="userNickName"></open-data>

 

<open-data type="userCountry"></open-data>

 

<open-data type="userCity"></open-data>

 

<open-data type="userAvatarUrl"></open-data> <!--獲取用戶的頭像並顯示-->

 

使用wx.getUserInfo(js)

注意:新版的wx.getUserInfo,不在彈出授權提示框,如果在未授權情況下使用--獲取失敗,使用先確保已授權

wx.getUserInfo({
success: (res) => {
console.log("getUserInfo success:",res)
},
fail:(res)=>{
console.log("getUserInfo fail:", res)
}
})
 
已授權(.userInfo獲取到用戶信息):

//getUserInfo success:{

//此內容同下下下下下圖中 detail對象的內容

}

未授權:

//getUserInfo fail: {errMsg: "getUserInfo:fail scope unauthorized"}

 

使用授權按鈕--獲取用戶授權

  1. button標簽, 
  2. 屬性open-type="getUserInfo",
  3. 綁定事件處理出程序xxx,bindgetuserinfo="xxx"

wxml:

<button open-type="getUserInfo" bindgetuserinfo="onGetUserInfo">獲取用戶信息</button>
js:
onGetUserInfo(event){
  console.log("onGetUserInfo event:",event)
}
操作點擊按鈕彈出授權框
 
拒絕如下:

 

授權如下(detail.userInfo獲取到用戶信息):

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM