parent
61123816d4
commit
e6551c7ddb
@ -1,4 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7 (EduSystemServer)" project-jdk-type="Python SDK" />
|
||||||
</project>
|
</project>
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,23 @@
|
|||||||
|
from django.http import JsonResponse
|
||||||
|
|
||||||
|
|
||||||
|
class AuthMiddleware:
|
||||||
|
"""
|
||||||
|
验证权限登录中间件
|
||||||
|
"""
|
||||||
|
def __init__(self, get_response):
|
||||||
|
self.get_response = get_response
|
||||||
|
|
||||||
|
def __call__(self, request):
|
||||||
|
path = request.path
|
||||||
|
if path in [
|
||||||
|
"/login",
|
||||||
|
]:
|
||||||
|
response = self.get_response(request)
|
||||||
|
return response
|
||||||
|
|
||||||
|
session = request.session
|
||||||
|
if not session.get("username") and not session.get("type"):
|
||||||
|
return JsonResponse({"code": -1, "msg": "not login!"}, status=401)
|
||||||
|
response = self.get_response(request)
|
||||||
|
return response
|
@ -1,9 +1,53 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
|
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
from Student.models import *
|
||||||
|
from teacher.models import Teacher
|
||||||
|
from EduSystemServer.utils import ResponseUtil
|
||||||
|
|
||||||
|
|
||||||
def student(request):
|
@csrf_exempt
|
||||||
return JsonResponse({"sid": 1, "sname": "张三"})
|
def login(request):
|
||||||
|
username = json.loads(request.body).get("username")
|
||||||
|
password = json.loads(request.body).get("password")
|
||||||
|
_type = json.loads(request.body).get("type")
|
||||||
|
if _type == "student":
|
||||||
|
student = Student.objects.filter(username=username,
|
||||||
|
password=password).first()
|
||||||
|
if student:
|
||||||
|
request.session["username"] = student.username
|
||||||
|
request.session["type"] = "student"
|
||||||
|
result = {"code": 0, "message": "login success!"}
|
||||||
|
else:
|
||||||
|
result = {"code": -1, "message": "username or password error!"}
|
||||||
|
elif _type == "teacher":
|
||||||
|
teacher = Teacher.objects.filter(username=username,
|
||||||
|
password=password).first()
|
||||||
|
if teacher:
|
||||||
|
request.session["username"] = teacher.username
|
||||||
|
request.session["type"] = "teacher"
|
||||||
|
result = {"code": 0, "message": "login success!"}
|
||||||
|
else:
|
||||||
|
result = ResponseUtil.error("username or password error!")
|
||||||
|
else:
|
||||||
|
result = ResponseUtil.error("type error!")
|
||||||
|
return JsonResponse(result)
|
||||||
|
@csrf_exempt
|
||||||
|
def get_user_info(request):
|
||||||
|
_type = request.GET.get("type")
|
||||||
|
username = request.GET.get("username")
|
||||||
|
if _type == "student":
|
||||||
|
student = Student.objects.filter(username=username).first()
|
||||||
|
result = ResponseUtil.ok(student.to_dict())
|
||||||
|
elif _type == "teacher":
|
||||||
|
teacher = Teacher.objects.filter(username=username).first()
|
||||||
|
result = ResponseUtil.ok(teacher.to_dict())
|
||||||
|
else:
|
||||||
|
result = ResponseUtil.error("type error")
|
||||||
|
request.session.clear()
|
||||||
|
return JsonResponse(result)
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
|
||||||
|
class ResponseUtil:
|
||||||
|
@staticmethod
|
||||||
|
def ok(data, message="success!"):
|
||||||
|
return {"code": 0, "message": message, "data": data}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def error(message="error!"):
|
||||||
|
return {"code": -1, "message": message}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue