const { BRAND_CONFIG } = require('../../utils/config') const auth = require('../../utils/auth') const planningApi = require('../../api/planning') const { buildProfileCard } = require('../../utils/profile-card') const profileCache = require('../../utils/profile-cache') const authApi = require('../../api/auth') const { showErrorToast } = require('../../utils/toast') const { showActionModal } = require('../../utils/action-modal') function resolveRoleText(type) { const role = Number(type) if (role === 2) return '店长' if (role === 3) return '美甲师' if (role === 4) return '管理员' return '管理端' } function resolveIdentityRole(type) { const role = Number(type) if (role === 2) return 'manager' if (role === 3) return 'artist' return '' } const MANAGER_MENUS = [ { title: '个人信息', url: '/pages/profile-edit/index' }, { title: '店铺信息', url: '/pages/shop-settings/index' }, { title: '账号安全', url: '/pages/account-settings/index' }, { title: '订单数据', url: '/pages/export/index' }, { title: '关于系统', url: '/pages/about/index' } ] const ARTIST_MENUS = MANAGER_MENUS.filter(item => item.title !== '店铺信息') Page({ data: { roleText: '管理端', identityRole: '', activeRole: '', profileCard: buildProfileCard({}, BRAND_CONFIG.defaultShopName), overviewLoading: false, overview: { date: '', todayAppointments: 0, pendingAppointments: 0, upcomingAppointments: 0, availableEmployees: 0, scheduleExceptions: 0, shopOpen: true, shopStatus: '营业中' }, menus: MANAGER_MENUS }, onShow() { this.refreshFromSession() this.loadProfile() this.loadOverview() }, async loadOverview() { this.setData({ overviewLoading: true }) try { const overview = await planningApi.todayOverview() this.setData({ overview: { date: overview.date || '', todayAppointments: Number(overview.todayAppointments || 0), pendingAppointments: Number(overview.pendingAppointments || 0), upcomingAppointments: Number(overview.upcomingAppointments || 0), availableEmployees: Number(overview.availableEmployees || 0), scheduleExceptions: Number(overview.scheduleExceptions || 0), shopOpen: overview.shopOpen !== false, shopStatus: overview.shopStatus || (overview.shopOpen === false ? '已暂停' : '营业中') } }) } catch (error) { showErrorToast(error, '今日概览加载失败') } 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 const identityRole = resolveIdentityRole(userInfo.type) const activeRole = this.data.activeRole || identityRole this.setData({ roleText: resolveRoleText(userInfo.type), identityRole, activeRole, menus: activeRole === 'artist' ? ARTIST_MENUS : MANAGER_MENUS, profileCard: buildProfileCard(userInfo, fallbackShopName) }) }, switchRole(event) { const role = event.currentTarget.dataset.role if (!role || role === this.data.activeRole) return this.setData({ activeRole: role, menus: role === 'artist' ? ARTIST_MENUS : MANAGER_MENUS }) }, go(event) { wx.navigateTo({ url: event.currentTarget.dataset.url }) }, async logout() { const result = await showActionModal(this, { title: '退出登录', content: '退出后将关闭自动登录,并清除本机提醒设置。', cancelText: '取消', confirmText: '确定', confirmType: 'danger' }) if (!result.confirm) return try { await authApi.logout() } catch (error) {} getApp().logoutDevice() wx.reLaunch({ url: '/pages/login/index' }) }, onProfileAvatarError() { this.setData({ 'profileCard.avatar': '' }) }, async loadProfile() { try { const profile = await profileCache.getProfile() this.syncProfileToSession(profile) } catch (error) { showErrorToast(error, '个人信息加载失败') } }, 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 }) const identityRole = resolveIdentityRole(nextUserInfo.type) const activeRole = this.data.activeRole || identityRole this.setData({ roleText: resolveRoleText(nextUserInfo.type), identityRole, activeRole, menus: activeRole === 'artist' ? ARTIST_MENUS : MANAGER_MENUS, profileCard: buildProfileCard(nextUserInfo, BRAND_CONFIG.defaultShopName) }) } })