功能变化:

后端可关闭验证码的验证
pull/49/head
猿小天 2022-04-15 17:24:03 +08:00
parent 58491d0fef
commit 20f88bda87
1 changed files with 15 additions and 15 deletions

View File

@ -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