基于Vue+Nodejs实现宿舍管理系统

时间:2024-11-13 16:15:40

作者主页:编程指南针

作者简介:Java领域优质创作者、****博客专家 、****内容合伙人、掘金特邀作者、阿里云博客专家、51CTO特邀作者、多年架构师设计经验、腾讯课堂常驻讲师

主要内容:Java项目、Python项目、前端项目、人工智能与大数据、简历模板、学习资料、面试题库、技术互助

收藏点赞不迷路  关注作者有好处

文末获取源码 

项目编号:BS-QD-002

主要需求:

  • 学生信息录入、修改、删除、查询
  • 宿舍管理评分
  • 学生早起率、懒床率
  • 学生宿舍打扫频率
  • 学生晚归情况
  • 楼层管理

考虑到实用性,该系统需要拆分为两大子系统,一个是学生端系统,一个是后台管理端系统。学生端系统主要提供给学生使用,负责一些宿舍记录及个人信息记录的基本操作;后台管理模块则是主要负责对所有学生信息的整理,提供宿舍管理、楼层管理、数据查看等权限,提供给宿舍管理员使用的。

学生登陆

学生系统拥有以下功能:

  • 创建账户
  • 分配宿舍
  • 填写个人信息
  • 修改个人信息
  • 起床打卡(用于统计懒床率)
  • 归宿登记(用于统计晚归情况)
  • 打扫记录(用于统计宿舍打扫频率)
  • 查看宿日常数据

管理员登陆

管理系统拥有以下功能:

  • 楼层管理
  • 宿舍评价
  • 宿舍信息管理
  • 学生信息查看
  • 保洁人员管理
  • 统计学生早起率
  • 统计学生宿舍打扫频率
  • 统计学生晚归

超级管理员在享有上述管理员同等权限的同时额外拥有如下功能:

  • 创建管理员
  • 创建宿舍楼
  • 为宿舍楼分配管理员
  • 为宿舍楼分配保洁人员

前端:

  • Vue 作为基础框架
  • vue-router 控制路由(hash 模式)
  • vuex 状态管理
  • axios 接入数据
  • Vue-element-admin 作为基础框架

后台(Nodejs):

  • Koa 作为基础框架
  • koa-router —— 服务端路由控制
  • koa-static —— 读取静态文件
  • koa-jwt —— JWT 登录校验
  • koa-body —— http body 数据处理
  • koa-compress —— Gzip 压缩
  • koa-cors —— CORS 解决跨域问题
  • sequelize —— ORM

数据库:

  • MySQL

数据库设计一览:

下面展示一下系统的部分功能:

仪表盘概揽:选择不同的宿舍楼查看相关信息

管理员管理:

宿舍楼管理

楼层管理

宿舍信息

宿舍入住学生信息

查看学生起床记录

查看学生归宿信息

查看学生宿舍打扫信息

查看个人信息

学生注册

注册后登陆系统

入住宿舍

起床打卡

归宿记录

打扫记录

后端工程:

前端工程

部门核心代码:

const { Building } = require("../model")

 = {
  getStudents: async function(buildingId) {
    const FloorController = require("./floor_controller")
    let users = []
    const building = await ({ where: { id: buildingId } })
    const floors = await ()
    for (let floor of floors) {
      const floorId = 
      users = [...users, ...(await (floorId))]
    }
    return users
  },
  delBuilding: async function(id) {
    const { setStudentRoomNull } = require("./user_controller")
    const students = await (id)
    (student => {
      setStudentRoomNull()
    })
    return await ({ where: { id } })
  }
}

