const { BRAND_CONFIG } = require('../../utils/config') const userApi = require('../../api/user') const auth = require('../../utils/auth') const planningApi = require('../../api/planning') const employeesApi = require('../../api/employees') const shopApi = require('../../api/shop') const dateUtil = require('../../utils/date') const { buildProfileCard } = require('../../utils/profile-card') function resolveRoleText(type) { const role = Number(type) if (role === 2) return '店长' if (role === 3) return '美甲师' if (role === 4) return '管理员' return '管理端' } Page({ data: { roleText: '管理端', profileCard: buildProfileCard({}, BRAND_CONFIG.defaultShopName), overviewLoading: false, overview: { date: '', todayAppointments: 0, pendingAppointments: 0, upcomingAppointments: 0, availableEmployees: 0, scheduleExceptions: 0, shopOpen: true, shopStatus: '营业中' }, menus: [ { title: '个人信息', url: '/pages/profile-edit/index' }, { title: '账号设置', url: '/pages/account-settings/index' }, { title: '店铺设置', url: '/pages/shop-settings/index' }, { title: '数据导出', url: '/pages/export/index' }, { title: '关于系统', url: '/pages/about/index' } ] }, onShow() { this.refreshFromSession() this.loadProfile() this.loadOverview() }, async loadOverview() { const today = dateUtil.formatDate(new Date()) this.setData({ overviewLoading: true }) try { const [calendar, shop] = await Promise.all([ planningApi.calendar({ startDate: today, endDate: today }), shopApi.settings() ]) const appointments = Array.isArray(calendar.appointments) ? calendar.appointments : [] const now = Date.now() const upcomingAppointments = appointments.filter(item => { const appointmentTime = new Date(String(item.appointmentTime || item.startTime || '').replace(/-/g, '/')).getTime() return Number.isFinite(appointmentTime) && appointmentTime >= now }).length const artists = Array.isArray(calendar.artists) ? calendar.artists : [] const scheduleRules = await Promise.all(artists.map(artist => employeesApi.scheduleRule({ artistId: artist.id }))) const scheduleExceptions = scheduleRules.reduce((total, setting) => { const exceptions = Array.isArray(setting && setting.exceptions) ? setting.exceptions : [] return total + exceptions.filter(item => item.workDate === today).length }, 0) const shopOpen = Number(shop && shop.status) !== 0 this.setData({ overview: { date: `${today.slice(5, 7)}月${today.slice(8, 10)}日`, todayAppointments: appointments.length, pendingAppointments: appointments.filter(item => Number(item.status) === 0).length, upcomingAppointments, availableEmployees: artists.filter(item => Number(item.status) !== 0).length, scheduleExceptions, shopOpen, shopStatus: shopOpen ? '营业中' : '已暂停' } }) } catch (error) { wx.showToast({ title: error.message || '今日概览加载失败', icon: 'none' }) } finally { this.setData({ overviewLoading: false }) } }, goOverviewAction(event) { const action = event.currentTarget.dataset.action if (action === 'scheduleExceptions') { wx.navigateTo({ url: '/pages/leave-management/index' }) return } wx.switchTab({ url: '/pages/planning/index' }) }, refreshFromSession() { const app = getApp() const userInfo = app.globalData.userInfo || {} const fallbackShopName = app.globalData.shopName || BRAND_CONFIG.defaultShopName this.setData({ roleText: resolveRoleText(userInfo.type), profileCard: buildProfileCard(userInfo, fallbackShopName) }) }, go(event) { wx.navigateTo({ url: event.currentTarget.dataset.url }) }, 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 || '', phoneVerified: profile.phoneVerified, maskedPhone: profile.maskedPhone || '', contactPhone: profile.contactPhone || '', wechatId: profile.wechatId || '', signature: profile.signature || '', type: profile.type || currentUserInfo.type, shopId: profile.shopId || currentUserInfo.shopId, shopName: profile.shopName || currentUserInfo.shopName || session.shopName || app.globalData.shopName || '' } app.setSession({ ...session, shopName: nextUserInfo.shopName || session.shopName, userInfo: nextUserInfo }) this.setData({ roleText: resolveRoleText(nextUserInfo.type), profileCard: buildProfileCard(nextUserInfo, BRAND_CONFIG.defaultShopName) }) } })