Files
wxapp_escort_admin/pages/home/index.js
T
2026-09-03 13:29:39 +08:00

191 lines
5.7 KiB
JavaScript

// pages/home/index.js
const API = require('../../utils/api.js')
Page({
data: {
today: '',
greeting: '',
todayText: '',
pendingCount: 0,
completedCount: 0,
recentOrders: [],
// 快捷入口:type=quick 为快捷操作(橙色系),type=function 为功能入口(绿色系),分行显示
quickEntries: [
{ type: 'quick', icon: 'add-circle', name: '增加预约', url: '/pages/order/orderEdit', color: '#ff8f4d' },
{ type: 'quick', icon: 'file-add', name: '增加档案', url: '/pages/healthprofile/profileInfo', color: '#ff8f4d' },
{ type: 'quick', icon: 'lightbulb-circle-filled', name: 'AI办公', url: '/pages/ai/index', isTab: true, color: '#ff8f4d' },
{ type: 'function', icon: 'bill', name: '预约订单', url: '/pages/order/index', color: '#1abc9c' },
{ type: 'function', icon: 'heart', name: '健康档案', url: '/pages/healthprofile/index', color: '#1abc9c' },
{ type: 'function', icon: 'user', name: '注册用户', url: '/pages/customer/index', color: '#1abc9c' },
{ type: 'function', icon: 'user-circle', name: '个人信息', url: '/pages/me/index', color: '#1abc9c' }
],
online: true,
profile: {
name: '',
level: '初级陪诊师',
avatar: ''
},
todayIncome: 0,
todayOrderCount: 0,
pendingOrder: {
count: 1,
hospital: '中医院',
time: '10:00',
price: 88
},
ongoingOrder: {
count: 1,
hospital: '康复医院',
time: '15:30',
price: 128
},
statusMap: {
pending: '待确认',
confirmed: '已确认',
in_progress: '进行中',
completed: '已完成',
cancelled: '已取消'
}
},
onShareAppMessage() {
return {
title: '暖橙陪诊后台', // 转发标题
path: '/pages/home/index',
}
},
onShareTimeline: function () {
return {
title: '暖橙陪诊后台',
}
},
onLoad(options) {
this.loadUser()
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}` });
this.getRecentOrders();
this.getStats();
},
onShow() {
this.loadUser()
this.getRecentOrders();
this.getStats();
},
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
})
},
onOrderCardTap(e) {
const { type } = e.currentTarget.dataset
const title = type === 'pending' ? '待接单' : '进行中'
wx.showToast({
title: `${title} 订单列表`,
icon: 'none'
})
},
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([
API.escort.getAllRecords({ appointmentDate: todayStr }),
API.escort.getAllRecords({ 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 API.escort.getAllRecords({ 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);
item.schedule.date = `${d.getMonth() + 1}${d.getDate()}${pad(d.getHours())}:${pad(d.getMinutes())}`;
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}`
})
}
})