fix(auth): 添加验证码生成异常处理
捕获验证码生成过程中的异常并抛出HTTP异常,提高系统健壮性
This commit is contained in:
parent
c98adab6ce
commit
b9150c3e29
Binary file not shown.
@ -5,6 +5,7 @@ from utils.redis_client import get_redis, set_token_in_redis
|
||||
import redis.asyncio as redis
|
||||
import random
|
||||
import string
|
||||
from fastapi import HTTPException
|
||||
|
||||
|
||||
def generate_captcha(length: int = 4) -> tuple[str, bytes]:
|
||||
@ -13,16 +14,19 @@ def generate_captcha(length: int = 4) -> tuple[str, bytes]:
|
||||
characters = string.digits + string.ascii_uppercase
|
||||
captcha_text = ''.join(random.choices(characters, k=length))
|
||||
|
||||
# 生成验证码图片
|
||||
image = ImageCaptcha(width=150, height=50)
|
||||
image_bytes = image.generate(captcha_text)
|
||||
try:
|
||||
# 生成验证码图片
|
||||
image = ImageCaptcha(width=150, height=50)
|
||||
image_bytes = image.generate(captcha_text)
|
||||
|
||||
# 将图片转换为字节流
|
||||
buffer = BytesIO()
|
||||
buffer.write(image_bytes.read())
|
||||
buffer.seek(0)
|
||||
# 将图片转换为字节流
|
||||
buffer = BytesIO()
|
||||
buffer.write(image_bytes.read())
|
||||
buffer.seek(0)
|
||||
|
||||
return captcha_text, buffer.getvalue()
|
||||
return captcha_text, buffer.getvalue()
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail="验证码生成失败")
|
||||
|
||||
|
||||
async def save_captcha_to_redis(redis_conn: redis.Redis, captcha_id: str, captcha_text: str, expire_seconds: int = 300):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user