const _ = require("lodash")
const { User } = require("../model")

 = {
  async getEvaluatesInfo(evaluates) {
    const cpEvaluates = _.cloneDeep(evaluates)
    for (let evaluate of cpEvaluates) {
      const creator = await ()
       = 
    }
    return cpEvaluates
  }
}
const { Floor } = require("../model")

 = {
  getStudents: async function(floorId) {
    const { getStudentInfo } = require("./user_controller")
    let users = []
    const floor = await ({ where: { id: floorId } })
    const rooms = await ()
    for (let room of rooms) {
      const roomUsers = await ()
      for (let user of roomUsers) {
        (await getStudentInfo())
      }
    }
    return users
  }
}

 = {
  UserController: require("./user_controller"),
  RoomController: require("./room_controller"),
  FloorController: require("./floor_controller"),
  BuildingController: require("./building_controller"),
  EvaluateController: require("./evaluate_controller"),
  RecordController: require("./record_controller")
}

const {
  User,
  GetupRecord,
  CleanRecord,
  BackRecord,
  Room,
  Floor,
  Building
} = require("../model")
const { Op } = require("sequelize")
const moment = require("moment")
const _ = require("lodash")

const getupEarlyPoint = 8
const backEarlyPoint = 22

 = {
  // getup 相关
  async addGetupRecord(userId) {
    const user = await ({ where: { id: userId } })
    const todyRecord = await ({
      where: {
        userId: ,
        roomId: ,
        createdAt: {
          []: moment()
            .startOf("day")
            .toDate(),
          []: moment()
            .endOf("day")
            .toDate()
        }
      }
    })
    if (todyRecord) {
      throw new Error("当天已经有记录,记录失败!")
    }
    return await ({ userId: , roomId:  })
  },
  async getUserGetupRecords(userId, days, pure = false) {
    days = parseInt(days)
    const user = await ({ where: { id: userId } })
    const roomId = 
    const room = await ({ where: { id: roomId } })
    // 获取最近 days 天的记录
    const startTime = moment()
      .subtract(days - 1 /* -1 代表查询天数包括今天 */, "days")
      .startOf("day")
      .toDate()
    const allRecords = []
    for (let i = 0; i < days; i++) {
      const todayStart = moment(startTime)
        .add(i, "days")
        .toDate()
      const todayEnd = moment(todayStart)
        .endOf("day")
        .toDate()
      let record = await ({
        where: {
          userId,
          roomId,
          createdAt: {
            []: todayStart,
            []: todayEnd
          }
        },
        attributes: { exclude: ["updatedAt", "deletedAt"] }
      })
      if (record) {
        // 如果当天有记录就推入结果
        record = ()
         = moment().format("HH:mm")
      } else if (!record && !pure) {
        // 如果获取的是全部数据且当前天无数据
        // 就建立一条空记录
        record = ({
          id: "fake" + i,
          roomId,
          userId,
          createdAt: todayStart
        }).toJSON()
         = null
      } else {
        continue
      }
       = moment().format("YYYY-MM-DD")
       = 
       = 
      (record)
    }
    return ()
  },
  async getRoomGetupRecords(roomId, days, pure = false) {
    days = parseInt(days)
    const room = await ({ where: { id: roomId } })
    const users = await ()
    const records = {}
    for (let user of users) {
      records[] = await (, days, pure)
    }
    return records
  },
  async getGetupRecordLineCharData(roomId) {
    const room = await ({ where: { id: roomId } })
    const users = await ()
    const data = { columns: ["周期"], rows: [] }
    const dataCount = 5 // 获取的记录条数
    const dataStep = 7 // 每条记录相隔的条数
    // 初始化记录值
    for (let i = 0; i < dataCount; i++) {
      ({ 周期: `最近${(i + 1) * dataStep}天` })
    }
    // 遍历当前宿舍的用户
    for (let user of users) {
      ()
      for (let i = 0; i < dataCount; i++) {
        const days = (i + 1) * dataStep
        // 获取某学生最近 days 天的早起记录
        const records = await (, days, true)
        let earlyTimes = 0
        (record => {
          // 统计这些记录中有几天是早起的
          const timeHour = parseInt(moment().format("HH"))
          if (timeHour < getupEarlyPoint) {
            earlyTimes++
          }
        })
        // 计算早起率
        const probability = (earlyTimes / days).toFixed(4)
        [i][] = probability
      }
    }
    return data
  },
  async getGetupTableData({
    current,
    step,
    buildingId,
    floorId,
    roomId,
    userId,
    startTime,
    endTime
  }) {
    // 初始化时间
    startTime = startTime
      ? moment(startTime)
          .startOf("day")
          .toDate()
      : moment(0).toDate()
    endTime = endTime
      ? moment(endTime)
          .endOf("day")
          .toDate()
      : moment()
          .endOf("day")
          .toDate()
    ("endTime: ", endTime)
    // 开始分情况获取数据
    let result
    if (userId) {
      result = await ({
        where: {
          userId: userId,
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    } else if (roomId) {
      result = await ({
        where: {
          roomId: roomId,
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    } else if (floorId) {
      result = await ({
        where: {
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        include: [
          {
            model: Room,
            where: { floorId }
          }
        ],
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    } else if (buildingId) {
      result = await ({
        where: {
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        include: [
          {
            model: Room,
            where: { buildingId }
          }
        ],
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    } else {
      result = await ({
        where: {
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    }
    const getStudentInfo = require("./user_controller").getStudentInfo
    let rows = []
    for (let record of ) {
      record = ()
      delete 
      const userInfo = await getStudentInfo()
      record = (userInfo, record)
       = moment().format("HH:mm")
       = moment().format("YYYY-MM-DD")
      if (parseInt(moment().format("HH")) < getupEarlyPoint) {
         = true
      } else {
         = false
      }
      (record)
    }
     = rows
    return result
  },

  // back 相关
  async addBackRecord(userId) {
    const user = await ({ where: { id: userId } })
    const todyRecord = await ({
      where: {
        userId: ,
        roomId: ,
        createdAt: {
          []: moment()
            .startOf("day")
            .toDate(),
          []: moment()
            .endOf("day")
            .toDate()
        }
      }
    })
    if (todyRecord) {
      throw new Error("当天已经有记录,记录失败!")
    }
    return await ({ userId: , roomId:  })
  },
  async getUserBackRecords(userId, days, pure = false) {
    days = parseInt(days)
    const user = await ({ where: { id: userId } })
    const roomId = 
    const room = await ({ where: { id: roomId } })
    // 获取最近 days 天的记录
    const startTime = moment()
      .subtract(days - 1 /* -1 代表查询天数包括今天 */, "days")
      .startOf("day")
      .toDate()
    const allRecords = []
    for (let i = 0; i < days; i++) {
      const todayStart = moment(startTime)
        .add(i, "days")
        .toDate()
      const todayEnd = moment(todayStart)
        .endOf("day")
        .toDate()
      let record = await ({
        where: {
          userId,
          roomId,
          createdAt: {
            []: todayStart,
            []: todayEnd
          }
        },
        attributes: { exclude: ["updatedAt", "deletedAt"] }
      })
      if (record) {
        // 如果当天有记录就推入结果
        record = ()
         = moment().format("HH:mm")
      } else if (!record && !pure) {
        // 如果获取的是全部数据且当前天无数据
        // 就建立一条空记录
        record = ({
          id: "fake" + i,
          roomId,
          userId,
          createdAt: todayStart
        }).toJSON()
         = null
      } else {
        continue
      }
       = moment().format("YYYY-MM-DD")
       = 
       = 
      (record)
    }
    return ()
  },
  async getRoomBackRecords(roomId, days, pure = false) {
    days = parseInt(days)
    const room = await ({ where: { id: roomId } })
    const users = await ()
    const records = {}
    for (let user of users) {
      records[] = await (, days, pure)
    }
    return records
  },
  async getBackRecordLineCharData(roomId) {
    const room = await ({ where: { id: roomId } })
    const users = await ()
    const data = { columns: ["周期"], rows: [] }
    const dataCount = 5 // 获取的记录条数
    const dataStep = 7 // 每条记录相隔的条数
    // 初始化记录值
    for (let i = 0; i < dataCount; i++) {
      ({ 周期: `最近${(i + 1) * dataStep}天` })
    }
    // 遍历当前宿舍的用户
    for (let user of users) {
      ()
      for (let i = 0; i < dataCount; i++) {
        const days = (i + 1) * dataStep
        // 获取某学生最近 days 天的归宿记录
        const records = await (, days, true)
        let earlyTimes = 0
        (record => {
          // 统计这些记录中有几天是早归的
          const timeHour = parseInt(moment().format("HH"))
          if (timeHour < backEarlyPoint) {
            earlyTimes++
          }
        })
        // 计算早起率
        const probability = (earlyTimes / days).toFixed(4)
        [i][] = probability
      }
    }
    return data
  },
  async getBackTableData({
    current,
    step,
    buildingId,
    floorId,
    roomId,
    userId,
    startTime,
    endTime
  }) {
    // 初始化时间
    startTime = startTime
      ? moment(startTime)
          .startOf("day")
          .toDate()
      : moment(0).toDate()
    endTime = endTime
      ? moment(endTime)
          .endOf("day")
          .toDate()
      : moment()
          .endOf("day")
          .toDate()
    // 开始分情况获取数据
    let result
    if (userId) {
      result = await ({
        where: {
          userId: userId,
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    } else if (roomId) {
      result = await ({
        where: {
          roomId: roomId,
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    } else if (floorId) {
      result = await ({
        where: {
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        include: [
          {
            model: Room,
            where: { floorId }
          }
        ],
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    } else if (buildingId) {
      result = await ({
        where: {
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        include: [
          {
            model: Room,
            where: { buildingId }
          }
        ],
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    } else {
      result = await ({
        where: {
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    }
    const getStudentInfo = require("./user_controller").getStudentInfo
    let rows = []
    for (let record of ) {
      record = ()
      delete 
      const userInfo = await getStudentInfo()
      record = (userInfo, record)
       = moment().format("HH:mm")
       = moment().format("YYYY-MM-DD")
      if (parseInt(moment().format("HH")) < backEarlyPoint) {
         = true
      } else {
         = false
      }
      (record)
    }
     = rows
    return result
  },

  // clean 相关
  async addCleanRecord(userId) {
    const user = await ({ where: { id: userId } })
    const todyRecord = await ({
      where: {
        roomId: ,
        createdAt: {
          []: moment()
            .startOf("day")
            .toDate(),
          []: moment()
            .endOf("day")
            .toDate()
        }
      }
    })
    if (todyRecord) {
      throw new Error("当天已经有清扫记录,记录失败")
    }
    return await ({
      userId: ,
      roomId: 
    })
  },
  async getUserCleanRecords(userId, days) {
    // 获取打扫记录不会自动补全每一天的信息
    days = parseInt(days)
    const user = await ({ where: { id: userId } })
    const roomId = 
    const room = await ({ where: { id: roomId } })
    // 获取最近 days 天的记录
    const startTime = moment()
      .subtract(days - 1 /* -1 代表查询天数包括今天 */, "days")
      .startOf("day")
      .toDate()
    const todayEnd = moment()
      .endOf("day")
      .toDate()
    const records = await ({
      where: {
        userId,
        roomId,
        createdAt: {
          []: startTime,
          []: todayEnd
        }
      },
      attributes: { exclude: ["updatedAt", "deletedAt"] },
      order: [["createdAt", "DESC"]]
    })
    const allRecords = []
    (record => {
      record = ()
       = moment().format("HH:mm")
       = moment().format("YYYY-MM-DD")
       = 
       = 
      (record)
    })
    return allRecords
  },
  async getRoomCleanRecords(roomId, days) {
    const room = await ({ where: { id: roomId } })
    const startTime = moment()
      .subtract(days - 1 /* -1 代表查询天数包括今天 */, "days")
      .startOf("day")
      .toDate()
    const todayEnd = moment()
      .endOf("day")
      .toDate()
    const records = await ({
      where: {
        createdAt: {
          []: startTime,
          []: todayEnd
        }
      },
      attributes: { exclude: ["updatedAt", "deletedAt"] },
      order: [["createdAt", "DESC"]]
    })
    const allRecords = []
    for (let record of records) {
      const user = await ()
      record = ()
       = moment().format("YYYY-MM-DD")
       = moment().format("HH:mm")
       = 
       = 
      (record)
    }
    return allRecords
  },
  async getCleanTableData({
    current,
    step,
    buildingId,
    floorId,
    roomId,
    userId,
    startTime,
    endTime
  }) {
    // 初始化时间
    startTime = startTime
      ? moment(startTime)
          .startOf("day")
          .toDate()
      : moment(0).toDate()
    endTime = endTime
      ? moment(endTime)
          .endOf("day")
          .toDate()
      : moment()
          .endOf("day")
          .toDate()
    // 开始分情况获取数据
    let result
    if (userId) {
      result = await ({
        where: {
          userId: userId,
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    } else if (roomId) {
      result = await ({
        where: {
          roomId: roomId,
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    } else if (floorId) {
      result = await ({
        where: {
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        include: [
          {
            model: Room,
            where: { floorId }
          }
        ],
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    } else if (buildingId) {
      result = await ({
        where: {
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        include: [
          {
            model: Room,
            where: { buildingId }
          }
        ],
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    } else {
      result = await ({
        where: {
          createdAt: {
            []: startTime,
            []: endTime
          }
        },
        limit: step,
        offset: step * (current - 1),
        order: [["createdAt", "DESC"]]
      })
    }
    const getStudentInfo = require("./user_controller").getStudentInfo
    let rows = []
    for (let record of ) {
      record = ()
      delete 
      const userInfo = await getStudentInfo()
      record = (userInfo, record)
       = moment().format("HH:mm")
       = moment().format("YYYY-MM-DD")
       = null
      (record)
    }
     = rows
    return result
  },

  // 通用
  async getUserProbability(type, userId) {
    const user = await (userId)
    const startTime = 
    let records = []
    let allRecords = []
    switch (type) {
      case "getup":
        allRecords = await ({
          where: {
            createdAt: { []: startTime }
          }
        })
        (record => {
          let hour = parseInt(moment().format("HH"))
          if (hour < getupEarlyPoint) {
            (record)
          }
        })
        break
      case "back":
        allRecords = await ({
          where: {
            createdAt: { []: startTime }
          }
        })
        (record => {
          let hour = parseInt(moment().format("HH"))
          if (hour < backEarlyPoint) {
            (record)
          }
        })
        break
      case "clean":
        records = await ({
          where: {
            createdAt: { []: startTime }
          }
        })
        break
      default:
        throw new Error("参数传入错误")
    }
    // 计算从入住到现在有几天了
    const days = (moment(startTime).diff(moment(), "days"))
    return ( / (days + 1)).toFixed(4)
  }
}

const { User } = require("../model")
const _ = require("lodash")
const RecordController = require("./record_controller")

 = {
  /**
   * 获取学生用户的完整信息
   * @param {Number} userId
   */
  async getStudentInfo(userId) {
    const student = await ({
      where: { id: userId },
      attributes: { exclude: ["password", "deletedAt"] }
    })
    const room = await ()
    const floor = await ()
    const building = await ()
    const getupProb = await ("getup", userId)
    const backProb = await ("back", userId)
    const cleanProb = await ("clean", userId)
    const info = (, {
      roomNumber: ,
      floorId: ,
      floorLayer: ,
      buildingId: ,
      buildingName: ,
      getupProb,
      backProb,
      cleanProb
    })
    return info
  },

  /**
   * 获取学生用户们的完整信息
   * @param {Array} users
   */
  async getStudentsInfo(users) {
    const cloneUsers = _.cloneDeep(users)
    for (let user of cloneUsers) {
      delete 
      delete 
      const room = await ()
      const floor = await ()
      const building = await ()
      (, {
        roomNumber: ,
        floorId: ,
        floorLayer: ,
        buildingId: ,
        buildingName: 
      })
    }
    return cloneUsers
  },

  async setStudentRoomNull(id) {
    const student = await ({ where: { id, role: "student" } })
    const result = await ({ roomId: null })
    return result
  }
}