const app = getApp();
Page({
  data: {
    ossPath:app.globalData.$filePath,
  },
  onLoad:function(){
    var userInfo = wx.getStorageSync('userInfo')
    this.setData({
      nickName:userInfo.user.nickName,
      sex:userInfo.user.sex,
      avatar:userInfo.user.avatar,
    })
  },
  editInfo(){
    var that = this
    var nickName=that.data.nickName
    if (!nickName) {
      app.showToptip(that, "worning", "用户名不能为空")
      return;
    }
    var sex=that.data.sex
    var avatar=that.data.avatar
    wx.showLoading({
      mask: true
    })
    app.requestP({
      url : "/user/updateUserInfo", 
      contentType:"application/json",
      data: JSON.stringify({
        nickName,
        sex,
        avatar,
      })
    }).then((res) => {
      app.showToptip(that,"success", res.data.msg)
    }).catch((res) => {
      app.showToptip(that,"error", res.data.msg)
    });
  },
  setSex(e){
    this.setData({
      sex:e.detail.value?"1":"0"
    })
  },
  setSexs(e){
    this.setData({
      sex:e.detail.value
    })
  },
  setNickName: function (e) {
    if (e.detail.cursor == this.data.nickNamecursor) return 
    var nickName = e.detail.value.replace(/[^\u4e00-\u9fa5\da-zA-Z ]/g, "");
    if (nickName.length >= 25) {
      nickName = nickName.substring(0, 25)
    }
    this.setData({
      nickNamecursor: e.detail.cursor,
      nickName: nickName
    })
  } ,
  delImg(){
    this.setData({
      image:""
    })
  },
  uploadImg(){
    var that = this;
    wx.chooseImage({
      count: 1, // 单选
      success: function (res) {
        var filePath = res.tempFilePaths && res.tempFilePaths[0]
        app.wxUploadFile({
          url: "/common/upload/v1",
          filePath: filePath,
          name:"file",
        }).then(function(res){
          var data = JSON.parse(res.data)
          if(data.code == 200){
            that.setData({
              avatar:data.data.path
            })
          }else{
            app.showToptip(that, "error", "上传失败请联系管理员")
          }
        }).catch(function(){
          app.showToptip(that, "error", "上传失败请联系管理员")
        })
      }
    })
  },
})