Files
wxapp_companion/pages/home/index.js
T
lik 51b6ef497b feat(attendant): 展示实习/正式身份与实习只读提示
- 角色解析缓存陪诊师阶段与等级,实习仍可进入工作台

- 首页与个人中心展示身份文案,实习期给出不能接单的提示

- 申请页文案说明审核通过后为实习陪诊师、转正后可接单
2026-11-10 12:02:08 +08:00

211 lines
6.9 KiB
JavaScript

// pages/home/index.js
const role = require('../../utils/role.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', 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', 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: true,
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
this.setData({
role: r,
isAdmin: r === 'admin',
// 实习陪诊师可进工作台查看,但不能接单:首页给一条只读提示
isIntern: !!(attendant && attendant.stage === 'intern'),
levelText: r === 'admin' ? '管理员' : (attendant ? attendant.text : ''),
quickEntries: r === 'admin' ? ADMIN_ENTRIES : (r === 'attendant' ? ATTENDANT_ENTRIES : [])
})
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(() => {})
},
toggleOnline() {
this.setData({
online: !this.data.online
})
},
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.navigateTo({
url: '/pages/order/index'
})
},
/**
* 点击近7日订单卡片,进入订单详情
*/
onOrderTap(e) {
const id = e.currentTarget.dataset.id
if (!id) return
wx.navigateTo({
url: `/pages/order/orderDetail?id=${id}`
})
}
})