// pages/user/index.js import http from '../../utils/http' import api from '../../utils/api' import util from '../../utils/util' const app = getApp() Page({ /** * 页面的初始数据 */ data: { userInfo: null, avatar: '', name: '', phone: '' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if(app.globalData.userInfo) { var user = app.globalData.userInfo this.setData({ userInfo: user, avatar: user.avatar, name: user.name, phone: user.phone }) } }, chooseImage() { var that = this wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success (res) { const paths = res.tempFilePaths if(paths.length > 0) { that.setData({ avatar: paths[0] }) } } }) }, updateInput: function(e) { app.updateInput(this, e) }, updateInfo: function() { http({ url: 'users/update', data: { avatar: this.data.avatar, name: this.data.name, phone: this.data.phone }, success: function (res) { if (res.code == 0) { util.success('操作成功') app.updateUserInfo(res.data) } else { util.error('操作失败') } } }) }, save: function() { if(!util.checkMobile(this.data.phone)) { util.error('手机号错误') return false } if(!this.data.name) { util.error('姓名必填') return false } if(this.data.userInfo.avatar != this.data.avatar) { var that = this; util.uploadFile(this.data.avatar, function(res) { that.setData({ avatar: res.path }) that.updateInfo() }) } else { that.updateInfo() } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })