Compare commits

...

6 Commits

Author SHA1 Message Date
wangsiyuan dc66080c22 更新 mysql.sql 2023-12-28 23:32:11 +08:00
wangsiyuan f69c0a112f 更新 database_manager.py 2023-12-28 23:32:08 +08:00
wangsiyuan d41e846ee4 更新 views.py 2023-12-28 23:32:06 +08:00
wangsiyuan c7024d5851 创建 time_utils.py 2023-12-28 23:32:03 +08:00
wangsiyuan ada0a97a42 更新 attendance.html 2023-12-28 23:32:00 +08:00
wangsiyuan fdd09d5e4d 更新 upload_excel.js 2023-12-28 23:31:52 +08:00
6 changed files with 134 additions and 7 deletions

View File

@ -36,7 +36,7 @@ fileInput.addEventListener('change', function () {
}) })
.catch(error => { .catch(error => {
console.error('Upload failed:', error); console.error('Upload failed:', error);
alert("文件上传失败!"); // 弹出失败消息 layer.msg("文件上传失败!"); // 弹出失败消息
}); });
} else { } else {
alert("请上传Excel文件!"); alert("请上传Excel文件!");

View File

@ -44,9 +44,10 @@
</div> </div>
<div class="layui-body"> <div class="layui-body">
<div id="sign-in-reminder" class="layui-container"> <div id="sign-in-reminder" class="layui-container">
<h1>课程签到</h1> <blockquote class="layui-elem-quote layui-text" id="title">
<p>课程即将开始,请抓紧时间签到!</p> 课程签到
<!-- 操作按钮 --> </blockquote>
<div class="layui-text" id="course-info"></div>
<div class="layui-row" style="margin-top: 20px;"> <div class="layui-row" style="margin-top: 20px;">
<div class="layui-col-xs12"> <div class="layui-col-xs12">
<button class="layui-btn" id="sign-in-btn">立即签到</button> <button class="layui-btn" id="sign-in-btn">立即签到</button>
@ -62,7 +63,41 @@
<script src="/static/js/menu.js"></script> <script src="/static/js/menu.js"></script>
<script src="/static/js/logout.js"></script> <script src="/static/js/logout.js"></script>
<script> <script>
// 请求后端获取菜单数 $(document).ready(function () {
// 获取课程名称或状态
$.get("/api/get-course-name", function (response) {
if (response.msg === "ok") {
// 如果后端返回课程名
$("#course-info").text("课程:" + response.data.course_name +" 在上课时间内,请及时签到");
// 启用签到按钮
$("#sign-in-btn").prop('disabled', false);
} else {
// 根据不同的消息更新状态
$("#course-info").text(response.msg); // 显示没有课程的消息
// 禁用签到按钮
$("#sign-in-btn").prop('disabled', true);
}
});
// 绑定签到按钮事件
$("#sign-in-btn").click(function () {
if (!$(this).prop('disabled')) {
// 发送签到请求到后端
$.post("/api/student-sign-in", function (response) {
// 处理签到后的响应
if (response.success) {
layer.msg('签到成功!');
} else {
layer.msg("签到失败!");
}
});
} else {
alert("当前不可签到"); // Or handle disabled button click as needed
}
});
});
</script> </script>
</body> </body>
</html> </html>

25
app/utils/time_utils.py Normal file
View File

@ -0,0 +1,25 @@
import datetime
time_periods = {
1: {"period_name": "一、二节", "start_time": "08:00:00", "end_time": "09:30:00"},
2: {"period_name": "三、四节", "start_time": "10:00:00", "end_time": "11:30:00"},
3: {"period_name": "五、六节", "start_time": "14:30:00", "end_time": "16:00:00"},
4: {"period_name": "七、八节", "start_time": "16:30:00", "end_time": "18:00:00"}
}
def check_now_time():
# 获取当前时间
current_time = datetime.datetime.now().time()
# 遍历time_periods的每个时间段
for period_id, period_info in time_periods.items():
start_time = datetime.datetime.strptime(period_info["start_time"], "%H:%M:%S").time()
end_time = datetime.datetime.strptime(period_info["end_time"], "%H:%M:%S").time()
# 检查当前时间是否在时间段内
if start_time <= current_time <= end_time:
return period_id, period_info["period_name"]
# 如果当前时间不在任何一个时间段内
return None, "当前不在任何课程时间段内"

