From 04fb72c14cbd247ef8511cb21f9c3269b0ecfe86 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 12 Jul 2025 18:55:05 +0800 Subject: [PATCH] fix --- python_core/cli/commands/auth.py | 56 +++++---------------------- python_core/utils/jsonrpc_enhanced.py | 9 ++++- src/services/pythonCliAuth.ts | 4 +- 3 files changed, 17 insertions(+), 52 deletions(-) diff --git a/python_core/cli/commands/auth.py b/python_core/cli/commands/auth.py index 40fa125..630f684 100644 --- a/python_core/cli/commands/auth.py +++ b/python_core/cli/commands/auth.py @@ -26,12 +26,11 @@ def register_user( password: Optional[str] = typer.Option(None, "--password", "-p", help="密码(不提供则交互式输入)") ): """注册新用户""" - + response = create_response_handler(str(uuid4())) try: - response = create_response_handler(uuid4()) # 获取密码 if not password: - response.error("❌ 密码不能为空") + response.error(-32602, "❌ 密码不能为空") raise typer.Exit(1) # 执行注册 @@ -43,68 +42,31 @@ def register_user( }) response.success(result) except Exception as e: - response.error(f"❌ 注册失败: {str(e)}") + response.error(-32603, f"❌ 注册失败: {str(e)}") raise typer.Exit(1) @auth_app.command("login") def login_user( username_or_email: str = typer.Argument(..., help="用户名或邮箱"), - password: Optional[str] = typer.Option(None, "--password", "-p", help="密码(不提供则交互式输入)"), - verbose: bool = typer.Option(False, "--verbose", "-v", help="详细输出"), - json_output: bool = typer.Option(False, "--json", help="JSON格式输出") + password: Optional[str] = typer.Option(None, "--password", "-p", help="密码(不提供则交互式输入)") ): """用户登录""" - + response = create_response_handler(str(uuid4())) try: - console.print(f"🔐 [bold blue]用户登录[/bold blue]") - console.print(f"用户名/邮箱: {username_or_email}") - # 获取密码 if not password: - password = getpass.getpass("请输入密码: ") - if not password: - console.print("[red]❌ 密码不能为空[/red]") - raise typer.Exit(1) + response.error(-32602, "❌ 密码不能为空") + raise typer.Exit(1) # 执行登录 result = auth_api.login({ "username_or_email": username_or_email, "password": password }) - - if result["success"]: - if json_output: - # JSON格式输出 - print(json.dumps(result, ensure_ascii=False, indent=2)) - else: - console.print(f"\n✅ [bold green]登录成功![/bold green]") - console.print(f"欢迎回来,{result['data']['user']['display_name']}!") - console.print(f"最后登录: {result['data']['user']['last_login']}") - - if verbose: - # 显示详细信息 - user_info = f""" -🆔 用户ID: {result['data']['user']['id']} -👤 用户名: {result['data']['user']['username']} -📧 邮箱: {result['data']['user']['email']} -🏷️ 显示名称: {result['data']['user']['display_name']} -🕒 最后登录: {result['data']['user']['last_login']} -🔑 Token: {result['data']['token'][:50]}... -⏰ 过期时间: {result['data']['expires_at']} - """ - - panel = Panel(user_info.strip(), title="登录信息", border_style="green") - console.print(panel) - else: - if json_output: - print(json.dumps(result, ensure_ascii=False, indent=2)) - else: - console.print(f"[red]❌ 登录失败: {result['message']}[/red]") - raise typer.Exit(1) - + response.success(result) except Exception as e: - console.print(f"[red]❌ 登录失败: {str(e)}[/red]") + response.error(-32603, f"❌ 注册失败: {str(e)}") raise typer.Exit(1) diff --git a/python_core/utils/jsonrpc_enhanced.py b/python_core/utils/jsonrpc_enhanced.py index 85689ca..7344505 100644 --- a/python_core/utils/jsonrpc_enhanced.py +++ b/python_core/utils/jsonrpc_enhanced.py @@ -59,8 +59,13 @@ class EnhancedJSONRPCResponse: def error(self, code: int, message: str, data: Any = None) -> None: """发送错误响应""" - error = JSONRPCError(code=code, message=message, data=data) - response = JSONRPC20Response(error=error, _id=self.request_id) + error_obj = { + "code": code, + "message": message + } + if data is not None: + error_obj["data"] = data + response = JSONRPC20Response(error=error_obj, _id=self.request_id) self._send_response(response.data) def progress(self, step: str, progress: int, message: str, diff --git a/src/services/pythonCliAuth.ts b/src/services/pythonCliAuth.ts index f61c300..2d4b9aa 100644 --- a/src/services/pythonCliAuth.ts +++ b/src/services/pythonCliAuth.ts @@ -77,8 +77,7 @@ class PythonCliAuth { const response: PythonCliAuthResponse = await invoke('python_cli_auth_login', { request: { username_or_email: username_or_email, - password: credentials.password, - json_output: true + password: credentials.password } }); @@ -138,7 +137,6 @@ class PythonCliAuth { email: credentials.email, password: credentials.password, display_name: credentials.displayName, - } }); console.log({ response })