179 lines
5.1 KiB
JavaScript
179 lines
5.1 KiB
JavaScript
const { BRAND_CONFIG } = require('../../utils/config')
|
|
const userApi = require('../../api/user')
|
|
const shopApi = require('../../api/shop')
|
|
const authApi = require('../../api/auth')
|
|
const auth = require('../../utils/auth')
|
|
|
|
const DEFAULT_BACKEND_AVATAR_URL = 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
|
|
|
|
function resolveRoleText(type) {
|
|
const role = Number(type)
|
|
if (role === 2) return '店长'
|
|
if (role === 3) return '美甲师'
|
|
if (role === 4) return '管理员'
|
|
return '管理端'
|
|
}
|
|
|
|
function normalizeAvatarUrl(avatar) {
|
|
const value = String(avatar || '').trim()
|
|
if (!value) return ''
|
|
return value.split('?')[0]
|
|
}
|
|
|
|
function resolveDisplayAvatar(avatar) {
|
|
const value = String(avatar || '').trim()
|
|
if (!value) return ''
|
|
if (normalizeAvatarUrl(value) === DEFAULT_BACKEND_AVATAR_URL) return ''
|
|
return value
|
|
}
|
|
|
|
function resolveShopCoverImage(settings) {
|
|
if (!settings || !Array.isArray(settings.coverImageUrls)) {
|
|
return ''
|
|
}
|
|
return String(settings.coverImageUrls[0] || '').trim()
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
shopName: BRAND_CONFIG.defaultShopName,
|
|
shopCoverImage: '',
|
|
phone: '店长端',
|
|
roleText: '管理端',
|
|
userInfo: {},
|
|
loggingOut: false,
|
|
menus: [
|
|
{ title: '个人信息', url: '/pages/profile-settings/index' },
|
|
{ title: '店铺设置', url: '/pages/shop-settings/index' },
|
|
{ title: '数据导出', url: '/pages/export/index' },
|
|
{ title: '修改密码', url: '/pages/password/index' },
|
|
{ title: '关于系统', url: '/pages/about/index' },
|
|
{ title: '退出登录', action: 'logout', danger: true }
|
|
]
|
|
},
|
|
|
|
onShow() {
|
|
this.refreshFromSession()
|
|
this.loadShopHeader()
|
|
this.loadProfile()
|
|
},
|
|
|
|
refreshFromSession() {
|
|
const app = getApp()
|
|
const userInfo = app.globalData.userInfo || {}
|
|
const displayAvatar = resolveDisplayAvatar(userInfo.avatar)
|
|
this.setData({
|
|
shopName: app.globalData.shopName || BRAND_CONFIG.appName,
|
|
shopCoverImage: app.globalData.shopCoverImage || '',
|
|
phone: userInfo.contactPhone || userInfo.phone || userInfo.account || '店长端',
|
|
roleText: resolveRoleText(userInfo.type),
|
|
userInfo: {
|
|
...userInfo,
|
|
displayAvatar
|
|
}
|
|
})
|
|
},
|
|
|
|
async loadShopHeader() {
|
|
try {
|
|
const settings = await shopApi.settings()
|
|
const app = getApp()
|
|
const shopName = settings.shopName || settings.name || app.globalData.shopName || BRAND_CONFIG.appName
|
|
const shopCoverImage = resolveShopCoverImage(settings)
|
|
app.globalData.shopName = shopName
|
|
app.globalData.shopCoverImage = shopCoverImage
|
|
this.setData({
|
|
shopName,
|
|
shopCoverImage
|
|
})
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '店铺信息加载失败', icon: 'none' })
|
|
}
|
|
},
|
|
|
|
go(event) {
|
|
const action = event.currentTarget.dataset.action
|
|
if (action === 'logout') {
|
|
this.confirmLogout()
|
|
return
|
|
}
|
|
wx.navigateTo({ url: event.currentTarget.dataset.url })
|
|
},
|
|
|
|
confirmLogout() {
|
|
if (this.data.loggingOut) {
|
|
return
|
|
}
|
|
wx.showModal({
|
|
title: '退出登录',
|
|
content: '确认退出当前账号吗?',
|
|
confirmText: '退出',
|
|
confirmColor: '#d98d8d',
|
|
success: res => {
|
|
if (res.confirm) {
|
|
this.logout()
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
async logout() {
|
|
if (this.data.loggingOut) {
|
|
return
|
|
}
|
|
this.setData({ loggingOut: true })
|
|
try {
|
|
await authApi.logout()
|
|
const app = getApp()
|
|
if (app && typeof app.clearSession === 'function') {
|
|
app.clearSession()
|
|
} else {
|
|
auth.clearSession()
|
|
}
|
|
wx.reLaunch({ url: '/pages/login/index' })
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '退出登录失败', icon: 'none' })
|
|
} finally {
|
|
this.setData({ loggingOut: false })
|
|
}
|
|
},
|
|
|
|
async loadProfile() {
|
|
try {
|
|
const profile = await userApi.info()
|
|
this.syncProfileToSession(profile)
|
|
} catch (error) {
|
|
wx.showToast({ title: error.message || '个人信息加载失败', icon: 'none' })
|
|
}
|
|
},
|
|
|
|
syncProfileToSession(profile) {
|
|
const app = getApp()
|
|
const session = auth.getSession()
|
|
const currentUserInfo = session.userInfo || app.globalData.userInfo || {}
|
|
const nextUserInfo = {
|
|
...currentUserInfo,
|
|
id: profile.id || currentUserInfo.id,
|
|
account: profile.account || currentUserInfo.account,
|
|
nickname: profile.nickname || '',
|
|
avatar: profile.avatar || '',
|
|
phone: profile.phone || currentUserInfo.phone || '',
|
|
contactPhone: profile.contactPhone || '',
|
|
wechatId: profile.wechatId || '',
|
|
signature: profile.signature || '',
|
|
type: profile.type || currentUserInfo.type,
|
|
shopId: profile.shopId || currentUserInfo.shopId,
|
|
displayAvatar: resolveDisplayAvatar(profile.avatar || '')
|
|
}
|
|
app.setSession({
|
|
...session,
|
|
userInfo: nextUserInfo
|
|
})
|
|
this.setData({
|
|
userInfo: nextUserInfo,
|
|
phone: nextUserInfo.contactPhone || nextUserInfo.phone || nextUserInfo.account || '店长端',
|
|
roleText: resolveRoleText(nextUserInfo.type)
|
|
})
|
|
}
|
|
})
|