View File

@ -1,8 +1,9 @@
import datetime
import os import os
import openpyxl as openpyxl import openpyxl as openpyxl
from flask import Flask, redirect, url_for, render_template, session, jsonify, request, send_file from flask import Flask, redirect, url_for, render_template, session, jsonify, request, send_file
from utils.time_utils import check_now_time
from utils.allowed_files import allowed_excel from utils.allowed_files import allowed_excel
from db.connection import MySQLPool from db.connection import MySQLPool
from config import SECRET_KEY from config import SECRET_KEY
@ -291,5 +292,53 @@ def receive_excel():
return jsonify({"error": "Invalid file type"}), 400 return jsonify({"error": "Invalid file type"}), 400
@app.route('/api/get-course-name', methods=['GET'])
def get_course_name():
data = {'course_name': "信息技术"}
return jsonify({
'msg': 'ok',
'data': data
})
period_id, period_name = check_now_time() # 获取当前时间段信息
print(f"period_id: {period_id}, period_name: {period_name}")
# 如果当前不在任何时间段内
if period_id is None:
return jsonify({
'msg': period_name, # 返回不在课程时间段内的消息
'data': None
})
# 获取当前用户编号
number = session.get('number')
if not number:
return jsonify({"msg": "用户未登录或编号不可用", "data": None})
# 获取今天是星期几
day_of_week = datetime.date.today().weekday() + 1
# 如果是周末
if not (1 <= day_of_week <= 5):
return jsonify({"msg": "周末没有课程", "data": None})
# 如果是工作日,获取课程信息
print(f"day of week: {day_of_week}")
db_manager = DatabaseManager()
data = db_manager.get_course_name(number, day_of_week)
print(data)
# 返回课程信息
return jsonify({
'msg': 'ok',
'data': data
})
@app.route('/api/student-sign-in', methods=['POST'])
def student_sign_in():
number = session.get("number")
print(number)
return "aaa"
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True) app.run(debug=True)

View File

@ -124,3 +124,22 @@ WHERE
""" """
data = (student.student_name, student.student_number, student.user_id, student.major_id, student.class_name) data = (student.student_name, student.student_number, student.user_id, student.major_id, student.class_name)
return self.execute(sql, data) return self.execute(sql, data)
def get_course_name(self, student_number, day_of_week):
# 从student表获取class_name
sql_student = "SELECT class_name FROM student WHERE student_number = %s;"
class_name = self.fetch(sql_student, (student_number,))[0]['class_name']
# 使用class_name和day_of_week从schedule表获取course_id
sql_schedule = "SELECT course_id, FROM schedule WHERE day_of_week = %s AND class_name = %s;"
course_id_list = self.fetch(sql_schedule, (day_of_week, class_name))
# 对于每一个course_id从course表中查询course_name
course_names = []
for course_id in course_id_list:
sql_course = "SELECT course_name FROM course WHERE course_id = %s;"
course_name = self.fetch(sql_course, (course_id['course_id'],))
if course_name:
course_names.extend([cn['course_name'] for cn in course_name])
return course_names

View File

@ -246,4 +246,3 @@ VALUES ('一、二节', '08:00:00', '09:30:00'),
('三、四节', '10:00:00', '11:30:00'), ('三、四节', '10:00:00', '11:30:00'),
('五、六节', '14:30:00', '16:00:00'), ('五、六节', '14:30:00', '16:00:00'),
('七、八节', '16:30:00', '18:00:00'); ('七、八节', '16:30:00', '18:00:00');