微信小程序:获取用户信息(昵称和头像)

网友投稿 1178 2022-05-30

微信小程序获取用户信息的接口几经变更,建议直接使用方式四: wx.getUserProfile获取

目录

方式一:open-data展示用户信息`不推荐`

方式二: wx.getUserInfo `不推荐`

方式三:open-type="getUserInfo" `不推荐`

方式四:wx.getUserProfile `推荐`

方式一:open-data展示用户信息不推荐

组件功能调整为优化用户体验,平台将于2022年2月21日24时起回收通过展示个人信息的能力。

如有使用该技术服务,请开发者及时对小程序进行调整,避免影响服务流程。查看详情:

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

获取的数据

{ 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

调用后发现,获取的数据已经不是真实的用户数据了

方式三:open-type=“getUserInfo” 不推荐

调用后发现,获取的数据已经不是真实的用户数据了

bug:开发者工具中 2.10.4~2.16.1 基础库版本通过 会返回真实数据,真机上此区间会按照公告返回匿名数据。

查看公告:

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

用户数据结构 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

小程序

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【STM32平衡小车】线性CCD( TSL1401CL)巡线
下一篇:C语言之动态内存开辟
相关文章