高校实验室资源管理一直是个让人头疼的问题。记得我读大学时,每次做实验都要提前一周跑去实验室门口排队登记,经常遇到时间冲突或者找不到负责老师的情况。现在有了微信小程序这个国民级应用入口,结合SpringBoot后端技术,完全可以打造一个24小时在线的实验室预约系统。
这个系统的核心价值在于:
采用经典的三层架构:
code复制微信小程序前端 -> SpringBoot后端 -> MySQL数据库
选择这套组合的考虑:
java复制// 典型的核心依赖示例
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.baomidou:mybatis-plus-boot-starter:3.5.2'
implementation 'com.github.binarywang:weixin-java-miniapp:4.1.0'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
}
特别注意:微信小程序开发需要提前申请教育类目资质,审核周期约3-7个工作日
实验室预约最关键的冲突检测逻辑:
java复制public boolean checkTimeConflict(LabAppointment newAppoint) {
return appointmentMapper.selectList(new QueryWrapper<LabAppointment>()
.eq("lab_id", newAppoint.getLabId())
.eq("appoint_date", newAppoint.getAppointDate())
.le("start_time", newAppoint.getEndTime())
.ge("end_time", newAppoint.getStartTime())
).isEmpty();
}
这个SQL查询会找出同一实验室、同一天内所有与新预约时段有重叠的已有预约。
mermaid复制sequenceDiagram
participant 用户
participant 小程序
participant 服务端
用户->>小程序: 点击登录按钮
小程序->>微信服务器: 调用wx.login()
微信服务器-->>小程序: 返回code
小程序->>服务端: 提交code
服务端->>微信服务器: code+appid+secret换session_key
微信服务器-->>服务端: 返回openid等
服务端->>服务端: 生成自定义登录态token
服务端-->>小程序: 返回token
实际开发中建议将session_key加密存储,不要直接返回给前端
| 表名 | 关键字段 | 说明 |
|---|---|---|
| lab_info | id, name, capacity, equipment, location | 实验室基本信息 |
| appointment | id, user_id, lab_id, date, start_time, end_time, status | 预约记录 |
| user | openid, name, student_id, role_type | 用户信息 |
| equipment | id, lab_id, name, status, last_maintain | 设备管理 |
sql复制-- 高频查询必须添加索引
CREATE INDEX idx_lab_date ON appointment(lab_id, appoint_date);
CREATE INDEX idx_user_status ON appointment(user_id, status);
java复制public enum AppointStatus {
PENDING("待审核", Arrays.asList("CANCEL", "REJECT", "APPROVE")),
APPROVED("已通过", Arrays.asList("COMPLETE", "CANCEL")),
REJECTED("已拒绝", Collections.emptyList()),
COMPLETED("已完成", Collections.emptyList()),
CANCELLED("已取消", Collections.emptyList());
// 状态转换校验逻辑
public boolean canTransferTo(String targetStatus) {
return allowedTransfers.contains(targetStatus);
}
}
使用Redis缓存两类数据:
java复制@Cacheable(value = "lab_status", key = "#labId+'_'+#date")
public List<TimeSlot> getLabStatus(Integer labId, String date) {
// 数据库查询逻辑
}
每天凌晨执行的维护任务:
java复制@Scheduled(cron = "0 0 3 * * ?")
public void dailyMaintenance() {
// 业务逻辑实现
}
java复制@RateLimiter(value = 5, key = "#userId")
public Result submitAppointment(AppointmentDTO dto) {
// 业务逻辑
}
最低配置要求:
基于历史数据实现:
未来可扩展:
java复制// 分布式锁使用示例
public boolean lockLab(Integer labId) {
String lockKey = "lab_lock:" + labId;
return redisTemplate.opsForValue().setIfAbsent(lockKey, "1", 30, TimeUnit.SECONDS);
}
这个项目最让我有成就感的是看到学生不再需要排队预约实验室,管理员也能实时掌握设备使用情况。特别是在疫情期间,无接触的预约方式显得尤为重要。如果让我重新设计,我会更早引入Elasticsearch来解决历史记录检索慢的问题。