Files
JayuCloud/JY-Salon-WeChat/pages/profile/index.js
T

240 lines
7.1 KiB
JavaScript

const { BRAND_CONFIG } = require('../../utils/config')
const userApi = require('../../api/user')
const shopApi = require('../../api/shop')
const dashboardApi = require('../../api/dashboard')
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()
}
function formatCount(value) {
const count = Number(value)
if (!Number.isFinite(count)) return '--'
return String(Math.max(0, Math.trunc(count)))
}
function formatMoney(value) {
const amount = Number(value)
if (!Number.isFinite(amount)) return '--'
return `¥${amount.toFixed(2).replace(/\.00$/, '')}`
}
function resolveTodayAppointments(stats) {
return stats.todayAppointments || stats.todayAppointmentCount || stats.todayServiceCount || stats.todayServices || stats.todayOrders
}
function buildDashboardStats(stats) {
const data = stats || {}
return [
{ mark: '今', label: '今日预约', value: formatCount(resolveTodayAppointments(data)), unit: '单' },
{ mark: '单', label: '今日订单', value: formatCount(data.todayOrders), unit: '单' },
{ mark: '¥', label: '今日收入', value: formatMoney(data.todayOrderAmount), unit: '' }
]
}
function resolveBusinessStatus(settings) {
if (Number(settings.status) === 0) {
return { text: '已停用', className: 'closed' }
}
if (settings.authorizationExpired) {
return { text: '授权到期', className: 'warning' }
}
return { text: '营业中', className: 'open' }
}
function resolveBusinessHours(settings) {
const businessHours = String(settings.businessHours || '').trim()
if (businessHours) return `今日营业 ${businessHours}`
const startTime = String(settings.startTime || '').slice(0, 5)
const endTime = String(settings.endTime || '').slice(0, 5)
if (startTime && endTime) return `今日营业 ${startTime} - ${endTime}`
return '营业时间未设置'
}
Page({
data: {
shopName: BRAND_CONFIG.defaultShopName,
shopCoverImage: '',
phone: '店长端',
roleText: '管理端',
businessStatusText: '营业中',
businessStatusClass: 'open',
businessHoursText: '营业时间未设置',
dashboardStats: buildDashboardStats({}),
userInfo: {},
loggingOut: false
},
onShow() {
this.refreshFromSession()
this.loadShopHeader()
this.loadProfile()
this.loadDashboardStats()
},
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)
const status = resolveBusinessStatus(settings || {})
app.globalData.shopName = shopName
app.globalData.shopCoverImage = shopCoverImage
this.setData({
shopName,
shopCoverImage,
businessStatusText: status.text,
businessStatusClass: status.className,
businessHoursText: resolveBusinessHours(settings || {})
})
} catch (error) {
wx.showToast({ title: error.message || '店铺信息加载失败', icon: 'none' })
}
},
async loadDashboardStats() {
try {
const stats = await dashboardApi.stats()
this.setData({ dashboardStats: buildDashboardStats(stats || {}) })
} catch (error) {
this.setData({ dashboardStats: buildDashboardStats({}) })
wx.showToast({ title: error.message || '经营数据加载失败', icon: 'none' })
}
},
go(event) {
const action = event.currentTarget.dataset.action
if (action === 'logout') {
this.confirmLogout()
return
}
const url = event.currentTarget.dataset.url
if (!url) {
wx.showToast({ title: '功能即将开放', icon: 'none' })
return
}
wx.navigateTo({ 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)
})
}
})