const uploadJS = require('../../mixin/upload.js') const { updateUserInfo, userLoginOut } = require('../../api/common') const { sessionStorageKey } = require('../../api/request') const app = getApp() Page({ /** * 页面的初始数据 */ data: { form: { user_head_img_url: '', user_nickname: '', user_phone: '' }, user_head_img_url: [], user_nickname: '', isAutoFocus: false, booNickname: false, booLogout: false, booLock: false, sessionId: '', userInfo: { // 'shop_status': 0, // 状态(0审核中1审核通过2审核失败) // 'user_shop_id': 0 //店铺ID(0代表无店铺) }, kefu: '18551725991' // todo 客服电话 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.init() }, async bindCallBack() { this.init() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.getTabBar().init() this.setData({ sessionId: wx.getStorageSync(sessionStorageKey), userInfo: app.globalData.userInfo }) }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, async init() { // 更新用户信息 app.fetchUserDataCallback = () => { const userInfo = app.globalData.userInfo let temp = { userInfo } for (let key in userInfo) { let value = userInfo[key] if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') { if (this.data.hasOwnProperty(key)) { if (key === 'user_head_img_url') { value = [ { url: value, formkey: key } ] } temp[`${key}`] = value } if (this.data.form.hasOwnProperty(key)) { temp[`form.${key}`] = value } } } this.setData(temp) } await app.fetchUserData() }, ...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) } }, // 有昵称时:编辑昵称 editNickname() { this.setData({ 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) }, async _updateUserInfo(key, val) { try { const { status, msg } = await updateUserInfo({ [key]: val }) if (status) { this.init() } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (e) {} if (key === 'user_nickname') { this.setData({ booNickname: false }) } }, jump(e) { const { page } = e.currentTarget.dataset wx.navigateTo({ url: `/pages/${page}/${page}` }) }, call() { wx.makePhoneCall({ phoneNumber: this.data.kefu }) }, 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 }) } })