设置昵称

其实也是差不多的

<button open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">使用微信昵称 </button>

<button size="default" bindtap="handleBtn">自定义昵称</button>
<input type="text" value="{{nickName}}" bindinput="handleText" />

1、使用微信昵称

  就是获取账号的昵称来更新系统的昵称

  

bindGetUserInfo(ev){
    let userInfo = ev.detail.userInfo;
    if(userInfo){
      this.setData({
        nickName:userInfo.nickName
      },()=>{
        this.updatenickName();
      })
    }
  }

2、输入框 

    属性是文本格式的,属性绑定了data 的nickName字段,

    还绑定了bindinput属性 可以监听输入的ev信息  使用ev.detail.value可以获取输入的信息

    

 handleText(ev){

    let value = ev.detail.value;
    this.setData({
      nickName:value
    })
  }

3、自定义昵称

  绑定事件handleBtn  handleBtn用来调用updatenickName()方法   

  updatenickName() 方法获取文本中的文字更新到数据库 和系统字段中
handleBtn(){
    this.updatenickName()
  },
  updatenickName(){
    console.log(this.nickName)
    wx.showLoading({
      title: '更新中',
    })
      db.collection("users").doc(app.userInfo._id).update({
        data:{
          nickName:this.data.nickName
        }
      }).then(res=>{
      wx.hideLoading({
        success: (res) => {},
      });
      wx.showToast({ 
        title: '更新成功',
      })
      app.userInfo.nickName = this.data.nickName;

      })
  },
原文地址:https://www.cnblogs.com/zhuangdd/p/14065564.html