diff --git a/backend/dvadmin/system/views/login.py b/backend/dvadmin/system/views/login.py index 41f0a88..2aee9a1 100644 --- a/backend/dvadmin/system/views/login.py +++ b/backend/dvadmin/system/views/login.py @@ -56,7 +56,7 @@ class LoginSerializer(TokenObtainPairSerializer): 登录的序列化器: 重写djangorestframework-simplejwt的序列化器 """ - captcha = serializers.CharField(max_length=6) + captcha = serializers.CharField(max_length=6, required=False, allow_null=True) class Meta: model = Users @@ -67,21 +67,21 @@ class LoginSerializer(TokenObtainPairSerializer): 'no_active_account': _('账号/密码不正确') } - def validate_captcha(self, captcha): - self.image_code = CaptchaStore.objects.filter( - id=self.initial_data['captchaKey']).first() - five_minute_ago = datetime.now() - timedelta(hours=0, minutes=5, seconds=0) - if self.image_code and five_minute_ago > self.image_code.expiration: - self.image_code and self.image_code.delete() - raise CustomValidationError('验证码过期') - else: - if self.image_code and (self.image_code.response == captcha or self.image_code.challenge == captcha): - self.image_code and self.image_code.delete() - else: - self.image_code and self.image_code.delete() - raise CustomValidationError("图片验证码错误") - def validate(self, attrs): + if settings.CAPTCHA_STATE: + self.image_code = CaptchaStore.objects.filter( + id=self.initial_data['captchaKey']).first() + five_minute_ago = datetime.now() - timedelta(hours=0, minutes=5, seconds=0) + if self.image_code and five_minute_ago > self.image_code.expiration: + self.image_code and self.image_code.delete() + raise CustomValidationError('验证码过期') + else: + captcha = attrs['captcha'] + if self.image_code and (self.image_code.response == captcha or self.image_code.challenge == captcha): + self.image_code and self.image_code.delete() + else: + self.image_code and self.image_code.delete() + raise CustomValidationError("图片验证码错误") data = super().validate(attrs) data['name'] = self.user.name data['userId'] = self.user.id