modifyPersonInformation.js 6.4 KB
// pages/my/modifyPersonInformation/modifyPersonInformation.js
const app = getApp();
const qiniuUploader = require("../../../utils/qiniuUploader");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag: false,
    birth: false,
    choose: 10,
    photoPath: "",
    user: {},
    mobile: '',
    phone: '',
    birthday: '',
    nick_name: '',
    nick_names: '',
    sex: '',
    nameFlag: false,
    key:''
  },
   //选择上传头像
  changePhoto: function () {
    var that = this;
    // 选择图片
    wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
      success: function (res) {
        const filePath = res.tempFilePaths[0];
        that.setData({
          photoPath: filePath
        });
        qiniuUploader.upload(filePath, (res) => {
                console.log(res)
                let a ='http://qiniu.himrhi.com/'+res.key
          that.setData({
                key: a
          });
        }, (error) => {
          console.log('error: ' + error);
        }, {
            region: 'ECN',
            domain: 'qiniu.himrhi.com',
            uptokenURL: 'http://www.himrhi.com/api/wxapp/public/getQiniuToken', 
            uptokenFunc: function () { return 'JMgHPK8vEdZfUfxZIW8qIsyucRCZeTZ4aqHLWsjI:9VjchzcKJTa9uqK_0MWELzc4_zg=:eyJzY29wZSI6ImhpLWZyZXNoIiwiZGVhZGxpbmUiOjE1NjA3MzU3ODF9'; }
          }, (res) => {
            console.log('上传进度', res.progress)
            console.log('已经上传的数据长度', res.totalBytesSent)
            console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
          }, () => {
            // 取消上传
          }, () => {
            // `before` 上传前执行的操作
          }, (err) => {
            // `complete` 上传接受后执行的操作(无论成功还是失败都执行)
          });
      }
    })
  },
  //修改性别
  //修改男
  choose1() {
    this.setData({
      choose: 1
    });
  },
  //修改女
  choose2() {
    this.setData({
      choose: 2
    });
  },
  //渲染个人信息
  fetchPersonal() {
    let that = this
    let url = '/wxapp/user/getUserInfo'
    app.post(url).then(r => {
      if (r.code == 1) {
        if (r.data.sex == 1) {
          r.data.sex = '女'
        } else {
          r.data.sex = '男'
        }
        let leixing=typeof(r.data.birthday)
        if (leixing == 'number'){
                r.data.birthday = app.timeFormate(r.data.birthday, 'YYMMDD')
        }
        // r.data.birthday = app.timeFormate(r.data.birthday, 'YYMMDD')
        that.setData({
          user: r.data,
          phone: r.data.mobile,
          birthday: r.data.birthday,
          nick_name: r.data.user_nickname,
          sex: r.data.sex,
          photoPath:r.data.avatar
        })
      }
    })
  },
  //选择生日
  bindDateChange(e) {
    let birthday = e.detail.value;
    this.setData({
      birthday: birthday
    })
  },
  //确认修改
  confirmUpdate() {
    let that = this
    let url = '/wxapp/user/setUserInfo'
    let sex = ''
    if (that.data.choose == 2) {
      sex =1
    } else if (that.data.choose == 1) {
      sex =2
    } else if (that.data.choose == 10) {
      sex = that.data.sex
      if(that.data.sex=='女'){
              sex=1
      }else{
              sex=2
      }
    }
    if (sex == '' && !sex) {
      wx.showToast({
        title: '请确定性别',
        icon: 'none'
      })
      return;
    }
    if (that.data.birthday == '' && !that.data.birthday) {
      wx.showToast({
        title: '请确定生日',
        icon: 'none'
      })
      return;
    }
  
    if (that.data.nick_names == ''&&that.data.nick_name=='') {
      wx.showToast({
        title: '请确定昵称',
        icon: 'none'
      })
      return;
    }
    let nick_names=''
    if(that.data.nick_names!=''){
            nick_names=that.data.nick_names
    }else{
            nick_names=that.data.nick_name
    }
        let avatar = that. data.key
          if (that.data.photoPath == '' && avatar == '') {
                  wx.showToast({
                          title: '请确定头像',
                          icon: 'none'
                  })
                  return;
          }
          if(that.data.avatar==''){
                  avatar=that.data.photoPath
          }else{
                  avatar=that.data.key
          }
        console.log(avatar+"新的头像信息")
    console.log(that.data.birthday+"生日")
    let params = {
      avatar: avatar,
      user_nickname: nick_names,
      sex: sex,
      birthday: that.data.birthday
    }
    app.post(url, params).then(r => {
      if (r.code == 1) {
        wx.showToast({
          title: '修改成功',
          icon: 'none'
        })
        this.fetchPersonal();
        wx.switchTab({
          url: '../../my/my',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.fetchPersonal();
    if (options.mobile) {
      that.setData({
        mobile: options.mobile
      })
    }
    if (options.value) {
      that.setData({
        nick_names: options.value,
        nameFlag: true
      })
    }
  },
  // 返回个人中心
  get_my() {
    wx.switchTab({
            url: '../my',
    })
  },
  // 修改昵称
  getamend_nickname() {
    wx.navigateTo({
      url: '../modifyName/modifyName',
    })
  },
  // 修改手机号
  getamend_nickphone() {
    wx.navigateTo({
      url: '../modifyPhone/modifyPhone',
    })
  },
  //修改性别
  changeSex() {
    this.setData({
      flag: !this.data.flag
    });
  },
  //修改生日
  changeBirth() {
    this.setData({
      birth: !this.data.birth
    });
  },
  //点击修改性别确定
  hide() {
    this.setData({
      flag: !this.data.flag
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})