123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- 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
- })
- }
- })
|