目录
- [1 优化数据模型](#1 优化数据模型)
- [2 优化排班功能](#2 优化排班功能)
- [3 创建获取7天排班计划的API](#3 创建获取7天排班计划的API)
- [4 前端加载](#4 前端加载)
- [5 最终效果](#5 最终效果)
- 总结
我们上一篇介绍了前端挂号日期切换组件的开发。有了挂号组件后,我们就需要从数据库里获取有效的数据供组件显示数据,本篇我们介绍一下如何通过API来获取最近7天的挂号记录。
1 优化数据模型
原来我们设计数据模型的时候,排班信息没有考虑科室字段。在前端查询的时候是需要先通过科室来查询排班信息,因此我们在排班表里添加一个所属科室字段

2 优化排班功能
在后台排班的时候,因为增加了所属科室的字段,需要在排班的时候从表格获取
选中我们的排班按钮,在传入参数改为对象类型,将医生的ID和科室的ID都传入弹窗的入参里

bash
({docotrId:$w.table1.cell__custom__option.record._id,departId:$w.table1.cell__custom__option.record.department_id._id})
在排班的表单里增加所属科室字段,设置选中值


因为我们传入了对象,在预填充号码段表格的自定义方法中,从入参的属性里获取医生的ID

3 创建获取7天排班计划的API
为了在前端展示我们7天排班的记录,需要添加一个API来组装数据
打开API,点击+号添加

选择自定义代码

输入API的名称和标识

输入方法名称和标识

添加入参

输入如下代码
bash
module.exports = async function (params, context) {
// --- 1. 核心修复:强制使用北京时间处理日期的工具函数 ---
const getBeijingDateInfo = (timestampOrDate) => {
let date;
if (typeof timestampOrDate === 'number') {
date = new Date(timestampOrDate);
} else {
date = timestampOrDate || new Date(); // 默认为当前时间
}
// 关键步骤:将当前时间加上8小时的毫秒数,强制偏移到北京时间
// 这样后续使用 getUTC... 方法获取的就是北京时间的年月日
const utc8Offset = 8 * 60 * 60 * 1000;
const beijingDate = new Date(date.getTime() + utc8Offset);
const year = beijingDate.getUTCFullYear();
const month = String(beijingDate.getUTCMonth() + 1).padStart(2, '0');
const day = String(beijingDate.getUTCDate()).padStart(2, '0');
const weekDay = beijingDate.getUTCDay(); // 0-6
return {
full: `${year}-${month}-${day}`, // "2025-11-20"
short: `${month}-${day}`, // "11-20"
weekDay: weekDay, // 星期几的索引
timestamp: date.getTime() // 原始时间戳
};
};
const getWeekLabel = (weekDayIndex, isToday) => {
if (isToday) return "今日";
const weekMap = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
return weekMap[weekDayIndex];
};
// --- 2. 确定查询范围 (基于北京时间) ---
const now = new Date();
// 获取今天的起始时间戳(北京时间 00:00:00)
// 我们利用 getBeijingDateInfo 获取今天的字符串,再反推时间戳,确保精准
const todayInfo = getBeijingDateInfo(now);
// 为了查询数据库,我们需要标准的时间戳
// 简单起见,查询范围设为:当前时间 - 1天 到 当前时间 + 8天 (稍微放宽范围,依靠JS内存筛选保证准确)
const startTimestamp = now.getTime();
const endTimestamp = startTimestamp + (8 * 24 * 60 * 60 * 1000);
// --- 3. 调用数据库 ---
const result = await context.callModel({
name: 'schedules',
methodName: 'wedaGetRecordsV2',
params: {
filter: {
where: {
$and: [
{ depart_id: { $eq: params.departid } },
{ date: { $gte: startTimestamp } }, // 注意:这里如果数据库存的是0点时间戳,可能需要调整,建议用宽泛范围
{ date: { $lte: endTimestamp } }
]
}
},
pageSize: 100,
pageNumber: 1
}
});
// --- 4. 处理数据 (构建哈希表) ---
const scheduleMap = {};
if (result.records) {
result.records.forEach(record => {
// 修复点:使用北京时间工具函数处理数据库返回的 date
const dateInfo = getBeijingDateInfo(record.date);
const dateKey = dateInfo.full; // "2025-11-20"
if (!scheduleMap[dateKey]) {
scheduleMap[dateKey] = 0;
}
scheduleMap[dateKey] += (record.remaining || 0);
});
}
// --- 5. 生成未来7天的数据 ---
const responseData = [];
// 我们从"今天"开始循环7次
// 这里的"今天"也必须基于北京时间
const oneDayMs = 24 * 60 * 60 * 1000;
for (let i = 0; i < 7; i++) {
// 计算第 i 天的时间戳 (基于当前的原始时间戳 + i天)
const targetTimestamp = now.getTime() + (i * oneDayMs);
// 再次使用工具函数转化为北京时间的字符串
const dateInfo = getBeijingDateInfo(targetTimestamp);
let statusStr = "未出诊";
// 用 "2025-11-20" 去 map 里找
if (scheduleMap[dateInfo.full] !== undefined) {
const remainingCount = scheduleMap[dateInfo.full];
if (remainingCount > 0) {
statusStr = "有号";
} else {
statusStr = "满号";
}
}
responseData.push({
id: i + 1,
date: dateInfo.short,
label: getWeekLabel(dateInfo.weekDay, i === 0),
select: i === 0,
hasSlot: statusStr,
fullDate: dateInfo.full
});
}
return {
code: 0,
message: '获取排班成功',
data: responseData
};
};
然后点击方法测试,如果看到查询成功就点击出参映射完成API的编写

4 前端加载
后端API写好之后,就需要在前端进行加载。我们在上一节是用数组来展示数据,就需要考虑数组什么时候获取数据。
一般我们是在页面的生命周期函数里调用初始化方法加载数据。在代码区创建一个javascript方法

选中页面组件,我们添加一个URL参数,用来接收科室ID

然后在自定义代码中输入如下代码:
bash
export default async function({event, data}) {
const departid = $w.page.dataset.params.departid
const result = await $w.cloud.callDataSource({
dataSourceName: 'guahaoyuyue_dhhs59v',
methodName: 'getLatestSchedulesPlan',
params: {
departid:departid
}, // 方法入参
});
console.log(result.data)
$w.page.dataset.state.canSelectDate = result.data
}
设置页面的显示事件,调用我们的方法

回到科室选择页面,给科室设置点击事件,打开我们的日期选择页面,并且传入科室ID

5 最终效果
打开科室选择页面

点击一个科室,跳转到日期选择页面,显示最近7天的排班计划

总结
本篇我们介绍了使用API获取最近7天排班计划的功能,涉及到后端开发API,前端调用API以及页面跳转传参的问题。好多初学者认为低代码就是简单的托拉拽,其实那是认知偏差。不是低代码只是托拉拽,是你的场景比较简单,只需要托拉拽就可以了。