// 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: {}, 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 }) } }, updatePhoneNumber(e) { var that = this if (e.detail.errMsg == 'getPhoneNumber:ok') { wx.checkSession({ success: () => { var data = Object.assign({}, e.detail, { session_key: wx.getStorageSync('session_key') }) that.parsePhone(data) }, fail: () => { wx.login({ success: function(res) { var data = Object.assign({}, e.detail, { code: res.code }) that.parsePhone(data) } }) } }) } }, parsePhone(data) { var that = this http({ url: 'parsePhone', data: data, success: function (res) { if(res.code == 0) { getApp().updateUserInfo(res.data) that.setData({ userInfo: res.data }) } else { util.error('发生错误') } } }) }, 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) setTimeout(function() { wx.navigateBack({ delta: 0, }) }, 1000) } else { util.error('操作失败') } } }) }, save: function() { // if(!util.checkMobile(this.data.phone)) { // util.error('手机号错误') // return false // } if(!this.data.name) { util.error('姓名必填') return false } var that = this; if(this.data.userInfo.avatar != this.data.avatar) { 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 () { } })