const uploadJS = require('../../mixin/upload.js') const { updateUserInfo, userLoginOut } = require('../../api/common') const app = getApp() Page({ /** * 页面的初始数据 */ data: { form: { user_head_img_url: '', user_nickname: '', account: '' }, user_head_img_url: [], user_nickname: '', isAutoFocus: false, booNickname: false, booLogout: false, booLock: false }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.getTabBar().init() }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, ...uploadJS, // 图片上传成功回调 uploadCallBack(res) { const temp = res.map(item => { return { 'url': item.url, 'formkey': item.formkey } }) let formkey = '' if (temp.length > 0) { formkey = temp[0].formkey } if (formkey === 'user_head_img_url') { this._updateUserInfo(formkey, temp[0].url, this.data.form.user_head_img_url) } }, // 有昵称时:编辑昵称 editNickname() { this.setData({ user_nickname: this.data.form.user_nickname, booNickname: true }) setTimeout(() => { this.setData({ isAutoFocus: true }) }, 500) }, bindInput(e) { this.setData({ user_nickname: e.detail.value.trim() }) }, // 失去焦点:编辑昵称 bindblur() { const user_nickname = this.data.user_nickname if (user_nickname.length < 1) { this.setData({ booNickname: false }) return } this._updateUserInfo('user_nickname', user_nickname, this.data.form.user_nickname) }, async _updateUserInfo(key, newVal, oldVal) { let val = newVal try { const { status, msg } = await updateUserInfo({ [key]: newVal }) if (status) { await app.fetchUserData() } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (e) { val = oldVal } const temp = { [`form.${key}`]: val } if (key === 'user_head_img_url') { temp.user_head_img_url = [ { 'url': val, 'formkey': key } ] } if (key === 'user_nickname') { temp.booNickname = false } this.setData(temp) }, jump(e) { const { page } = e.currentTarget.dataset wx.navigateTo({ url: `/pages/${page}/${page}` }) }, showLogout() { this.setData({ booLogout: true }) }, hideLogout() { this.setData({ booLogout: false }) }, async confirmLogout() { this.setData({ booLock: true }) try { const { status, msg } = await userLoginOut() if (status) { this.hideLogout() wx.removeStorageSync(sessionStorageKey) // 更新用户信息 app.globalData.userInfo = {} wx.reLaunch({ url: '/pages/mine/mine' }) } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (e) {} this.setData({ booLock: false }) } })