def post(self, request): # 接收一下后台传递过来的值 check_name = request.POST.get("admin_name", None) check_password = request.POST.get("password", None) # 对数据进行验证,用户名和密码是否规范合理 check_status = Base.check_data(check_name, check_password) # 判断当前用户是否存在 info = Admins.objects.filter(admin_name=check_name).exists() if info and check_status['errors'] is None: #判断账户是否可用 status = Admins.objects.get(admin_name=check_name).is_status if status == 0: return JsonResponse({ "status": "fail", "message": "登录失败。账户不可用!", "info": "请更改密码" }) # 判断密码是否和数据库一致 pwd = Admins.objects.get(admin_name=check_name).password if pwd == check_password: ip = Base.get_ip(request) info = Admins.objects.get(admin_name=check_name) info.last_ip = ip info.save() request.session['admin_name'] = check_name # url_info = "/cms/index/" # 访问成功后跳转的路径 url_info = "/cms/" #因为检查登录状态那里已经写了跳转路径,所以此处只用写cms就可,否则会跳错 return JsonResponse({ "status": "success", "message": "登录成功,可以进入后台首页", "info": url_info }) else: return JsonResponse({ "status": "fail", "message": "登录失败。密码错误", "info": "请更改密码" }) elif info is False: return JsonResponse({ "status": "fail", "message": "当前用户不存在", "info": "请检查用户名" }) elif check_status['errors'] is not None: return JsonResponse({ "status": "fail", "message": "登录信息有误", "info": check_status['errors'] }) else: return JsonResponse({ "status": "fail", "message": "当前登录信息发生了不可知的错误,请重新输入", "info": "" })
def post(self, request): # 接收输入信息 check_name = request.POST.get('admin_name', None) check_pwd = request.POST.get('password', None) # 验证信息 check_status = Base.check_data(check_name, check_pwd) # 判断当前用户是存在 info = Admins.objects.filter(admin_name=check_name).exists() # 存在 if info and check_status['errors'] is None: pwd = Admins.objects.get(admin_name=check_name).password if check_password(check_pwd, pwd): # 获取信息 ip = Base.get_ip(request) info = Admins.objects.get(admin_name=check_name) info.last_ip = ip info.save() request.session['admin_name'] = check_name # request.session.set_expiry(60) request.session.set_expiry(604800) info_url = '/cms/' return JsonResponse({ 'status': 'success', 'message': '登录成功,可以进入首页', 'info': info_url }) else: return JsonResponse({ 'status': 'fail', 'message': '登录失败,密码错误', 'info': "请重新输入密码" }) elif info == False: return JsonResponse({ 'status': 'fail', 'message': '当前用户不存在', 'info': '请检查用户名' }) elif check_status['errors'] is not None: return JsonResponse({ 'status': 'fail', 'message': '登录信息错误', 'info': check_status['errors'] }) else: return JsonResponse({ 'status': 'fail', 'message': '出现未知错误,请重新输入', 'info': '' })
def post(self, request): # 接收前台数据 check_name = request.POST.get('admin_name') check_pwd = request.POST.get('pwd') # 对数据进行校验 check_status = Base.check_data(check_name, check_pwd) # 判断用户名是否注册 info = Admin.objects.filter(admin_name=check_name).exists() if info and check_status['errors'] is None: if Admin.objects.get(admin_name=check_name).password == check_pwd: ip = Base.get_ip(request) info = Admin.objects.get(admin_name=check_name) info.last_ip = ip info.last_time = Base.getNowTime(request) info.save() request.session['admin_name'] = check_name # print('/cms/index/?id=' + str(info.id)) return JsonResponse({ 'status': 'success', 'message': '登录成功,可以跳转后台页面', 'info': '/cms/index/?id=' + str(info.id), }) else: return JsonResponse({ 'status': 'fail', 'message': '密码错误', 'info': '' }) elif not info: return JsonResponse({ 'status': 'fail', 'message': '用户名不存在', 'info': '' }) elif check_status['errors']: return JsonResponse({ 'status': 'fail', 'message': check_status['errors'], 'info': '' }) else: return JsonResponse({ 'status': 'fail', 'message': '存在不可预知的错误', 'info': '' })