diff --git a/EduSystemServer/API/middle.py b/EduSystemServer/API/middle.py index 209280a..313bd7c 100644 --- a/EduSystemServer/API/middle.py +++ b/EduSystemServer/API/middle.py @@ -31,18 +31,18 @@ class JWTMiddleware: # 将解码后的 Token 数据存储在 request 中,以便视图可以访问 if payload.get("type") == "student": 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 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 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 except jwt.ExpiredSignatureError: - return JsonResponse(ResponseUtil.error("登录失效!"), status=401) + return JsonResponse(ResponseUtil.error("Token 失效,请重新登录!"), status=401) except jwt.DecodeError: - return JsonResponse(ResponseUtil.error("登录失效!"), status=401) + return JsonResponse(ResponseUtil.error("Token Error!"), status=401) # 继续处理请求 response = self.get_response(request) return response \ No newline at end of file diff --git a/EduSystemServer/API/views.py b/EduSystemServer/API/views.py index 17abca2..f1dc4e3 100644 --- a/EduSystemServer/API/views.py +++ b/EduSystemServer/API/views.py @@ -9,10 +9,13 @@ from teacher.models import Teacher from EduSystemServer.settings import TOKEN_KEY from EduSystemServer.utils import * + # 用户登录成功后生成 JWT Token def generate_jwt_token(user, _type): # 设置 Token 的有效期 expiration_time = datetime.utcnow() + timedelta(hours=1) + print(datetime.utcnow()) + print(expiration_time) # 构建 Token 数据 payload = { diff --git a/EduSystemServer/Student/views.py b/EduSystemServer/Student/views.py index f75a453..9758234 100644 --- a/EduSystemServer/Student/views.py +++ b/EduSystemServer/Student/views.py @@ -99,7 +99,7 @@ def select_course(request): try: request_data = request.POST cid = request_data.get("cid") - username = request_data.get("username") + username = request.jwt_payload.get("username") student = Student.objects.filter(username=username).first() if SC.objects.filter(cid=cid, sid=student.sid).exists(): return JsonResponse(ResponseUtil.error("该课程已经选择!")) diff --git a/EduSystemServer/course/models.py b/EduSystemServer/course/models.py index fd2466f..455fa36 100644 --- a/EduSystemServer/course/models.py +++ b/EduSystemServer/course/models.py @@ -12,15 +12,15 @@ class Course(models.Model): credit = models.IntegerField(verbose_name="课程学分", name="credit") 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: db_table = "course" 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): sid = models.ForeignKey(Student, to_field="sid", on_delete=models.CASCADE, name="sid")