master
bettleChen 1 year ago
parent 41e76a2742
commit e943dc92a5

@ -31,18 +31,18 @@ class JWTMiddleware:
# 将解码后的 Token 数据存储在 request 中,以便视图可以访问 # 将解码后的 Token 数据存储在 request 中,以便视图可以访问
if payload.get("type") == "student": if payload.get("type") == "student":
if not Student.objects.filter(username=payload.get("username")).exists(): if not Student.objects.filter(username=payload.get("username")).exists():
return JsonResponse(ResponseUtil.error("错误信息"), status=401) return JsonResponse(ResponseUtil.error("用户不存在"), status=401)
if payload.get("type") == "teacher": if payload.get("type") == "teacher":
if not Teacher.objects.filter(username=payload.get("username")).exists(): if not Teacher.objects.filter(username=payload.get("username")).exists():
return JsonResponse(ResponseUtil.error("错误信息"), status=401) return JsonResponse(ResponseUtil.error("用户不存在"), status=401)
if payload.get("type") == "admin": if payload.get("type") == "admin":
if not Admin.objects.filter(username=payload.get("username")).exists(): if not Admin.objects.filter(username=payload.get("username")).exists():
return JsonResponse(ResponseUtil.error("错误信息"), status=401) return JsonResponse(ResponseUtil.error("用户不存在"), status=401)
request.jwt_payload = payload request.jwt_payload = payload
except jwt.ExpiredSignatureError: except jwt.ExpiredSignatureError:
return JsonResponse(ResponseUtil.error("登录失效"), status=401) return JsonResponse(ResponseUtil.error("Token 失效,请重新登录"), status=401)
except jwt.DecodeError: except jwt.DecodeError:
return JsonResponse(ResponseUtil.error("登录失效"), status=401) return JsonResponse(ResponseUtil.error("Token Error"), status=401)
# 继续处理请求 # 继续处理请求
response = self.get_response(request) response = self.get_response(request)
return response return response

@ -9,10 +9,13 @@ from teacher.models import Teacher
from EduSystemServer.settings import TOKEN_KEY from EduSystemServer.settings import TOKEN_KEY
from EduSystemServer.utils import * from EduSystemServer.utils import *
# 用户登录成功后生成 JWT Token # 用户登录成功后生成 JWT Token
def generate_jwt_token(user, _type): def generate_jwt_token(user, _type):
# 设置 Token 的有效期 # 设置 Token 的有效期
expiration_time = datetime.utcnow() + timedelta(hours=1) expiration_time = datetime.utcnow() + timedelta(hours=1)
print(datetime.utcnow())
print(expiration_time)
# 构建 Token 数据 # 构建 Token 数据
payload = { payload = {

@ -99,7 +99,7 @@ def select_course(request):
try: try:
request_data = request.POST request_data = request.POST
cid = request_data.get("cid") cid = request_data.get("cid")
username = request_data.get("username") username = request.jwt_payload.get("username")
student = Student.objects.filter(username=username).first() student = Student.objects.filter(username=username).first()
if SC.objects.filter(cid=cid, sid=student.sid).exists(): if SC.objects.filter(cid=cid, sid=student.sid).exists():
return JsonResponse(ResponseUtil.error("该课程已经选择!")) return JsonResponse(ResponseUtil.error("该课程已经选择!"))

@ -12,15 +12,15 @@ class Course(models.Model):
credit = models.IntegerField(verbose_name="课程学分", name="credit") credit = models.IntegerField(verbose_name="课程学分", name="credit")
tid = models.ForeignKey(Teacher, to_field="tid", on_delete=models.CASCADE, name="tid") tid = models.ForeignKey(Teacher, to_field="tid", on_delete=models.CASCADE, name="tid")
def to_dict(self):
return {"cid": self.cid, "c_name": self.name, "type": self.type, "credit": self.credit,
"tid": self.tid}
class Meta: class Meta:
db_table = "course" db_table = "course"
verbose_name = "课程" verbose_name = "课程"
verbose_name_plural = verbose_name verbose_name_plural = verbose_name
def to_dict(self):
return {"cid": self.cid, "c_name": self.name, "type": self.type, "credit": self.credit,
"tid": self.tid}
class SC(models.Model): class SC(models.Model):
sid = models.ForeignKey(Student, to_field="sid", on_delete=models.CASCADE, name="sid") sid = models.ForeignKey(Student, to_field="sid", on_delete=models.CASCADE, name="sid")

Loading…
Cancel
Save