// pages/user/myInfo/index.js import { store } from '../../../utils/store.js' import { config } from '../../../config.js' import { UserModel } from '../../../models/user.js' let userModel = new UserModel() Page({ /** * 页面的初始数据 */ data: { avatarUrl:String, nikeName: String, phone: String, company: String, position: String, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { console.log(options) this.setData({ avatarUrl: options.userIcon, nikeName: options.userName, phone: options.userPhone || '', company: options.company || '', position: options.position || '', }) }, onTapItem(e) { const { type } = e.currentTarget.dataset console.log(type) if(type === 'userIcon'){ this.upload() } }, upload(){ let that = this wx.chooseImage({ success(res) { wx.showLoading() const tempFilePaths = res.tempFilePaths console.log(res) const token = store.readToken() wx.uploadFile({ url: config.api_url + "/api/common/upload", filePath: tempFilePaths[0], header: { 'token': token, 'content-type': 'application/json', }, name: 'files', success(res) { console.log(res); const data = JSON.parse(res.data) const image = { url: config.api_url + '/' + data.result.imgUrl, } that.setData({ avatarUrl: image.url },()=>{ wx.hideLoading() }) } }) } }) }, getUserInfo(){ userModel.getUserInfo(res => { let nickName = res.result.nickName let avatarUrl = res.result.avatarUrl this.setData({ avatarUrl: res.result.avatarUrl, nikeName: res.result.nickName, phone: res.result.phone || '', company: res.result.company || '', position: res.result.position || '', }) store.saveUserInfo({ nickName: res.result.nickName, avatarUrl: res.result.avatarUrl, phone: res.result.phone || '' }) }) }, bindNikeNameInput(e){ this.setData({ nikeName:e.detail.value }) }, bindPhoneInput(e) { this.setData({ phone: e.detail.value }) }, bindCompanyInput(e) { this.setData({ company: e.detail.value }) }, bindPositionInput(e) { this.setData({ position: e.detail.value }) }, submit(){ let avatarUrl = this.data.avatarUrl let nikeName = this.data.nikeName let phone = this.data.phone let company = this.data.company let position = this.data.position userModel.updateUserInfo(avatarUrl, nikeName, phone, company, position, res => { // that.getUserInfo() if(res.code === 200){ wx.showToast({ title: '修改成功', icon: 'none', duration: 2000, success(res) { setTimeout(function () { wx.navigateBack({ delta: 1 }) }, 2000) } }) } }) } })