Files
wxapp_escort_admin/pages/home/index.js
T
2026-09-03 11:46:06 +08:00

169 lines
4.6 KiB
JavaScript

// pages/home/index.js
const API = require('../../utils/api.js')
Page({
data: {
today: '',
pendingCount: 0,
completedCount: 0,
recentOrders: [],
// 快捷入口:前3个为快捷操作(橙色系),后4个为功能入口(绿色系)
quickEntries: [
{ icon: 'add-circle', name: '增加预约', url: '/pages/order/orderEdit', color: '#ff8f4d' },
{ icon: 'file-add', name: '增加档案', url: '/pages/healthprofile/profileInfo', color: '#ff8f4d' },
{ icon: 'lightbulb-circle-filled', name: 'AI办公', url: '/pages/ai/index', isTab: true, color: '#ff8f4d' },
{ icon: 'bill', name: '预约订单', url: '/pages/order/index', color: '#1abc9c' },
{ icon: 'heart', name: '健康档案', url: '/pages/healthprofile/index', color: '#1abc9c' },
{ icon: 'user', name: '注册用户', url: '/pages/customer/index', color: '#1abc9c' },
{ icon: 'user-circle', name: '个人信息', url: '/pages/me/index', color: '#1abc9c' }
],
online: true,
profile: {
name: '',
level: '初级陪诊师',
avatar: ''
},
todayIncome: 168,
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);
this.setData({ today });
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 today = new Date().toISOString().substring(0, 10);
const [todayRes, pendingRes, completedRes] = await Promise.all([
API.escort.getAllRecords({ appointmentDate: today }),
API.escort.getAllRecords({ status: 'pending,confirmed' }),
API.escort.getAllRecords({ status: 'completed' })
]);
this.setData({
todayOrderCount: todayRes.code === 0 ? (todayRes.data.records || []).length : 0,
pendingCount: pendingRes.code === 0 ? (pendingRes.data.records || []).length : 0,
completedCount: completedRes.code === 0 ? (completedRes.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}`
})
}
})