style: 使用ruff格式化验证码模块,优化导入
This commit is contained in:
@@ -1,28 +1,41 @@
|
|||||||
import uuid
|
import uuid
|
||||||
|
from datetime import timedelta
|
||||||
from fastapi import APIRouter, Request
|
from fastapi import APIRouter, Request
|
||||||
from config.env import RedisInitKeyConfig
|
from config.env import RedisInitKeyConfig
|
||||||
from module_admin.service.captcha_service import *
|
|
||||||
from module_admin.entity.vo.login_vo import CaptchaCode
|
from module_admin.entity.vo.login_vo import CaptchaCode
|
||||||
from utils.response_util import *
|
from module_admin.service.captcha_service import CaptchaService
|
||||||
from utils.log_util import *
|
from utils.response_util import ResponseUtil
|
||||||
from datetime import timedelta
|
from utils.log_util import logger
|
||||||
|
|
||||||
|
|
||||||
captchaController = APIRouter()
|
captchaController = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@captchaController.get("/captchaImage")
|
@captchaController.get('/captchaImage')
|
||||||
async def get_captcha_image(request: Request):
|
async def get_captcha_image(request: Request):
|
||||||
captcha_enabled = True if await request.app.state.redis.get(f"{RedisInitKeyConfig.SYS_CONFIG.get('key')}:sys.account.captchaEnabled") == 'true' else False
|
captcha_enabled = (
|
||||||
register_enabled = True if await request.app.state.redis.get(
|
True
|
||||||
f"{RedisInitKeyConfig.SYS_CONFIG.get('key')}:sys.account.registerUser") == 'true' else False
|
if await request.app.state.redis.get(f"{RedisInitKeyConfig.SYS_CONFIG.get('key')}:sys.account.captchaEnabled")
|
||||||
|
== 'true'
|
||||||
|
else False
|
||||||
|
)
|
||||||
|
register_enabled = (
|
||||||
|
True
|
||||||
|
if await request.app.state.redis.get(f"{RedisInitKeyConfig.SYS_CONFIG.get('key')}:sys.account.registerUser")
|
||||||
|
== 'true'
|
||||||
|
else False
|
||||||
|
)
|
||||||
session_id = str(uuid.uuid4())
|
session_id = str(uuid.uuid4())
|
||||||
captcha_result = await CaptchaService.create_captcha_image_service()
|
captcha_result = await CaptchaService.create_captcha_image_service()
|
||||||
image = captcha_result[0]
|
image = captcha_result[0]
|
||||||
computed_result = captcha_result[1]
|
computed_result = captcha_result[1]
|
||||||
await request.app.state.redis.set(f"{RedisInitKeyConfig.CAPTCHA_CODES.get('key')}:{session_id}", computed_result, ex=timedelta(minutes=2))
|
await request.app.state.redis.set(
|
||||||
|
f"{RedisInitKeyConfig.CAPTCHA_CODES.get('key')}:{session_id}", computed_result, ex=timedelta(minutes=2)
|
||||||
|
)
|
||||||
logger.info(f'编号为{session_id}的会话获取图片验证码成功')
|
logger.info(f'编号为{session_id}的会话获取图片验证码成功')
|
||||||
|
|
||||||
return ResponseUtil.success(
|
return ResponseUtil.success(
|
||||||
model_content=CaptchaCode(captchaEnabled=captcha_enabled, registerEnabled=register_enabled, img=image, uuid=session_id)
|
model_content=CaptchaCode(
|
||||||
|
captchaEnabled=captcha_enabled, registerEnabled=register_enabled, img=image, uuid=session_id
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
from PIL import Image, ImageDraw, ImageFont
|
import base64
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import base64
|
from PIL import Image, ImageDraw, ImageFont
|
||||||
|
|
||||||
|
|
||||||
class CaptchaService:
|
class CaptchaService:
|
||||||
@@ -35,7 +35,7 @@ class CaptchaService:
|
|||||||
else:
|
else:
|
||||||
result = num1 * num2
|
result = num1 * num2
|
||||||
# 绘制文本
|
# 绘制文本
|
||||||
text = f"{num1} {operational_character} {num2} = ?"
|
text = f'{num1} {operational_character} {num2} = ?'
|
||||||
draw.text((25, 15), text, fill='blue', font=font)
|
draw.text((25, 15), text, fill='blue', font=font)
|
||||||
|
|
||||||
# 将图像数据保存到内存中
|
# 将图像数据保存到内存中
|
||||||
|
Reference in New Issue
Block a user