def login(request): resp_dict = {'code': 30000, 'message': '用户登录成功'} username = request.data.get('username', '') password = request.data.get('password', '') password = to_md5_hex(password) user = User.objects.filter(username=username, password=password) \ .only('userid').first() if user: request.session['userid'] = user.userid request.session['realname'] = user.realname with transaction.atomic(): resp_dict['token'] = token = uuid1().hex UserToken.objects.update_or_create(user=user, defaults={'token': token}) current_time = timezone.now() delta = current_time - user.lastvisit if delta.days >= 1: user.point += 5 user.lastvisit = current_time user.save() log = LoginLog() log.user = user log.ipaddr = get_ip_address(request) log.save() else: resp_dict['code'] = '30001' resp_dict['message'] = '用户名或密码错误' return Response(resp_dict)
def login(request): """用户登录""" if request.method == 'GET': return render(request, 'login.html') else: if request.session['captcha'] == request.POST['captcha'].lower(): username = request.POST['username'] password = request.POST['password'] try: user = User.objects.get(username=username) except User.DoesNotExist: hint = '用户名或密码错误' else: if user and user.password == to_md5_hex(password): request.session['userid'] = user.userid request.session['realname'] = user.realname delta = timezone.now() - user.lastvisit if delta.days >= 1: user.point += randint(1, 10) user.lastvisit = timezone.now() user.save() ipaddr = get_ip_address(request) log = LoginLog(user=user, ipaddr=ipaddr) log.save() return redirect('/') else: hint = '用户名或密码错误' else: hint = '请输入正确的图片验证码' return render(request, 'login.html', {'hint': hint})
def request_info(request): scheme = request.scheme body = request.body path = request.path method = request.method host = request.get_host() get = request.GET post = request.POST cookies = request.COOKIES http_x_forwarded_for, remote_addr, client_ip = utils.get_ip_address(request) remote_host = request.META.get("REMOTE_HOST", "") # meta = request.META headers = request.headers return render(request, "request_info.html", locals())
def login(request): """登录(获取用户身份令牌)""" username = request.data.get('username') password = request.data.get('password') if (check_tel(username) or check_email(username) or check_username(username)) and len(password) >= 6: password = to_md5_hex(password) q = Q(username=username, password=password) | \ Q(tel=username, password=password) | \ Q(email=username, password=password) user = User.objects.filter(q)\ .only('username', 'realname').first() if user: if user.status: # 用户登录成功通过JWT生成用户身份令牌 payload = { 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=1), 'data': { 'userid': user.userid, 'realname': user.realname, 'roleid': user.roles.get_queryset().first().roleid } } token = jwt.encode(payload, SECRET_KEY, algorithm='HS256').decode() with atomic(): current_time = timezone.now() if not user.lastvisit or \ (current_time - user.lastvisit).days >= 1: user.point += 2 user.lastvisit = current_time user.save() loginlog = LoginLog() loginlog.user = user loginlog.logdate = current_time loginlog.ipaddr = get_ip_address(request) loginlog.save() resp = DefaultResponse(*USER_LOGIN_SUCCESS, data={'token': token}) else: resp = DefaultResponse(*USER_LOGIN_STATUS) else: resp = DefaultResponse(*USER_LOGIN_FAILED) else: resp = DefaultResponse(*INVALID_LOGIN_INFO) return resp
def login(request): """登录(获取用户身份令牌)""" username = request.data.get('username') password = request.data.get('password') if username and password: password = to_md5_hex(password) user = User.objects.filter( Q(username=username, password=password) | Q(tel=username, password=password) | Q(email=username, password=password) ).first() if user: # roles = RoleSimpleSerializer(user.roles.all(), many=True).data # 用户登录成功通过JWT生成用户身份令牌 payload = { 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=1), 'data': {'userid': user.userid, } } token = jwt.encode(payload, SECRET_KEY, algorithm='HS256').encode() with atomic(): current_time = timezone.now() if not user.lastvisit or \ (current_time - user.lastvisit).days >= 1: user.point += 2 user.lastvisit = current_time user.save() loginlog = LoginLog() loginlog.user = user loginlog.logdate = current_time loginlog.ipaddr = get_ip_address(request) loginlog.save() resp = DefaultResponse(*USER_LOGIN_SUCCESS, data={'token': token}) else: resp = DefaultResponse(*USER_LOGIN_FAILED) else: resp = DefaultResponse(*INVALID_LOGIN_INFO) return resp