微信小程序獲取用戶信息的接口幾經(jīng)變更,建議直接使用方式四: wx.getUserProfile
獲取
方式一:open-data展示用戶信息不推薦
組件功能調(diào)整為優(yōu)化用戶體驗(yàn),平臺(tái)將于2022年2月21日24時(shí)起回收通過展示個(gè)人信息的能力。
如有使用該技術(shù)服務(wù),請(qǐng)開發(fā)者及時(shí)對(duì)小程序進(jìn)行調(diào)整,避免影響服務(wù)流程。查看詳情:
https://developers.weixin.qq.com/community/develop/doc/000e881c7046a8fa1f4d464105b001
<!-- 如果只是展示用戶頭像昵稱,可以使用 <open-data /> 組件 -->
<open-data type="userAvatarUrl"></open-data>
<open-data type="userNickName"></open-data>
方式二: wx.getUserInfo 不推薦
小程序登錄、用戶信息相關(guān)接口調(diào)整說明:
https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801
Page({
onLoad: function (options) {
this.getUserInfo();
},
async getUserInfo() {
// 可以直接調(diào)用,無需用戶授權(quán)
const res = await wx.getUserInfo();
console.log(res);
},
});
獲取的數(shù)據(jù)
{
userInfo{
avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132"
city: ""
country: ""
gender: 0
language: ""
nickName: "微信用戶"
province: ""
}
}
調(diào)用后發(fā)現(xiàn),獲取的數(shù)據(jù)已經(jīng)不是真實(shí)的用戶數(shù)據(jù)了
方式三:open-type=“getUserInfo” 不推薦
調(diào)用后發(fā)現(xiàn),獲取的數(shù)據(jù)已經(jīng)不是真實(shí)的用戶數(shù)據(jù)了
bug:開發(fā)者工具中 2.10.4~2.16.1 基礎(chǔ)庫(kù)版本通過 會(huì)返回真實(shí)數(shù)據(jù),真機(jī)上此區(qū)間會(huì)按照公告返回匿名數(shù)據(jù)。
查看公告:
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html#Bug-Tip
wxml
<button open-type="getUserInfo"
bind:getuserinfo="handleGetUserinfo">獲取用戶信息</button>
js
Page({
handleGetUserinfo(e) {
console.log(e);
},
});
輸出
{
detail{
userInfo:{
avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132"
city: ""
country: ""
gender: 0
language: ""
nickName: "微信用戶"
province: ""
}
}
}
方式四:wx.getUserProfile 推薦
獲取用戶信息。頁(yè)面產(chǎn)生點(diǎn)擊事件(例如 button 上 bindtap 的回調(diào)中)后才可調(diào)用,每次請(qǐng)求都會(huì)彈出授權(quán)窗口,用戶同意后返回 userInfo。該接口用于替換 wx.getUserInfo
文檔:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html
用戶數(shù)據(jù)結(jié)構(gòu) UserInfo : https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/UserInfo.html文章來源:http://www.zghlxwxcb.cn/news/detail-403564.html
<button bindtap="getUserProfile">獲取頭像昵稱</button>
Page({
async getUserProfile(e) {
const res = await wx.getUserProfile({
desc: '用于完善會(huì)員資料',
});
console.log(res);
},
});
輸出文章來源地址http://www.zghlxwxcb.cn/news/detail-403564.html
{
detail{
userInfo:{
avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132"
city: ""
country: ""
gender: 0
language: ""
nickName: "真實(shí)昵稱"
province: ""
}
}
}
到了這里,關(guān)于微信小程序:獲取用戶信息(昵稱和頭像)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!