微信小程序wx.getUserInfo授權獲取用戶信息(頭像、昵稱)的實現(xiàn)
更新時間:2020年08月19日 11:32:44 作者:TANKING-
這篇文章主要介紹了微信小程序wx.getUserInfo授權獲取用戶信息(頭像、昵稱)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
這個接口只能獲得一些非敏感信息,例如用戶昵稱,用戶頭像,經(jīng)過用戶授權允許獲取的情況下即可獲得用戶信息,至于openid這些,需要調(diào)取wx.login來獲取。
index.wxml
<!-- 當已經(jīng)授權的時候 -->
<view wx:if="{{result == 'ok'}}" class="result">
<view class="headimg">
<image src="{{avatarUrl}}"></image>
</view>
<view class="nickname">{{nickName}}</view>
</view>
<!-- 當未授權的時候 -->
<view wx:else class="result">
<view>未授權</view>
<button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授權登錄</button>
</view>
index.js
Page({
data: {
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
onLoad: function() {
var that = this;
// 查看是否授權
wx.getSetting({
success (res){
if (res.authSetting['scope.userInfo']) {
// 已經(jīng)授權,可以直接調(diào)用 getUserInfo 獲取頭像昵稱
wx.getUserInfo({
success: function(res) {
console.log(res.userInfo)
that.setData({
result:'ok',// 結果
nickName:res.userInfo.nickName,// 微信昵稱
avatarUrl:res.userInfo.avatarUrl,// 微信頭像
})
}
})
}else{
// 未授權,結果返回null
that.setData({
result:'null',// 結果
})
}
}
})
},
// 請求API授權,獲得用戶頭像和昵稱
bindGetUserInfo (e) {
console.log(e.detail.userInfo.nickName)
var that = this;
that.setData({
result:'ok',// 結果
nickName:e.detail.userInfo.nickName,// 微信昵稱
avatarUrl:e.detail.userInfo.avatarUrl,// 微信頭像
})
}
})
index.wxss
button{
margin:30px auto 0;
}
.result{
width:200px;
margin:20px auto;
text-align: center;
}
.result .headimg{
width:200px;
height: 200px;
border-radius: 100px;
margin-bottom: 20px;
}
.result .headimg image{
width:200px;
height: 200px;
border-radius: 100px;
}

到此這篇關于微信小程序wx.getUserInfo授權獲取用戶信息(頭像、昵稱)的實現(xiàn)的文章就介紹到這了,更多相關小程序wx.getUserInfo授權內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
教你用js截取字符串開頭、結尾及兩字符串之間的內(nèi)容
在js中可以通過indexOf()方法找到指定的字符位置,再使用length屬性獲得字符串的長度,下面這篇文章主要給大家介紹了關于如何用js截取字符串開頭、結尾及兩字符串之間的內(nèi)容的相關資料,需要的朋友可以參考下2022-11-11
JS一分鐘在github+Jekyll的博客中添加訪問量功能的實現(xiàn)
這篇文章主要介紹了JS一分鐘在github+Jekyll的博客中添加訪問量功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04

