微信小程序:獲取用戶信息(昵稱和頭像)
微信小程序獲取用戶信息的接口幾經變更,建議直接使用方式四: wx.getUserProfile獲取
目錄
方式一:open-data展示用戶信息`不推薦`
方式二: wx.getUserInfo `不推薦`
方式三:open-type="getUserInfo" `不推薦`
方式四:wx.getUserProfile `推薦`
方式一:open-data展示用戶信息不推薦
組件功能調整為優(yōu)化用戶體驗,平臺將于2022年2月21日24時起回收通過展示個人信息的能力。
如有使用該技術服務,請開發(fā)者及時對小程序進行調整,避免影響服務流程。查看詳情:
https://developers.weixin.qq.com/community/develop/doc/000e881c7046a8fa1f4d464105b001
1
2
3
4
方式二: wx.getUserInfo 不推薦
小程序登錄、用戶信息相關接口調整說明:
https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801
Page({ onLoad: function (options) { this.getUserInfo(); }, async getUserInfo() { // 可以直接調用,無需用戶授權 const res = await wx.getUserInfo(); console.log(res); }, });
1
2
3
4
5
6
7
8
9
10
11
獲取的數(shù)據(jù)
{ userInfo{ avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132" city: "" country: "" gender: 0 language: "" nickName: "微信用戶" province: "" } }
1
2
3
4
5
6
7
8
9
10
11
調用后發(fā)現(xiàn),獲取的數(shù)據(jù)已經不是真實的用戶數(shù)據(jù)了
方式三:open-type=“getUserInfo” 不推薦
調用后發(fā)現(xiàn),獲取的數(shù)據(jù)已經不是真實的用戶數(shù)據(jù)了
bug:開發(fā)者工具中 2.10.4~2.16.1 基礎庫版本通過 會返回真實數(shù)據(jù),真機上此區(qū)間會按照公告返回匿名數(shù)據(jù)。
查看公告:
https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html#Bug-Tip
wxml
1
2
js
Page({ handleGetUserinfo(e) { console.log(e); }, });
1
2
3
4
5
6
輸出
{ detail{ userInfo:{ avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132" city: "" country: "" gender: 0 language: "" nickName: "微信用戶" province: "" } } }
1
2
3
4
5
6
7
8
9
10
11
12
13
方式四:wx.getUserProfile 推薦
獲取用戶信息。頁面產生點擊事件(例如 button 上 bindtap 的回調中)后才可調用,每次請求都會彈出授權窗口,用戶同意后返回 userInfo。該接口用于替換 wx.getUserInfo
文檔:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html
用戶數(shù)據(jù)結構 UserInfo : https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/UserInfo.html
1
Page({ async getUserProfile(e) { const res = await wx.getUserProfile({ desc: '用于完善會員資料', }); console.log(res); }, });
1
2
3
4
5
6
7
8
輸出
{ detail{ userInfo:{ avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132" city: "" country: "" gender: 0 language: "" nickName: "真實昵稱" province: "" } } }
1
2
3
4
5
6
7
8
9
10
11
12
13
小程序
版權聲明:本文內容由網(wǎng)絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內刪除侵權內容。
版權聲明:本文內容由網(wǎng)絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內刪除侵權內容。