From 1fe912ac76d6c21eae41f030bd03f9c2721b5dd6 Mon Sep 17 00:00:00 2001 From: longda Date: Tue, 24 Mar 2026 23:16:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96:=20=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=9C=89=E6=95=88=E6=9C=9F7=E5=A4=A9/=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E8=BF=9E=E6=8E=A5=E6=B1=A0=E5=A2=9E=E5=BC=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/core/auth.py | 2 +- backend/app/core/database.py | 56 ++++++++++++++++++++++++++++++------ backend/app/routers/users.py | 10 ++++++- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/backend/app/core/auth.py b/backend/app/core/auth.py index 3702b7e..360b5bf 100644 --- a/backend/app/core/auth.py +++ b/backend/app/core/auth.py @@ -13,7 +13,7 @@ from app.models.models import User # 配置 SECRET_KEY = os.getenv("SECRET_KEY", "your-secret-key-change-in-production") ALGORITHM = "HS256" -ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "60")) +ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", "10080")) # 7天 # HTTP Bearer 认证 security = HTTPBearer(auto_error=False) diff --git a/backend/app/core/database.py b/backend/app/core/database.py index fffbbdd..2218c71 100644 --- a/backend/app/core/database.py +++ b/backend/app/core/database.py @@ -1,31 +1,69 @@ import os -from sqlalchemy import create_engine +import time +from sqlalchemy import create_engine, event from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker +from sqlalchemy.pool import QueuePool +from typing import Generator +import logging + +logger = logging.getLogger(__name__) DATABASE_URL = os.getenv( "DATABASE_URL", "postgresql://postgres:postgres@localhost:5432/zodiac" ) +# 增强版数据库引擎配置 engine = create_engine( DATABASE_URL, - pool_pre_ping=True, - pool_size=20, - max_overflow=40, - pool_recycle=3600, - pool_timeout=30, - echo=False + # 连接池配置 + poolclass=QueuePool, + pool_size=20, # 常规连接数 + max_overflow=40, # 允许超出的连接数(高并发时) + pool_timeout=30, # 获取连接超时时间(秒) + pool_recycle=1800, # 连接回收时间(30分钟),避免连接过期 + pool_pre_ping=True, # 每次获取连接前检查连接是否有效 + echo=False, + # 连接参数优化 + connect_args={ + "connect_timeout": 10, + "application_name": "zodiac-api", + "options": "-c statement_timeout=30000" # 查询超时30秒 + } ) +# 添加连接事件监听器 +@event.listens_for(engine, "connect") +def set_connect_timeout(dbapi_conn, connection_record): + """设置连接参数""" + cursor = dbapi_conn.cursor() + cursor.execute("SET statement_timeout = 30000") + cursor.close() + +@event.listens_for(engine, "checkout") +def check_connection(dbapi_conn, connection_record, connection_proxy): + """检出连接时检查""" + try: + cursor = dbapi_conn.cursor() + cursor.execute("SELECT 1") + cursor.close() + except Exception as e: + logger.warning(f"连接检查失败: {e}") + raise Exception("数据库连接无效") + SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() - -def get_db(): +def get_db() -> Generator: + """获取数据库会话,带错误处理""" db = SessionLocal(expire_on_commit=False) try: yield db + except Exception as e: + logger.error(f"数据库会话错误: {e}") + db.rollback() + raise finally: db.close() diff --git a/backend/app/routers/users.py b/backend/app/routers/users.py index 6928b85..1914635 100644 --- a/backend/app/routers/users.py +++ b/backend/app/routers/users.py @@ -104,7 +104,15 @@ def get_users( "role": u.role, "user_code": u.user_code, "created_at": u.f99_92_created_at.isoformat() if u.f99_92_created_at else None, - "collection_count": count + "collectionCount": count, + "level": u.f99_94_level, + "aiCount": u.f99_95_ai_count, + "searchCount": u.f99_96_search_count, + "loginCount": u.f99_98_login_count, + "points": u.f99_100_points, + "balance": float(u.f01_11_balance) if u.f01_11_balance else 0, + "totalAmount": float(u.f01_12_total_amount) if u.f01_12_total_amount else 0, + "phoneVerified": u.f01_06_phone_verified, }) return user_list