From d53a114dbaa71162c406c68c1419a9b468549ddd Mon Sep 17 00:00:00 2001 From: zhinianboke <115088296+zhinianboke@users.noreply.github.com> Date: Tue, 5 Aug 2025 15:03:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=98=AF=E5=90=A6=E5=BC=80?= =?UTF-8?q?=E5=90=AF=E6=B3=A8=E5=86=8C=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db_manager.py | 3 +- reply_server.py | 113 ++++++++++++++++++++++++++++++++++++++++++++-- static/index.html | 35 ++++++++++++++ static/js/app.js | 113 +++++++++++++++++++++++++++++++++++++++++++++- static/login.html | 30 +++++++++++- 5 files changed, 287 insertions(+), 7 deletions(-) diff --git a/db_manager.py b/db_manager.py index 85b45f4..5e648f4 100644 --- a/db_manager.py +++ b/db_manager.py @@ -342,7 +342,8 @@ class DBManager: # 插入默认系统设置(不包括管理员密码,由reply_server.py初始化) cursor.execute(''' INSERT OR IGNORE INTO system_settings (key, value, description) VALUES - ('theme_color', 'blue', '主题颜色') + ('theme_color', 'blue', '主题颜色'), + ('registration_enabled', 'true', '是否开启用户注册') ''') # 检查并升级数据库 diff --git a/reply_server.py b/reply_server.py index b6b64b6..eac4295 100644 --- a/reply_server.py +++ b/reply_server.py @@ -85,6 +85,8 @@ class LoginResponse(BaseModel): token: Optional[str] = None message: str user_id: Optional[int] = None + username: Optional[str] = None + is_admin: Optional[bool] = None class ChangePasswordRequest(BaseModel): @@ -397,6 +399,33 @@ async def login_page(): # 注册页面路由 @app.get('/register.html', response_class=HTMLResponse) async def register_page(): + # 检查注册是否开启 + from db_manager import db_manager + registration_enabled = db_manager.get_system_setting('registration_enabled') + if registration_enabled != 'true': + return HTMLResponse(''' + + + + 注册已关闭 + + + + +

🚫 注册功能已关闭

+

系统管理员已关闭用户注册功能

+ + + + ''', status_code=403) + register_path = os.path.join(static_dir, 'register.html') if os.path.exists(register_path): with open(register_path, 'r', encoding='utf-8') as f: @@ -491,7 +520,9 @@ async def login(request: LoginRequest): success=True, token=token, message="登录成功", - user_id=user['id'] + user_id=user['id'], + username=user['username'], + is_admin=(user['username'] == ADMIN_USERNAME) ) logger.warning(f"【{request.username}】登录失败:用户名或密码错误") @@ -520,7 +551,9 @@ async def login(request: LoginRequest): success=True, token=token, message="登录成功", - user_id=user['id'] + user_id=user['id'], + username=user['username'], + is_admin=(user['username'] == ADMIN_USERNAME) ) logger.warning(f"【{request.email}】邮箱登录失败:邮箱或密码错误") @@ -564,7 +597,9 @@ async def login(request: LoginRequest): success=True, token=token, message="登录成功", - user_id=user['id'] + user_id=user['id'], + username=user['username'], + is_admin=(user['username'] == ADMIN_USERNAME) ) else: @@ -581,7 +616,8 @@ async def verify(user_info: Optional[Dict[str, Any]] = Depends(verify_token)): return { "authenticated": True, "user_id": user_info['user_id'], - "username": user_info['username'] + "username": user_info['username'], + "is_admin": user_info['username'] == ADMIN_USERNAME } return {"authenticated": False} @@ -759,6 +795,15 @@ async def send_verification_code(request: SendCodeRequest): async def register(request: RegisterRequest): from db_manager import db_manager + # 检查注册是否开启 + registration_enabled = db_manager.get_system_setting('registration_enabled') + if registration_enabled != 'true': + logger.warning(f"【{request.username}】注册失败: 注册功能已关闭") + return RegisterResponse( + success=False, + message="注册功能已关闭,请联系管理员" + ) + try: logger.info(f"【{request.username}】尝试注册,邮箱: {request.email}") @@ -1420,6 +1465,66 @@ def update_system_setting(key: str, setting_data: SystemSettingIn, _: None = Dep raise HTTPException(status_code=500, detail=str(e)) +# ------------------------- 注册设置接口 ------------------------- + +@app.get('/registration-status') +def get_registration_status(): + """获取注册开关状态(公开接口,无需认证)""" + from db_manager import db_manager + try: + enabled_str = db_manager.get_system_setting('registration_enabled') + logger.info(f"从数据库获取的注册设置值: '{enabled_str}'") # 调试信息 + + # 如果设置不存在,默认为开启 + if enabled_str is None: + enabled_bool = True + message = '注册功能已开启' + else: + enabled_bool = enabled_str == 'true' + message = '注册功能已开启' if enabled_bool else '注册功能已关闭' + + logger.info(f"解析后的注册状态: enabled={enabled_bool}, message='{message}'") # 调试信息 + + return { + 'enabled': enabled_bool, + 'message': message + } + except Exception as e: + logger.error(f"获取注册状态失败: {e}") + return {'enabled': True, 'message': '注册功能已开启'} # 出错时默认开启 + + +class RegistrationSettingUpdate(BaseModel): + enabled: bool + + +@app.put('/registration-settings') +def update_registration_settings(setting_data: RegistrationSettingUpdate, admin_user: Dict[str, Any] = Depends(require_admin)): + """更新注册开关设置(仅管理员)""" + from db_manager import db_manager + try: + enabled = setting_data.enabled + success = db_manager.set_system_setting( + 'registration_enabled', + 'true' if enabled else 'false', + '是否开启用户注册' + ) + if success: + log_with_user('info', f"更新注册设置: {'开启' if enabled else '关闭'}", admin_user) + return { + 'success': True, + 'enabled': enabled, + 'message': f"注册功能已{'开启' if enabled else '关闭'}" + } + else: + raise HTTPException(status_code=500, detail='更新注册设置失败') + except HTTPException: + raise + except Exception as e: + logger.error(f"更新注册设置失败: {e}") + raise HTTPException(status_code=500, detail=str(e)) + + diff --git a/static/index.html b/static/index.html index 1e27179..ca12012 100644 --- a/static/index.html +++ b/static/index.html @@ -983,6 +983,41 @@ + + +