Files
chunyu_project/user/views/captcha.py
T
2026-08-05 23:59:15 +08:00

51 lines
1.8 KiB
Python

from rest_framework.permissions import AllowAny
from rest_framework.views import APIView
from rest_framework import status
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from chunyu_project.common_schemas import success_response, error_response, unauthorized_response, not_found_response
from utils.captcha import generate_captcha
from utils.response_codes import ResponseCode, create_standardized_response
class CaptchaAPIView(APIView):
permission_classes = [AllowAny]
@swagger_auto_schema(
tags=['验证码'],
operation_summary='生成图形验证码',
operation_description='生成新的图形验证码,返回验证码 key 和 base64 图片',
responses={200: success_response, 500: error_response},
)
def get(self, request):
captcha_key, captcha_image = generate_captcha()
return create_standardized_response(
data={
'captcha_key': captcha_key,
'captcha_image': captcha_image,
},
code=ResponseCode.SUCCESS,
status_code=status.HTTP_200_OK
)
class ImageCaptchaAPIView(APIView):
permission_classes = [AllowAny]
@swagger_auto_schema(
tags=['验证码'],
operation_summary='生成图片验证码',
operation_description='生成新的图片验证码,返回验证码 key 和 base64 图片',
responses={200: success_response, 500: error_response},
)
def get(self, request):
captcha_key, captcha_image = generate_captcha()
return create_standardized_response(
data={
'captcha_key': captcha_key,
'captcha_image': captcha_image,
},
code=ResponseCode.SUCCESS,
status_code=status.HTTP_200_OK
)