From c934ccb10d657b62906924a1e6e1bd7ce6c36f10 Mon Sep 17 00:00:00 2001 From: Wenjie Zhang Date: Sun, 21 Sep 2025 14:31:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(auth):=20=E6=94=AF=E6=8C=81=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=91=98=E5=88=9D=E5=A7=8B=E5=8C=96=E6=97=B6=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E7=94=A8=E6=88=B7ID=E5=92=8C=E6=89=8B=E6=9C=BA?= =?UTF-8?q?=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/routers/auth_router.py | 33 ++++-- web/src/components/AgentChatComponent.vue | 2 +- .../components/UserManagementComponent.vue | 6 +- web/src/views/LoginView.vue | 100 ++++++++++++++---- 4 files changed, 108 insertions(+), 33 deletions(-) diff --git a/server/routers/auth_router.py b/server/routers/auth_router.py index 766711f8..7895a50e 100644 --- a/server/routers/auth_router.py +++ b/server/routers/auth_router.py @@ -1,4 +1,5 @@ from datetime import datetime +import re from fastapi import APIRouter, Depends, HTTPException, Request, status, UploadFile, File from fastapi.security import OAuth2PasswordRequestForm @@ -60,8 +61,9 @@ class UserResponse(BaseModel): class InitializeAdmin(BaseModel): - username: str + user_id: str # 直接输入用户ID password: str + phone_number: str | None = None class UsernameValidation(BaseModel): @@ -182,21 +184,34 @@ async def initialize_admin(admin_data: InitializeAdmin, db: Session = Depends(ge # 创建管理员账户 hashed_password = AuthUtils.hash_password(admin_data.password) - # 验证用户名 - is_valid, error_msg = validate_username(admin_data.username) - if not is_valid: + # 验证用户ID格式(只支持字母数字和下划线) + if not re.match(r'^[a-zA-Z0-9_]+$', admin_data.user_id): raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, - detail=error_msg, + detail="用户ID只能包含字母、数字和下划线", ) - # 生成user_id - existing_user_ids = [] - user_id = generate_unique_user_id(admin_data.username, existing_user_ids) + if len(admin_data.user_id) < 3 or len(admin_data.user_id) > 20: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="用户ID长度必须在3-20个字符之间", + ) + + # 验证手机号格式(如果提供了) + if admin_data.phone_number and not is_valid_phone_number(admin_data.phone_number): + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="手机号格式不正确" + ) + + # 由于是首次初始化,直接使用输入的user_id + user_id = admin_data.user_id new_admin = User( - username=admin_data.username, + username=admin_data.user_id, # username和user_id设置为相同值 user_id=user_id, + phone_number=admin_data.phone_number, + avatar=None, # 初始化时头像为空 password_hash=hashed_password, role="superadmin", last_login=datetime.now() diff --git a/web/src/components/AgentChatComponent.vue b/web/src/components/AgentChatComponent.vue index 3a99731d..45e8a1f7 100644 --- a/web/src/components/AgentChatComponent.vue +++ b/web/src/components/AgentChatComponent.vue @@ -222,7 +222,7 @@ const currentAgentId = computed(() => { }); const currentAgentMetadata = computed(() => { - if (agentStore?.metadata && CurrentAgentId.value in agentStore?.metadata[currentAgentId.value]) { + if (agentStore?.metadata && currentAgentId.value in agentStore?.metadata[currentAgentId.value]) { return agentStore?.metadata[currentAgentId.value] } return {} diff --git a/web/src/components/UserManagementComponent.vue b/web/src/components/UserManagementComponent.vue index 2aff4659..cf3b7b60 100644 --- a/web/src/components/UserManagementComponent.vue +++ b/web/src/components/UserManagementComponent.vue @@ -176,7 +176,7 @@