// pages/userInfo/index.js const app = getApp(); Page({ /** * 页面的初始数据 */ data: { setList: [], isShowSheet: false, members: {}, gohome: true }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() { }, /** * 生命周期函数--监听页面显示 */ onShow: function() { this.getData(); }, /** * 生命周期函数--监听页面隐藏 */ onHide: function() { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function() { }, /** * 用户点击右上角分享 */ onShareAppMessage: function() { return { title: '智慧云馆' } }, // 切换头像 onChooseAvatar(e) { let that = this; const { avatarUrl } = e.detail; if (!avatarUrl) return; wx.showLoading({ title: '正在上传...', mask: true, }) app.$api.uploadAvatar({ filePath: avatarUrl }).then(res => { wx.showToast({ title: '上传成功', icon: 'success', }) that.getData(); }, err => { wx.hideLoading(); }); }, // 没有修改次数时点击头像 onAvatarNoCount() { wx.showToast({ title: '本年度修改头像次数已用完', icon: 'none', duration: 2000 }); }, // 修改昵称 onNicknameChange(e) { let newName = e.detail.value; // 防止重复触发 if (!newName || newName === this.data.members.nickname) return; // 乐观反显更新 this.setData({ 'members.nickname': newName }); wx.showLoading({ title: '保存中...', mask: true }); app.$api.memberUpdateInformation({ nickname: newName }).then(res => { wx.hideLoading(); wx.showToast({ title: '修改成功', icon: 'success', }); // 这里的静默获取能确保和数据库端同步 this.getData(); }, err => { wx.hideLoading(); }); }, // 修改性别 changeSex(val) { this.setData({ setList: [{ label: '男', value: 0, type: 'sex' }, { label: '女', value: 1, type: 'sex' }, { label: '不选择', value: 2, type: 'sex' }], isShowSheet: true }) }, // 隐藏sheet弹框 cancelSheet() { this.setData({ isShowSheet: false }) }, // 选择sheet selectedSheet(e) { let obj = e.detail; if (obj.type == 'avatar') { if (obj.value == 0) { this.photograph('camera'); } if (obj.value == 1) { this.photograph('album'); } } if (obj.type == 'sex') { this.updateSex(obj.value); } this.setData({ isShowSheet: false }) }, // 修改性别 上传服务器 updateSex(val) { app.$api.memberUpdateInformation({ sex: val }).then(res => { this.getData(); }, err => { }) }, // 在当前页直接修改文本类别的信息(姓名、年龄、地址) onInfoChange(e) { let type = e.currentTarget.dataset.type; let newValue = e.detail.value; if (newValue === undefined || newValue === this.data.members[type]) return; let updateData = {}; updateData[type] = newValue; // 乐观反显 let key = 'members.' + type; this.setData({ [key]: newValue }); wx.showLoading({ title: '保存中...', mask: true }); app.$api.memberUpdateInformation(updateData).then(res => { wx.hideLoading(); wx.showToast({ title: '修改成功', icon: 'success', }); // 静默获取同步服务器 this.getData(); }, err => { wx.hideLoading(); }); }, // 获取数据 getData() { wx.showLoading({ title: '加载中...', mask: true, }) app.$api.memberInformation({}).then(res => { this.setData({ members: res.members }) }, err => { }) } })