From 2b6b53b13d69863f68b7d9cb8e603932fab645e5 Mon Sep 17 00:00:00 2001 From: xingc Date: Tue, 6 May 2025 21:11:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(card):=20=E9=9D=9E=E4=BC=9A=E5=91=98?= =?UTF-8?q?=E9=99=90=E5=88=B6=E5=AD=98=E5=8D=A1=E6=95=B0=20=E9=9D=9E?= =?UTF-8?q?=E4=BC=9A=E5=91=98=E9=99=90=E5=88=B6=E5=AD=98=E5=8D=A1=E6=95=B0?= =?UTF-8?q?=E9=87=8F=EF=BC=8C=E9=BB=98=E8=AE=A418=E5=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 36 ++++++++++++++++++----------------- config.py | 3 +++ core/routers/card/routes.py | 9 +++++++++ core/routers/card/services.py | 16 +++++++++++++++- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/.env.example b/.env.example index c814d6c..1c46674 100644 --- a/.env.example +++ b/.env.example @@ -1,30 +1,32 @@ # 应用配置 -ALLOWED_ORIGINS = http://127.0.0.1:3000,http://localhost:3000 +ALLOWED_ORIGINS=http://127.0.0.1:8001,http://localhost:8001 # 身份认证 -ACCESS_SECRET_KEY = a2f718e8313bf8efcb45ef7364d040c45800f34d10f65776eef0f6584677ecd0 +ACCESS_SECRET_KEY=a2f718e8313bf8efcb45ef7364d040c45800f34d10f65776eef0f6584677ecd0 # 数据库配置 -DB_USER = card_book -DB_PASSWORD = DFCPjk4rdkca6SPE -DB_HOST = 192.168.3.4 -DB_NAME = card_book +DB_USER=card_book +DB_PASSWORD=DFCPjk4rdkca6SPE +DB_HOST=192.168.3.4 +DB_NAME=card_book # redis配置 -REDIS_PASSWORD = xingc -REDIS_HOST = 192.168.3.4 -REDIS_PORT = 6379 -REDIS_DB = 0 +REDIS_PASSWORD=xingc +REDIS_HOST=192.168.3.4 +REDIS_PORT=6379 +REDIS_DB=0 # 文件上传配置 -ASSETS_BASE_URL = http://127.0.0.1:8001 +ASSETS_BASE_URL =http://127.0.0.1:8001 # 代理 -OVERSEAS_PROXY = http://127.0.0.1:7890 +OVERSEAS_PROXY=http://127.0.0.1:7890 # 微信 -APPID = wx3d30771f3987cc92 -APP_SECRET = 8e57b1b5344d1dbd058cd45f3b2c4d3c -PAY_MCH_ID = -PAY_API_KEY = -PAY_NOTIFY_URL = \ No newline at end of file +APPID=wx3d30771f3987cc92 +APP_SECRET=8e57b1b5344d1dbd058cd45f3b2c4d3c +PAY_MCH_ID= +PAY_API_KEY= +PAY_NOTIFY_URL= + +USER_MAX_CREATED_CARD_NUM=18 \ No newline at end of file diff --git a/config.py b/config.py index 657c184..cb253b6 100644 --- a/config.py +++ b/config.py @@ -66,6 +66,9 @@ class Settings(BaseSettings): PAY_API_KEY: str = '' PAY_NOTIFY_URL: str = '' + # 会员配置 + USER_MAX_CREATED_CARD_NUM: int = 18 + model_config = SettingsConfigDict(env_file=f'.env.{ENVIRONMENT}', env_file_encoding='utf-8') diff --git a/core/routers/card/routes.py b/core/routers/card/routes.py index 53776e2..49455d3 100644 --- a/core/routers/card/routes.py +++ b/core/routers/card/routes.py @@ -24,6 +24,7 @@ from core.utils.helper import generate_keyname, check_picture_for_empty from . import schema from .scraper import card_scraper +from .services import check_user_card_created_permission logger = logging.getLogger(__name__) card_router = APIRouter(prefix='/card', tags=['卡册模块']) @@ -190,6 +191,14 @@ async def created_card( db_session: AsyncSession = Depends(get_async_session) ): card_crud = CRUD(db_session, Card) + # 确认用户存卡权限 + if await check_user_card_created_permission( + db_session, login_user=login_user + ) is False: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, detail=f'非会员仅支持存入{settings.USER_MAX_CREATED_CARD_NUM}张卡片' + ) + # 卡片库查询 card: Card = await card_crud.get(filters={'cert_number': payload.cert_number}) if payload.source.value != 'other': diff --git a/core/routers/card/services.py b/core/routers/card/services.py index 05610b0..10187f2 100644 --- a/core/routers/card/services.py +++ b/core/routers/card/services.py @@ -7,7 +7,11 @@ from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession -from core.db.models import SearchCardCache +from core.db.crud import CRUD +from core.db.models import SearchCardCache, User, UserCollection +from core.routers.auth.schema import LoginUser + +from config import settings async def get_cache_card_by_number(db_session: AsyncSession, *, cert_number: str) -> SearchCardCache | None: @@ -16,3 +20,13 @@ async def get_cache_card_by_number(db_session: AsyncSession, *, cert_number: str card: SearchCardCache | None = result.scalar_one_or_none() return card + + +async def check_user_card_created_permission(db_session: AsyncSession, *, login_user: LoginUser) -> bool: + db_crud = CRUD(db_session, User) + user = await db_crud.get(filters={'user_id': login_user.user_id}) + if user.is_vip(): + return True + + total = await db_crud.total(model=UserCollection, filters={'user_id': login_user.user_id}) + return settings.USER_MAX_CREATED_CARD_NUM > total