• 最新uniApp微信小程序获取头像open-type=“chooseAvatar“ @chooseavatar方法


    小程序用户头像昵称获取规则调整公告

    调整说明

    自 2022 年 10 月 25 日 24 时后(以下统称 “生效期” ),用户头像昵称获取规则将进行如下调整:

    1. 自生效期起,小程序 wx.getUserProfile 接口将被收回:生效期后发布的小程序新版本,通过 wx.getUserProfile 接口获取用户头像将统一返回默认灰色头像,昵称将统一返回 “微信用户”。生效期前发布的小程序版本不受影响,但如果要进行版本更新则需要进行适配。
    2. 自生效期起,插件通过 wx.getUserInfo 接口获取用户昵称头像将被收回:生效期后发布的插件新版本,通过 wx.getUserInfo 接口获取用户头像将统一返回默认灰色头像,昵称将统一返回 “微信用户”。生效期前发布的插件版本不受影响,但如果要进行版本更新则需要进行适配。通过 wx.login 与 wx.getUserInfo 接口获取 openId、unionId 能力不受影响。
    3. 「头像昵称填写能力」支持获取用户头像昵称:如业务需获取用户头像昵称,可以使用「头像昵称填写能力」(基础库 2.21.2 版本开始支持,覆盖iOS与安卓微信 8.0.16 以上版本),具体实践可见下方《最佳实践》。
    4. 小程序 wx.getUserProfile 与插件 wx.getUserInfo 接口兼容基础库 2.27.1 以下版本的头像昵称获取需求:对于来自低版本的基础库与微信客户端的访问,小程序通过 wx.getUserProfile 接口将正常返回用户头像昵称,插件通过 wx.getUserInfo 接口将正常返回用户头像昵称,开发者可继续使用以上能力做向下兼容。

    对于上述 3,wx.getUserProfile 接口、wx.getUserInfo 接口、头像昵称填写能力的基础库版本支持能力详细对比见下表:

    以上是微信小程序对用户头像昵称更改的公告,从上文不能看出之前用到的wx.getUserProfile、wx.getUserInfo将被弃用,不在返回正确的用户昵称及头像信息。emm......完全看不懂为什么微信小程序总是揪着这个功能不放,一而再再而三的改动...

    新Api介绍及实战

    头像昵称填写

    从基础库 2.21.2 开始支持。

    需要将 button 组件 open-type 的值设置为 chooseAvatar,当用户选择需要使用的头像之后,可以通过 bindchooseavataruniApp使用@chooseavatar事件回调获取到头像信息的临时路径。

    项目实战以uniApp为例

    1. <template>
    2. <view class="containar">
    3. <view class="avatarUrl">
    4. <button type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseavatar">
    5. <image :src="avatarUrl" class="refreshIcon">image>
    6. button>
    7. view>
    8. <view class="nickname">
    9. <text>昵称:text>
    10. <input type="nickname" class="weui-input" :value="nickName" @blur="bindblur"
    11. placeholder="请输入昵称" @input="bindinput"/>
    12. view>
    13. <view class="btn">
    14. <view class="btn-sub" @click="onSubmit">保存view>
    15. view>
    16. view>
    17. template>
    1. export default {
    2. data() {
    3. return {
    4. avatarUrl: '',
    5. nickName: ''
    6. };
    7. },
    8. onLoad(option) {},
    9. methods: {
    10. bindblur(e) {
    11. this.nickName = e.detail.value; // 获取微信昵称
    12. },
    13. bindinput(e){
    14. this.nickName = e.detail.value; //这里要注意如果只用blur方法的话用户在输入玩昵称后直接点击保存按钮,会出现修改不成功的情况。
    15. },
    16. onChooseavatar(e) {
    17. let self = this;
    18. let {
    19. avatarUrl
    20. } = e.detail;
    21. uni.showLoading({
    22. title: '加载中'
    23. });
    24. uni.uploadFile({
    25. url: '后台uploadFile接口',
    26. filePath: avatarUrl,
    27. name: 'file',
    28. header: {
    29. token: '自己的token',
    30. },
    31. success: uploadFileRes => {
    32. // 注意:这里返回的uploadFileRes.data 为JSON 需要自己去转换
    33. let data = JSON.parse(uploadFileRes.data);
    34. if (data.code === 0) {
    35. this.avatarUrl = data.data;
    36. }
    37. },
    38. fail: (error) => {
    39. uni.showToast({
    40. title: error,
    41. duration: 2000
    42. });
    43. },
    44. complete: () => {
    45. uni.hideLoading();
    46. }
    47. });
    48. },
    49. onSubmit() {
    50. if(this.nickName === ''){
    51. uni.showToast({
    52. icon: 'none',
    53. title: '请输入昵称'
    54. })
    55. return false;
    56. }
    57. // 这里做自己公司的存储逻辑
    58. }
    59. }
    60. };
    1. .containar {
    2. .avatarUrl {
    3. padding: 80rpx 0 40rpx;
    4. background: #fff;
    5. button {
    6. background: #fff;
    7. line-height: 80rpx;
    8. height: auto;
    9. width: auto;
    10. padding: 20rpx 30rpx;
    11. margin: 0;
    12. display: flex;
    13. justify-content: center;
    14. align-items: center;
    15. .refreshIcon {
    16. width: 160rpx;
    17. height: 160rpx;
    18. border-radius: 50%;
    19. }
    20. .jt{
    21. width: 14rpx;
    22. height: 28rpx;
    23. }
    24. }
    25. }
    26. .nickname{
    27. background: #fff;
    28. padding: 20rpx 30rpx 80rpx;
    29. display: flex;
    30. align-items: center;
    31. justify-content: center;
    32. .weui-input{
    33. padding-left: 60rpx;
    34. }
    35. }
    36. .btn{
    37. width: 100%;
    38. .btn-sub{
    39. width: 670rpx;
    40. margin: 80rpx auto 0;
    41. height: 90rpx;
    42. background: #DF8585;
    43. border-radius: 45rpx;
    44. line-height: 90rpx;
    45. text-align: center;
    46. font-size: 36rpx;
    47. color: #fff;
    48. }
    49. }
    50. }

    效果图:

     这次分享就到这里了。希望能帮助到你。如果有需要做小程序的可以私信我哦。

  • 相关阅读:
    (王道考研计算机网络)第四章网络层-第五节2:OSPF协议与链路状态算法
    【Java 基础篇】Java Condition 接口详解
    企业级Java EE架构设计精深实践
    Guava:Java开发者的全方位工具库
    动态内存管理
    AVL 树的初步认识与基本操作
    Gopher的Rust第一课:Rust的那些事儿
    C++核心(八)构造和析构
    Android里获取正在前端运行的Activity的包名
    简单的 JSONParser
  • 原文地址:https://blog.csdn.net/Jensen_Yao/article/details/128032220