概述
本篇文档旨在指导开发者如何在微搭小程序应用中获取用户的微信昵称和头像信息。目前微搭已经将常用的小程序开放能力进行封装,开发者可以轻松实现相关功能的实现,进而提升开发效率。
实现流程
1. 获取头像。
在编辑中选择上传图片组件,图片内容属性中打开小程序端获取微信头像开关,图片形状选择圆形(可根据实际场景调整)。
![](https://qcloudimg.tencent-cloud.cn/image/document/a70a1d89b7608dd1b4ad52eef2208ab1.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/a70a1d89b7608dd1b4ad52eef2208ab1.png)
2. 获取昵称。
在编辑器中选择输入框组件,输入框属性中打开小程序端获取微信昵称开关。
![](https://qcloudimg.tencent-cloud.cn/image/document/283d9793b3967a8718db3894b97d1df8.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/283d9793b3967a8718db3894b97d1df8.png)
3. 存储信息。
新建数据模型微信用户用来存储用户的微信头像、昵称信息,并将字段 wxUid 关联系统用户表(标识:sys_user)。
![](https://qcloudimg.tencent-cloud.cn/image/document/c92469cd4d0daa2a6322566d241b1f24.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/c92469cd4d0daa2a6322566d241b1f24.png)
4. 事件配置。
按钮组件绑定数据源方法事件,具体配置如下,其中
$w.input1.value
为微信昵称,$w.uploadImage1.value
为微信头像,$w.auth.currentUser.userId
为当前微搭用户 ID。![](https://qcloudimg.tencent-cloud.cn/image/document/c243e76e50cbd163d203ffeeee976bbc.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/c243e76e50cbd163d203ffeeee976bbc.png)
5. 真机预览。
5.1 发布应用,手机扫码预览。
![](https://qcloudimg.tencent-cloud.cn/image/document/1a5a5812b8dbc3a9bbeba0124c24f58a.PNG)
5.2 单击图片上传图标,出现微信头像弹层,选择用微信头像。
![](https://qcloudimg.tencent-cloud.cn/image/document/560d1ff00ba05717e2c5a08a8daf0e66.PNG)
5.3 单击输入框,出现微信昵称提示弹层,选择使用微信昵称。
![](https://qcloudimg.tencent-cloud.cn/image/document/1906196273f9c771e12d0e6864f37842.PNG)
5.4 用户微信昵称、头像展示效果。
![](https://qcloudimg.tencent-cloud.cn/image/document/7822418a72c746fe7c5379793cccc8e7.PNG)
5.5 单击编辑,将用户信息存储到数据模型。
![](https://qcloudimg.tencent-cloud.cn/image/document/985638940b3870523f5596f540e68be0.PNG)
5.6 数据管理查看数据。
![](https://qcloudimg.tencent-cloud.cn/image/document/f08e690f19ef00876a3a865ffb2d1135.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/f08e690f19ef00876a3a865ffb2d1135.png)
注意事项
获取用户信息需要用户主动授权,开发者应尊重用户的隐私。
应妥善保管用户信息,不得用于非授权的用途。
微搭提供的 API 接口可能会根据政策调整,开发者应及时关注最新动态。