// pages/home/index.js const role = require('../../utils/role.js') const API = require('../../utils/api.js') // 取消与 status 解耦:展示状态优先取 cancelStatus function resolveDisplayStatus(order) { const cancelStatus = order?.cancelStatus || 'none' if (cancelStatus === 'cancelled') return 'cancelled' if (cancelStatus === 'requested') return 'cancel_requested' return order?.status || 'pending' } // 快捷入口:type=quick 为快捷操作(琥珀浅色系),type=function 为功能入口(暖铜主色系),分行显示。 // 管理员与陪诊员共用同一套入口配置,但陪诊员不展示档案/客户等管理员能力 const ADMIN_ENTRIES = [ { type: 'quick', icon: 'add-circle', name: '增加预约', url: '/pages/order/orderEdit', color: '#E8A96B' }, { type: 'quick', icon: 'file-add', name: '增加档案', url: '/pages/healthprofile/profileInfo', color: '#E8A96B' }, { type: 'quick', icon: 'lightbulb-circle-filled', name: 'AI办公', url: '/pages/ai/index', isTab: true, color: '#E8A96B' }, { type: 'function', icon: 'bill', name: '预约订单', url: '/pages/order/index', isTab: true, color: '#C26A2E' }, { type: 'function', icon: 'heart', name: '健康档案', url: '/pages/healthprofile/index', color: '#C26A2E' }, { type: 'function', icon: 'user', name: '注册用户', url: '/pages/customer/index', color: '#C26A2E' }, { type: 'function', icon: 'user-circle', name: '个人信息', url: '/pages/me/index', color: '#C26A2E' } ] const ATTENDANT_ENTRIES = [ { type: 'quick', icon: 'lightbulb-circle-filled', name: 'AI办公', url: '/pages/ai/index', isTab: true, color: '#E8A96B' }, { type: 'function', icon: 'bill', name: '预约订单', url: '/pages/order/index', isTab: true, color: '#C26A2E' }, { type: 'function', icon: 'user-circle', name: '个人信息', url: '/pages/me/index', color: '#C26A2E' } ] Page({ data: { today: '', greeting: '', todayText: '', recentOrders: [], role: '', isAdmin: false, isIntern: false, levelText: '', quickEntries: [], online: false, showOnlineSwitch: false, onlineSaving: false, profile: { name: '', avatar: '' }, todayIncome: 0, todayOrderCount: 0, statusMap: { pending: '待确认', confirmed: '已确认', cancel_requested: '待取消', in_progress: '进行中', completed: '已完成', cancelled: '已取消' } }, onShareAppMessage() { return { title: '暖橙陪诊后台', // 转发标题 path: '/pages/home/index', } }, onShareTimeline: function () { return { title: '暖橙陪诊后台', } }, onLoad(options) { const now = new Date(); const today = now.toISOString().substring(0, 10); const hour = now.getHours(); const greeting = hour < 6 ? '夜深了' : hour < 9 ? '早上好' : hour < 12 ? '上午好' : hour < 14 ? '中午好' : hour < 18 ? '下午好' : '晚上好'; const week = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][now.getDay()]; this.setData({ today, greeting, todayText: `${now.getMonth() + 1}月${now.getDate()}日 ${week}` }); }, onShow() { this.init() }, /** * 先解析角色再取数:管理员看全量,陪诊员只看本人订单 */ init() { return role.ensureRoleSafe() .then((detail) => { const r = detail.role const attendant = detail.attendant || null // 接单开关只对正式陪诊师展示:管理员无接单身份,实习陪诊师不可接单 const isIntern = !!(attendant && attendant.stage === 'intern') const showOnlineSwitch = r === 'attendant' && !isIntern this.setData({ role: r, isAdmin: r === 'admin', // 实习陪诊师可进工作台查看,但不能接单:首页给一条只读提示 isIntern, levelText: r === 'admin' ? '管理员' : (attendant ? attendant.text : ''), quickEntries: r === 'admin' ? ADMIN_ENTRIES : (r === 'attendant' ? ATTENDANT_ENTRIES : []), showOnlineSwitch, online: showOnlineSwitch && attendant ? attendant.isOnline === true : false }) this.loadUser() this.getRecentOrders() this.getStats() }) .catch(() => {}) }, loadUser() { const app = getApp() app.ensureLogin() .then(user => { if (!user) return const p = user.profile || {} this.setData({ 'profile.name': p.name || '微信用户', 'profile.avatar': p.avatar || '' }) }) .catch(() => {}) }, async toggleOnline() { if (!this.data.showOnlineSwitch || this.data.onlineSaving) return const next = !this.data.online // 先乐观更新,请求失败再回滚 this.setData({ online: next, onlineSaving: true }) try { const res = await API.employee.setOnline(next) if (!res || res.code !== 0) { throw new Error((res && res.msg) || '切换失败,请重试') } const info = res.data && res.data.employee ? res.data.employee.attendantInfo : null const actual = info && typeof info.isOnline === 'boolean' ? info.isOnline : next this.setData({ online: actual }) role.setAttendantOnline(actual) wx.showToast({ title: actual ? '已开启接单' : '已暂停接单', icon: 'none' }) } catch (e) { this.setData({ online: !next }) wx.showToast({ title: (e && e.message) || '切换失败,请重试', icon: 'none' }) } finally { this.setData({ onlineSaving: false }) } }, async getStats() { const now = new Date(); const pad = (n) => String(n).padStart(2, '0'); // 本地日期(toISOString 是 UTC,早晨会差一天) const todayStr = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`; const isSameDay = (d, ref) => d.getFullYear() === ref.getFullYear() && d.getMonth() === ref.getMonth() && d.getDate() === ref.getDate(); const feeOf = (item) => Number(item.payment && item.payment.totalFee) || 0; const [todayRes, completedRes] = await Promise.all([ role.queryOrders({ appointmentDate: todayStr }), role.queryOrders({ status: 'completed' }) ]); // 今日收益:当日已完成订单的费用合计 let todayIncome = 0; if (completedRes.code === 0) { (completedRes.data.records || []).forEach(item => { const d = item.schedule && item.schedule.date ? new Date(item.schedule.date) : null; if (d && !isNaN(d.getTime()) && isSameDay(d, now)) { todayIncome += feeOf(item); } }); } this.setData({ todayIncome, todayOrderCount: todayRes.code === 0 ? (todayRes.data.records || []).length : 0 }); }, /** * 加载近7日订单(含6天前至今及未来订单,按预约时间倒序,最多10条) */ async getRecentOrders() { const res = await role.queryOrders({ page: 1, pageSize: 50 }); if (res.code !== 0) return; const start = new Date(); start.setDate(start.getDate() - 6); start.setHours(0, 0, 0, 0); const pad = (n) => String(n).padStart(2, '0'); const records = (res.data.records || []) .filter(item => item.schedule && item.schedule.date && new Date(item.schedule.date) >= start) .slice(0, 10) .map(item => { const d = new Date(item.schedule.date); const dateText = `${d.getMonth() + 1}月${d.getDate()}日`; item.schedule.date = item.schedule.startTime ? `${dateText} ${item.schedule.startTime}` : `${dateText} ${pad(d.getHours())}:${pad(d.getMinutes())}`; item.displayStatus = resolveDisplayStatus(item); return item; }); this.setData({ recentOrders: records }); }, onEntryTap(e) { const { index } = e.currentTarget.dataset const item = this.data.quickEntries[index] if (item.isTab) { wx.switchTab({ url: item.url }) } else { wx.navigateTo({ url: item.url }) } }, viewAllOrders() { wx.switchTab({ url: '/pages/order/index' }) }, /** * 点击近7日订单卡片,进入订单详情 */ onOrderTap(e) { const id = e.currentTarget.dataset.id if (!id) return wx.navigateTo({ url: `/pages/order/orderDetail?id=${id}` }) } })