// pages/user/setup/setup.js Page({ /** * 页面的初始数据 */ data: { userData : '', // 基础信息 avatar : '', nickName : '', nameState : false, disabled : false, birthday : '', sexArray : [ { id: 0, name: '男' }, { id: 1, name: '女' }, ], sexIndex : '', reviseType: '', // 修改类型 nameValue : '' // 限制5个字符 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 生命周期函数--监听页面显示 */ onShow() { // 获取登录状态 if(wx.getStorageSync("token") != ''){ // 获取用户信息 this.userInfo(); } }, /** * 用户设置信息 */ userInfo() { wx.$api.user.userSetup().then(res => { this.setData({ userData : res.data, avatar : res.data.avatar, nickName : res.data.nickname, birthday : res.data.birthday, sexIndex : res.data.sex }) }).catch(err => {}) }, /** * 头像上传 */ updImg(e){ let type = e.currentTarget.dataset.type this.setData({ reviseType: e.currentTarget.dataset.type }) if(type == 'avatar') { wx.chooseMedia({ count : 1, success : path => { // 上传图片 wx.$api.file.uploadImg(path.tempFiles[0].tempFilePath, {}).then(res=>{ this.setData({ avatar:res.url }) this.settingInfo(type, res.path) }) } }) return } // 修改用户名 this.setData({ nameState: true }) }, /* 姓名截取 */ bindinput(e) { this.setData({ nameValue: e.detail.value.substr(0,5) }) }, // 修改用户名 freeform() { this.settingInfo(this.data.reviseType, this.data.nameValue) }, /* 出生年月日 */ bindDateChange(e) { this.setData({ birthday: e.detail.value }) this.settingInfo('birthday', e.detail.value) }, /* 性别选择 */ radioChange (e) { const sex = this.data.sexArray for (let i = 0, len = sex.length; i < len; ++i) { sex[i].checked = sex[i].id == e.detail.value } this.setData({ sexArray: sex, sexId : e.detail.value }) this.settingInfo('sex', e.detail.value) }, /** * 上传用户信息 */ settingInfo(key, value) { wx.$api.user.setting(key, { value: value }).then(() => { this.setData({ nameState: false, nameValue: '' }) // 获取用户信息 this.userInfo(); }).catch(err => {}) }, // 关闭弹框 establish() { this.setData({ nameState: false, nameValue: '' }) } })