BookSystem/backend/auth/config.py
jayhgq 81972f1328 feat(auth): 添加手机短信和邮箱注册功能
本次提交完成了用户认证模块的注册扩展:
1. 新增阿里云短信和异步邮件发送工具类
2. 新增短信/邮箱相关配置类与环境变量支持
3. 添加短信验证码、待注册数据的Redis缓存工具方法
4. 扩展用户登录逻辑,支持用户名/邮箱/手机号多方式登录
5. 实现手机短信注册和邮箱验证注册完整流程
6. 更新权限初始化数据与系统配置项
7. 补充相关Pydantic请求响应模型
8. 新增依赖包并完善requirements.txt
2026-06-14 22:19:33 +08:00

93 lines
2.1 KiB
Python

from pydantic_settings import BaseSettings
from typing import Optional
from urllib.parse import quote_plus
class PostgreSQLSettings(BaseSettings):
"""PostgreSQL数据库配置"""
host: str = "postgres"
port: int = 5432
user: str = "admin"
password: str = "PostgresAdmin@123"
database: str = "booksystem"
min_size: int = 5
max_size: int = 20
@property
def url(self) -> str:
"""生成数据库连接URL"""
encoded_password = quote_plus(self.password)
return f"postgresql+asyncpg://{self.user}:{encoded_password}@{self.host}:{self.port}/{self.database}"
class Config:
env_prefix = "POSTGRES_"
class RedisSettings(BaseSettings):
"""Redis配置"""
host: str = "redis"
port: int = 6379
password: Optional[str] = "RedisAdmin@123"
db: int = 0
@property
def url(self) -> str:
"""生成Redis连接URL"""
if self.password:
return f"redis://:{self.password}@{self.host}:{self.port}/{self.db}"
return f"redis://{self.host}:{self.port}/{self.db}"
class Config:
env_prefix = "REDIS_"
class AppSettings(BaseSettings):
"""应用配置"""
name: str = "Book System API"
version: str = "1.0.0"
debug: bool = False
secret_key: str = "BookSystemAPIQRAdmin123"
algorithm: str = "HS256"
access_token_expire_minutes: int = 10080
class Config:
env_prefix = "APP_"
class AliyunSMSSettings(BaseSettings):
"""阿里云短信配置"""
access_key: str = ""
access_secret: str = ""
sign_name: str = ""
template_code: str = ""
class Config:
env_prefix = "ALIYUN_SMS_"
class EmailSettings(BaseSettings):
"""邮件发送配置"""
smtp_host: str = ""
smtp_port: int = 465
smtp_user: str = ""
smtp_password: str = ""
use_tls: bool = True
from_addr: str = ""
base_url: str = "http://localhost:8000"
class Config:
env_prefix = "EMAIL_"
# 创建设置实例
postgres_settings = PostgreSQLSettings()
redis_settings = RedisSettings()
app_settings = AppSettings()
aliyun_sms_settings = AliyunSMSSettings()
email_settings = EmailSettings()