From 5f915c61b6a38fe78a5b470fe9bc8aec17e9528e Mon Sep 17 00:00:00 2001 From: moyin <2443444649@qq.com> Date: Fri, 2 Jan 2026 21:18:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor=EF=BC=9AAuthScene=E8=A7=A3=E8=80=A6?= =?UTF-8?q?=E9=87=8D=E6=9E=84=EF=BC=8C=E5=AE=9E=E7=8E=B0=E8=A7=86=E5=9B=BE?= =?UTF-8?q?=E4=B8=8E=E4=B8=9A=E5=8A=A1=E9=80=BB=E8=BE=91=E5=88=86=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建AuthManager.gd:负责所有认证业务逻辑 - 用户登录/注册逻辑 - 表单验证逻辑 - 验证码管理逻辑 - 网络请求管理 - 创建ToastManager.gd:负责Toast消息管理 - Toast创建和显示 - 动画和生命周期管理 - 支持成功/失败样式 - 重构AuthScene.gd:纯视图层实现 - 只负责UI交互和显示 - 通过信号与业务层通信 - 移除所有业务逻辑代码 - 修复GDScript警告: - 未使用参数添加下划线前缀 - 修复变量名与基类方法冲突 - 修复EventSystem中的try语法错误 - 修复AuthManager中的方法名不匹配错误 符合docs中的架构要求,实现完全解耦 --- _Core/managers/AuthManager.gd | 589 +++++++++++++++ _Core/managers/AuthManager.gd.uid | 1 + _Core/managers/ToastManager.gd | 234 ++++++ _Core/managers/ToastManager.gd.uid | 1 + scenes/ui/AuthScene.gd | 1135 +++++++++------------------- 5 files changed, 1181 insertions(+), 779 deletions(-) create mode 100644 _Core/managers/AuthManager.gd create mode 100644 _Core/managers/AuthManager.gd.uid create mode 100644 _Core/managers/ToastManager.gd create mode 100644 _Core/managers/ToastManager.gd.uid diff --git a/_Core/managers/AuthManager.gd b/_Core/managers/AuthManager.gd new file mode 100644 index 0000000..2f54ddb --- /dev/null +++ b/_Core/managers/AuthManager.gd @@ -0,0 +1,589 @@ +class_name AuthManager + +# ============================================================================ +# AuthManager.gd - 认证管理器 +# ============================================================================ +# 认证系统的业务逻辑管理器,负责处理所有认证相关的业务逻辑 +# +# 核心职责: +# - 用户登录业务逻辑(密码登录 + 验证码登录) +# - 用户注册业务逻辑 +# - 表单验证逻辑 +# - 验证码管理逻辑 +# - 网络请求管理 +# - 响应处理和状态管理 +# +# 使用方式: +# var auth_manager = AuthManager.new() +# auth_manager.login_success.connect(_on_login_success) +# auth_manager.execute_password_login(username, password) +# +# 注意事项: +# - 这是业务逻辑层,不包含任何UI相关代码 +# - 通过信号与UI层通信 +# - 所有验证逻辑都在这里实现 +# ============================================================================ + +extends RefCounted + +# ============ 信号定义 ============ + +# 登录成功信号 +signal login_success(username: String) + +# 登录失败信号 +signal login_failed(message: String) + +# 注册成功信号 +signal register_success(message: String) + +# 注册失败信号 +signal register_failed(message: String) + +# 验证码发送成功信号 +signal verification_code_sent(message: String) + +# 验证码发送失败信号 +signal verification_code_failed(message: String) + +# 表单验证失败信号 +signal form_validation_failed(field: String, message: String) + +# 网络状态变化信号 +signal network_status_changed(is_connected: bool, message: String) + +# 按钮状态变化信号 +signal button_state_changed(button_name: String, is_loading: bool, text: String) + +# Toast消息信号 +signal show_toast_message(message: String, is_success: bool) + +# ============ 枚举定义 ============ + +# 登录模式枚举 +enum LoginMode { + PASSWORD, # 密码登录模式 + VERIFICATION # 验证码登录模式 +} + +# ============ 成员变量 ============ + +# 登录状态 +var current_login_mode: LoginMode = LoginMode.PASSWORD +var is_processing: bool = false + +# 验证码管理 +var verification_codes_sent: Dictionary = {} +var code_cooldown: float = 60.0 +var current_email: String = "" + +# 网络请求管理 +var active_request_ids: Array = [] + +# ============ 生命周期方法 ============ + +# 初始化管理器 +func _init(): + print("AuthManager 初始化完成") + +# 清理资源 +func cleanup(): + # 取消所有活动的网络请求 + for request_id in active_request_ids: + NetworkManager.cancel_request(request_id) + active_request_ids.clear() + +# ============ 登录相关方法 ============ + +# 执行密码登录 +# +# 参数: +# username: String - 用户名/邮箱 +# password: String - 密码 +# +# 功能: +# - 验证输入参数 +# - 发送登录请求 +# - 处理响应结果 +func execute_password_login(username: String, password: String): + if is_processing: + show_toast_message.emit("请等待当前操作完成", false) + return + + # 验证输入 + var validation_result = validate_login_inputs(username, password) + if not validation_result.valid: + form_validation_failed.emit(validation_result.field, validation_result.message) + return + + # 设置处理状态 + is_processing = true + button_state_changed.emit("main_btn", true, "登录中...") + show_toast_message.emit("正在验证登录信息...", true) + + # 发送网络请求 + var request_id = NetworkManager.login(username, password, _on_login_response) + if request_id != "": + active_request_ids.append(request_id) + else: + _reset_login_state() + show_toast_message.emit("网络请求失败", false) + +# 执行验证码登录 +# +# 参数: +# identifier: String - 用户标识符 +# verification_code: String - 验证码 +func execute_verification_login(identifier: String, verification_code: String): + if is_processing: + show_toast_message.emit("请等待当前操作完成", false) + return + + # 验证输入 + if identifier.is_empty(): + form_validation_failed.emit("username", "请输入用户名/手机/邮箱") + return + + if verification_code.is_empty(): + form_validation_failed.emit("verification", "请输入验证码") + return + + # 设置处理状态 + is_processing = true + button_state_changed.emit("main_btn", true, "登录中...") + show_toast_message.emit("正在验证验证码...", true) + + # 发送网络请求 + var request_id = NetworkManager.verification_code_login(identifier, verification_code, _on_verification_login_response) + if request_id != "": + active_request_ids.append(request_id) + else: + _reset_login_state() + show_toast_message.emit("网络请求失败", false) + +# 切换登录模式 +func toggle_login_mode(): + if current_login_mode == LoginMode.PASSWORD: + current_login_mode = LoginMode.VERIFICATION + else: + current_login_mode = LoginMode.PASSWORD + +# 获取当前登录模式 +func get_current_login_mode() -> LoginMode: + return current_login_mode + +# ============ 注册相关方法 ============ + +# 执行用户注册 +# +# 参数: +# username: String - 用户名 +# email: String - 邮箱 +# password: String - 密码 +# confirm_password: String - 确认密码 +# verification_code: String - 邮箱验证码 +func execute_register(username: String, email: String, password: String, confirm_password: String, verification_code: String): + if is_processing: + show_toast_message.emit("请等待当前操作完成", false) + return + + # 验证注册表单 + var validation_result = validate_register_form(username, email, password, confirm_password, verification_code) + if not validation_result.valid: + form_validation_failed.emit(validation_result.field, validation_result.message) + return + + # 设置处理状态 + is_processing = true + button_state_changed.emit("register_btn", true, "注册中...") + show_toast_message.emit("正在创建账户...", true) + + # 发送注册请求 + var request_id = NetworkManager.register(username, password, username, email, verification_code, _on_register_response) + if request_id != "": + active_request_ids.append(request_id) + else: + _reset_register_state() + show_toast_message.emit("网络请求失败", false) + +# ============ 验证码相关方法 ============ + +# 发送邮箱验证码 +# +# 参数: +# email: String - 邮箱地址 +func send_email_verification_code(email: String): + # 验证邮箱格式 + var email_validation = validate_email(email) + if not email_validation.valid: + form_validation_failed.emit("email", email_validation.message) + return + + # 检查冷却时间 + if not _can_send_verification_code(email): + var remaining = get_remaining_cooldown_time(email) + show_toast_message.emit("该邮箱请等待 %d 秒后再次发送" % remaining, false) + return + + # 记录发送状态 + _record_verification_code_sent(email) + + # 发送请求 + var request_id = NetworkManager.send_email_verification(email, _on_send_code_response) + if request_id != "": + active_request_ids.append(request_id) + else: + _reset_verification_code_state(email) + show_toast_message.emit("网络请求失败", false) + +# 发送登录验证码 +# +# 参数: +# identifier: String - 用户标识符 +func send_login_verification_code(identifier: String): + if identifier.is_empty(): + form_validation_failed.emit("username", "请先输入用户名/手机/邮箱") + return + + button_state_changed.emit("get_code_btn", true, "发送中...") + show_toast_message.emit("正在发送登录验证码...", true) + + var request_id = NetworkManager.send_login_verification_code(identifier, _on_send_login_code_response) + if request_id != "": + active_request_ids.append(request_id) + else: + button_state_changed.emit("get_code_btn", false, "获取验证码") + show_toast_message.emit("网络请求失败", false) + +# 发送密码重置验证码 +# +# 参数: +# identifier: String - 用户标识符 +func send_password_reset_code(identifier: String): + if identifier.is_empty(): + show_toast_message.emit("请先输入邮箱或手机号", false) + return + + if not _is_valid_identifier(identifier): + show_toast_message.emit("请输入有效的邮箱或手机号", false) + return + + button_state_changed.emit("forgot_password_btn", true, "发送中...") + show_toast_message.emit("正在发送密码重置验证码...", true) + + var request_id = NetworkManager.forgot_password(identifier, _on_forgot_password_response) + if request_id != "": + active_request_ids.append(request_id) + else: + button_state_changed.emit("forgot_password_btn", false, "忘记密码") + show_toast_message.emit("网络请求失败", false) + +# ============ 验证方法 ============ + +# 验证登录输入 +func validate_login_inputs(username: String, password: String) -> Dictionary: + var result = {"valid": false, "field": "", "message": ""} + + if username.is_empty(): + result.field = "username" + result.message = "用户名不能为空" + return result + + if password.is_empty(): + result.field = "password" + result.message = "密码不能为空" + return result + + result.valid = true + return result + +# 验证注册表单 +func validate_register_form(username: String, email: String, password: String, confirm_password: String, verification_code: String) -> Dictionary: + var result = {"valid": false, "field": "", "message": ""} + + # 验证用户名 + var username_validation = validate_username(username) + if not username_validation.valid: + result.field = "username" + result.message = username_validation.message + return result + + # 验证邮箱 + var email_validation = validate_email(email) + if not email_validation.valid: + result.field = "email" + result.message = email_validation.message + return result + + # 验证密码 + var password_validation = validate_password(password) + if not password_validation.valid: + result.field = "password" + result.message = password_validation.message + return result + + # 验证确认密码 + var confirm_validation = validate_confirm_password(password, confirm_password) + if not confirm_validation.valid: + result.field = "confirm" + result.message = confirm_validation.message + return result + + # 验证验证码 + var code_validation = validate_verification_code(verification_code) + if not code_validation.valid: + result.field = "verification" + result.message = code_validation.message + return result + + # 检查是否已发送验证码 + if not _has_sent_verification_code(email): + result.field = "verification" + result.message = "请先获取邮箱验证码" + return result + + result.valid = true + return result + +# 验证用户名 +func validate_username(username: String) -> Dictionary: + var result = {"valid": false, "message": ""} + + if username.is_empty(): + result.message = "用户名不能为空" + return result + + if not StringUtils.is_valid_username(username): + if username.length() > 50: + result.message = "用户名长度不能超过50字符" + else: + result.message = "用户名只能包含字母、数字和下划线" + return result + + result.valid = true + return result + +# 验证邮箱 +func validate_email(email: String) -> Dictionary: + var result = {"valid": false, "message": ""} + + if email.is_empty(): + result.message = "邮箱不能为空" + return result + + if not StringUtils.is_valid_email(email): + result.message = "请输入有效的邮箱地址" + return result + + result.valid = true + return result + +# 验证密码 +func validate_password(password: String) -> Dictionary: + return StringUtils.validate_password_strength(password) + +# 验证确认密码 +func validate_confirm_password(password: String, confirm: String) -> Dictionary: + var result = {"valid": false, "message": ""} + + if confirm.is_empty(): + result.message = "确认密码不能为空" + return result + + if password != confirm: + result.message = "两次输入的密码不一致" + return result + + result.valid = true + return result + +# 验证验证码 +func validate_verification_code(code: String) -> Dictionary: + var result = {"valid": false, "message": ""} + + if code.is_empty(): + result.message = "验证码不能为空" + return result + + if code.length() != 6: + result.message = "验证码必须是6位数字" + return result + + for i in range(code.length()): + var character = code[i] + if not (character >= '0' and character <= '9'): + result.message = "验证码必须是6位数字" + return result + + result.valid = true + return result + +# ============ 网络响应处理 ============ + +# 处理登录响应 +func _on_login_response(success: bool, data: Dictionary, error_info: Dictionary): + _reset_login_state() + + var result = ResponseHandler.handle_login_response(success, data, error_info) + + if result.should_show_toast: + show_toast_message.emit(result.message, result.success) + + if result.success: + var username = "" + if data.has("data") and data.data.has("user") and data.data.user.has("username"): + username = data.data.user.username + + # 延迟发送登录成功信号 + await Engine.get_main_loop().create_timer(1.0).timeout + login_success.emit(username) + else: + login_failed.emit(result.message) + +# 处理验证码登录响应 +func _on_verification_login_response(success: bool, data: Dictionary, error_info: Dictionary): + _reset_login_state() + + var result = ResponseHandler.handle_verification_code_login_response(success, data, error_info) + + if result.should_show_toast: + show_toast_message.emit(result.message, result.success) + + if result.success: + var username = "" + if data.has("data") and data.data.has("user") and data.data.user.has("username"): + username = data.data.user.username + + await Engine.get_main_loop().create_timer(1.0).timeout + login_success.emit(username) + else: + login_failed.emit(result.message) + +# 处理注册响应 +func _on_register_response(success: bool, data: Dictionary, error_info: Dictionary): + _reset_register_state() + + var result = ResponseHandler.handle_register_response(success, data, error_info) + + if result.should_show_toast: + show_toast_message.emit(result.message, result.success) + + if result.success: + register_success.emit(result.message) + else: + register_failed.emit(result.message) + +# 处理发送验证码响应 +func _on_send_code_response(success: bool, data: Dictionary, error_info: Dictionary): + var result = ResponseHandler.handle_send_verification_code_response(success, data, error_info) + + if result.should_show_toast: + show_toast_message.emit(result.message, result.success) + + if result.success: + verification_code_sent.emit(result.message) + else: + verification_code_failed.emit(result.message) + _reset_verification_code_state(current_email) + +# 处理发送登录验证码响应 +func _on_send_login_code_response(success: bool, data: Dictionary, error_info: Dictionary): + button_state_changed.emit("get_code_btn", false, "获取验证码") + + var result = ResponseHandler.handle_send_login_code_response(success, data, error_info) + + if result.should_show_toast: + show_toast_message.emit(result.message, result.success) + +# 处理忘记密码响应 +func _on_forgot_password_response(success: bool, data: Dictionary, error_info: Dictionary): + button_state_changed.emit("forgot_password_btn", false, "忘记密码") + + var result = ResponseHandler.handle_send_login_code_response(success, data, error_info) + + if result.should_show_toast: + show_toast_message.emit(result.message, result.success) + +# ============ 网络测试 ============ + +# 测试网络连接 +func test_network_connection(): + var request_id = NetworkManager.get_app_status(_on_network_test_response) + if request_id != "": + active_request_ids.append(request_id) + +# 处理网络测试响应 +func _on_network_test_response(success: bool, data: Dictionary, error_info: Dictionary): + var result = ResponseHandler.handle_network_test_response(success, data, error_info) + network_status_changed.emit(result.success, result.message) + +# ============ 私有辅助方法 ============ + +# 重置登录状态 +func _reset_login_state(): + is_processing = false + button_state_changed.emit("main_btn", false, "进入小镇") + +# 重置注册状态 +func _reset_register_state(): + is_processing = false + button_state_changed.emit("register_btn", false, "注册") + +# 检查是否可以发送验证码 +func _can_send_verification_code(email: String) -> bool: + if not verification_codes_sent.has(email): + return true + + var email_data = verification_codes_sent[email] + if not email_data.sent: + return true + + var current_time = Time.get_time_dict_from_system() + var current_timestamp = current_time.hour * 3600 + current_time.minute * 60 + current_time.second + + return (current_timestamp - email_data.time) >= code_cooldown + +# 获取剩余冷却时间 +func get_remaining_cooldown_time(email: String) -> int: + if not verification_codes_sent.has(email): + return 0 + + var email_data = verification_codes_sent[email] + var current_time = Time.get_time_dict_from_system() + var current_timestamp = current_time.hour * 3600 + current_time.minute * 60 + current_time.second + + return int(code_cooldown - (current_timestamp - email_data.time)) + +# 记录验证码发送状态 +func _record_verification_code_sent(email: String): + var current_time = Time.get_time_dict_from_system() + var current_timestamp = current_time.hour * 3600 + current_time.minute * 60 + current_time.second + + if not verification_codes_sent.has(email): + verification_codes_sent[email] = {} + + verification_codes_sent[email].sent = true + verification_codes_sent[email].time = current_timestamp + current_email = email + +# 重置验证码状态 +func _reset_verification_code_state(email: String): + if verification_codes_sent.has(email): + verification_codes_sent[email].sent = false + +# 检查是否已发送验证码 +func _has_sent_verification_code(email: String) -> bool: + if not verification_codes_sent.has(email): + return false + + return verification_codes_sent[email].get("sent", false) + +# 验证标识符格式 +func _is_valid_identifier(identifier: String) -> bool: + return StringUtils.is_valid_email(identifier) or _is_valid_phone(identifier) + +# 验证手机号格式 +func _is_valid_phone(phone: String) -> bool: + var regex = RegEx.new() + regex.compile("^\\+?[1-9]\\d{1,14}$") + return regex.search(phone) != null \ No newline at end of file diff --git a/_Core/managers/AuthManager.gd.uid b/_Core/managers/AuthManager.gd.uid new file mode 100644 index 0000000..e2a85c7 --- /dev/null +++ b/_Core/managers/AuthManager.gd.uid @@ -0,0 +1 @@ +uid://bpdyraefv0yta diff --git a/_Core/managers/ToastManager.gd b/_Core/managers/ToastManager.gd new file mode 100644 index 0000000..388189a --- /dev/null +++ b/_Core/managers/ToastManager.gd @@ -0,0 +1,234 @@ +class_name ToastManager + +# ============================================================================ +# ToastManager.gd - Toast消息管理器 +# ============================================================================ +# 负责创建和管理Toast消息的显示 +# +# 核心功能: +# - 创建Toast消息实例 +# - 管理Toast动画和生命周期 +# - 支持多个Toast同时显示 +# - 自动排列和清理Toast +# - 支持中文字体显示 +# +# 使用方式: +# var toast_manager = ToastManager.new() +# toast_manager.setup(toast_container) +# toast_manager.show_toast("消息内容", true) +# +# 注意事项: +# - 需要提供一个容器节点来承载Toast +# - 自动处理Toast的位置计算和动画 +# - 支持Web平台的字体处理 +# ============================================================================ + +extends RefCounted + +# ============ 成员变量 ============ + +# Toast容器和管理 +var toast_container: Control # Toast消息容器 +var active_toasts: Array = [] # 当前显示的Toast消息列表 +var toast_counter: int = 0 # Toast计数器,用于生成唯一ID + +# ============ 初始化方法 ============ + +# 设置Toast管理器 +# +# 参数: +# container: Control - Toast消息的容器节点 +func setup(container: Control): + toast_container = container + print("ToastManager 初始化完成") + +# ============ 公共方法 ============ + +# 显示Toast消息 +# +# 参数: +# message: String - 消息内容 +# is_success: bool - 是否为成功消息(影响颜色) +func show_toast(message: String, is_success: bool = true): + if toast_container == null: + print("错误: toast_container 节点不存在") + return + + print("显示Toast消息: ", message, " 成功: ", is_success) + _create_toast_instance(message, is_success) + +# 清理所有Toast +func clear_all_toasts(): + for toast in active_toasts: + if is_instance_valid(toast): + toast.queue_free() + active_toasts.clear() + +# ============ 私有方法 ============ + +# 创建Toast实例 +func _create_toast_instance(message: String, is_success: bool): + toast_counter += 1 + + # Web平台字体处理 + var is_web = OS.get_name() == "Web" + + # 1. 创建Toast Panel(方框UI) + var toast_panel = Panel.new() + toast_panel.name = "Toast_" + str(toast_counter) + + # 设置Toast样式 + var style = StyleBoxFlat.new() + if is_success: + style.bg_color = Color(0.15, 0.7, 0.15, 0.95) + style.border_color = Color(0.2, 0.9, 0.2, 0.9) + else: + style.bg_color = Color(0.7, 0.15, 0.15, 0.95) + style.border_color = Color(0.9, 0.2, 0.2, 0.9) + + style.border_width_left = 3 + style.border_width_top = 3 + style.border_width_right = 3 + style.border_width_bottom = 3 + style.corner_radius_top_left = 12 + style.corner_radius_top_right = 12 + style.corner_radius_bottom_left = 12 + style.corner_radius_bottom_right = 12 + style.shadow_color = Color(0, 0, 0, 0.3) + style.shadow_size = 4 + style.shadow_offset = Vector2(2, 2) + + toast_panel.add_theme_stylebox_override("panel", style) + + # 设置Toast基本尺寸 + var toast_width = 320 + toast_panel.size = Vector2(toast_width, 60) + + # 2. 创建VBoxContainer + var vbox = VBoxContainer.new() + vbox.add_theme_constant_override("separation", 0) + vbox.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT) + vbox.alignment = BoxContainer.ALIGNMENT_CENTER + + # 3. 创建CenterContainer + var center_container = CenterContainer.new() + center_container.size_flags_horizontal = Control.SIZE_EXPAND_FILL + center_container.size_flags_vertical = Control.SIZE_SHRINK_CENTER + + # 4. 创建Label(文字控件) + var text_label = Label.new() + text_label.text = message + text_label.add_theme_color_override("font_color", Color(1, 1, 1, 1)) + text_label.add_theme_font_size_override("font_size", 14) + + # 平台特定的字体处理 + if is_web: + print("Web平台Toast字体处理") + # Web平台使用主题文件 + var chinese_theme = load("res://assets/ui/chinese_theme.tres") + if chinese_theme: + text_label.theme = chinese_theme + print("Web平台应用中文主题") + else: + print("Web平台中文主题加载失败") + else: + print("桌面平台Toast字体处理") + # 桌面平台直接加载中文字体 + var desktop_chinese_font = load("res://assets/fonts/msyh.ttc") + if desktop_chinese_font: + text_label.add_theme_font_override("font", desktop_chinese_font) + print("桌面平台使用中文字体") + + text_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART + text_label.custom_minimum_size = Vector2(280, 0) + text_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER + text_label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER + + # 组装控件层级 + center_container.add_child(text_label) + vbox.add_child(center_container) + toast_panel.add_child(vbox) + + # 计算位置 + var margin = 20 + var start_x = toast_container.get_viewport().get_visible_rect().size.x + var final_x = toast_container.get_viewport().get_visible_rect().size.x - toast_width - margin + + # 计算Y位置 + var y_position = margin + for existing_toast in active_toasts: + if is_instance_valid(existing_toast): + y_position += existing_toast.size.y + 15 + + # 设置初始位置 + toast_panel.position = Vector2(start_x, y_position) + + # 添加到容器 + toast_container.add_child(toast_panel) + active_toasts.append(toast_panel) + + # 等待一帧让布局系统计算尺寸 + await toast_container.get_tree().process_frame + + # 让Toast高度自适应内容 + var content_size = vbox.get_combined_minimum_size() + var final_height = max(60, content_size.y + 20) # 最小60,加20像素边距 + toast_panel.size.y = final_height + + # 重新排列所有Toast + _rearrange_toasts() + + # 开始动画 + _animate_toast_in(toast_panel, final_x) + +# Toast入场动画 +func _animate_toast_in(toast_panel: Panel, final_x: float): + var tween = toast_container.create_tween() + tween.set_ease(Tween.EASE_OUT) + tween.set_trans(Tween.TRANS_BACK) + + tween.parallel().tween_property(toast_panel, "position:x", final_x, 0.6) + tween.parallel().tween_property(toast_panel, "modulate:a", 1.0, 0.4) + + toast_panel.modulate.a = 0.0 + + # 等待3秒后开始退场动画 + await toast_container.get_tree().create_timer(3.0).timeout + _animate_toast_out(toast_panel) + +# Toast退场动画 +func _animate_toast_out(toast_panel: Panel): + if not is_instance_valid(toast_panel): + return + + var tween = toast_container.create_tween() + tween.set_ease(Tween.EASE_IN) + tween.set_trans(Tween.TRANS_QUART) + + var end_x = toast_container.get_viewport().get_visible_rect().size.x + 50 + tween.parallel().tween_property(toast_panel, "position:x", end_x, 0.4) + tween.parallel().tween_property(toast_panel, "modulate:a", 0.0, 0.3) + + await tween.finished + _cleanup_toast(toast_panel) + +# 清理Toast +func _cleanup_toast(toast_panel: Panel): + if not is_instance_valid(toast_panel): + return + + active_toasts.erase(toast_panel) + _rearrange_toasts() + toast_panel.queue_free() + +# 重新排列Toast位置 +func _rearrange_toasts(): + var margin = 20 + var current_y = margin + + for i in range(active_toasts.size()): + var toast = active_toasts[i] + if is_instance_valid(toast): + var tween = toast_container.create_tween() + tween.tween_property(toast, "position:y", current_y, 0.2) + current_y += toast.size.y + 15 \ No newline at end of file diff --git a/_Core/managers/ToastManager.gd.uid b/_Core/managers/ToastManager.gd.uid new file mode 100644 index 0000000..102c857 --- /dev/null +++ b/_Core/managers/ToastManager.gd.uid @@ -0,0 +1 @@ +uid://buk7d21cag262 diff --git a/scenes/ui/AuthScene.gd b/scenes/ui/AuthScene.gd index 485da13..b9b1873 100644 --- a/scenes/ui/AuthScene.gd +++ b/scenes/ui/AuthScene.gd @@ -1,140 +1,223 @@ extends Control -# 信号定义 +# ============================================================================ +# AuthScene.gd - 认证场景视图控制器 +# ============================================================================ +# 认证系统的视图层控制器,只负责UI相关的操作 +# +# 核心职责: +# - UI控件的显示和隐藏 +# - 用户输入的收集和传递 +# - 错误提示的显示 +# - 按钮状态的管理 +# - Toast消息的显示 +# +# 业务逻辑: +# - 所有业务逻辑都委托给AuthManager处理 +# - 通过信号与AuthManager通信 +# - 不包含任何验证逻辑或网络请求 +# +# 注意事项: +# - 这是纯视图层,不处理业务逻辑 +# - 通过事件系统与业务层解耦 +# - 专注于用户体验和界面交互 +# ============================================================================ + +# ============ 信号定义 ============ + +# 登录成功信号 - 传递给上层场景 signal login_success(username: String) -# UI节点引用 -@onready var background_image: TextureRect = $BackgroundImage -@onready var login_panel: Panel = $CenterContainer/LoginPanel -@onready var register_panel: Panel = $CenterContainer/RegisterPanel -@onready var title_label: Label = $CenterContainer/LoginPanel/VBoxContainer/TitleLabel -@onready var subtitle_label: Label = $CenterContainer/LoginPanel/VBoxContainer/SubtitleLabel -@onready var whale_frame: TextureRect = $WhaleFrame +# ============ UI节点引用 ============ -# 登录表单 +# 主要容器和背景 +@onready var background_image: TextureRect = $BackgroundImage # 背景图片 +@onready var login_panel: Panel = $CenterContainer/LoginPanel # 登录面板 +@onready var register_panel: Panel = $CenterContainer/RegisterPanel # 注册面板 +@onready var title_label: Label = $CenterContainer/LoginPanel/VBoxContainer/TitleLabel # 标题标签 +@onready var subtitle_label: Label = $CenterContainer/LoginPanel/VBoxContainer/SubtitleLabel # 副标题标签 +@onready var whale_frame: TextureRect = $WhaleFrame # 鲸鱼装饰框 + +# 登录表单输入控件 @onready var login_username: LineEdit = $CenterContainer/LoginPanel/VBoxContainer/LoginForm/UsernameContainer/UsernameInput @onready var login_password: LineEdit = $CenterContainer/LoginPanel/VBoxContainer/LoginForm/PasswordContainer/PasswordInput @onready var login_verification: LineEdit = $CenterContainer/LoginPanel/VBoxContainer/LoginForm/VerificationContainer/VerificationInputContainer/VerificationInput + +# 登录表单错误提示标签 @onready var login_username_error: Label = $CenterContainer/LoginPanel/VBoxContainer/LoginForm/UsernameContainer/UsernameLabelContainer/UsernameError @onready var login_password_error: Label = $CenterContainer/LoginPanel/VBoxContainer/LoginForm/PasswordContainer/PasswordLabelContainer/PasswordError @onready var login_verification_error: Label = $CenterContainer/LoginPanel/VBoxContainer/LoginForm/VerificationContainer/VerificationLabelContainer/VerificationError + +# 登录表单容器(用于显示/隐藏) @onready var password_container: VBoxContainer = $CenterContainer/LoginPanel/VBoxContainer/LoginForm/PasswordContainer @onready var verification_container: VBoxContainer = $CenterContainer/LoginPanel/VBoxContainer/LoginForm/VerificationContainer + +# 登录相关按钮 @onready var get_code_btn: Button = $CenterContainer/LoginPanel/VBoxContainer/LoginForm/VerificationContainer/VerificationInputContainer/GetCodeBtn @onready var main_btn: Button = $CenterContainer/LoginPanel/VBoxContainer/MainButton @onready var login_btn: Button = $CenterContainer/LoginPanel/VBoxContainer/ButtonContainer/LoginBtn @onready var forgot_password_btn: Button = $CenterContainer/LoginPanel/VBoxContainer/BottomLinks/ForgotPassword @onready var register_link_btn: Button = $CenterContainer/LoginPanel/VBoxContainer/BottomLinks/RegisterLink -# 注册表单 +# 注册表单输入控件 @onready var register_username: LineEdit = $CenterContainer/RegisterPanel/VBoxContainer/RegisterForm/UsernameContainer/UsernameInput @onready var register_email: LineEdit = $CenterContainer/RegisterPanel/VBoxContainer/RegisterForm/EmailContainer/EmailInput @onready var register_password: LineEdit = $CenterContainer/RegisterPanel/VBoxContainer/RegisterForm/PasswordContainer/PasswordInput @onready var register_confirm: LineEdit = $CenterContainer/RegisterPanel/VBoxContainer/RegisterForm/ConfirmContainer/ConfirmInput @onready var verification_input: LineEdit = $CenterContainer/RegisterPanel/VBoxContainer/RegisterForm/VerificationContainer/VerificationInputContainer/VerificationInput + +# 注册相关按钮 @onready var send_code_btn: Button = $CenterContainer/RegisterPanel/VBoxContainer/RegisterForm/VerificationContainer/VerificationInputContainer/SendCodeBtn @onready var register_btn: Button = $CenterContainer/RegisterPanel/VBoxContainer/ButtonContainer/RegisterBtn @onready var to_login_btn: Button = $CenterContainer/RegisterPanel/VBoxContainer/ButtonContainer/ToLoginBtn -# 错误提示标签 +# 注册表单错误提示标签 @onready var register_username_error: Label = $CenterContainer/RegisterPanel/VBoxContainer/RegisterForm/UsernameContainer/UsernameLabelContainer/UsernameError @onready var register_email_error: Label = $CenterContainer/RegisterPanel/VBoxContainer/RegisterForm/EmailContainer/EmailLabelContainer/EmailError @onready var register_password_error: Label = $CenterContainer/RegisterPanel/VBoxContainer/RegisterForm/PasswordContainer/PasswordLabelContainer/PasswordError @onready var register_confirm_error: Label = $CenterContainer/RegisterPanel/VBoxContainer/RegisterForm/ConfirmContainer/ConfirmLabelContainer/ConfirmError @onready var verification_error: Label = $CenterContainer/RegisterPanel/VBoxContainer/RegisterForm/VerificationContainer/VerificationLabelContainer/VerificationError -# Toast消息节点 -@onready var toast_container: Control = $ToastContainer +# Toast消息系统 +@onready var toast_container: Control = $ToastContainer # Toast消息容器 -# Toast管理 -var active_toasts: Array = [] -var toast_counter: int = 0 +# ============ 成员变量 ============ -# 验证码状态 -var verification_codes_sent: Dictionary = {} -var code_cooldown: float = 60.0 +# 业务逻辑管理器 +var auth_manager: AuthManager + +# Toast消息管理器 +var toast_manager: ToastManager + +# 验证码冷却计时器 var cooldown_timer: Timer = null -var current_email: String = "" -# 登录模式枚举 -enum LoginMode { - PASSWORD, # 密码登录模式 - VERIFICATION # 验证码登录模式 -} +# 当前登录模式(从管理器同步) +var current_login_mode: AuthManager.LoginMode = AuthManager.LoginMode.PASSWORD -# 当前登录模式 -var current_login_mode: LoginMode = LoginMode.PASSWORD - -# 网络请求管理 -var active_request_ids: Array = [] +# ============ 生命周期方法 ============ +# 初始化认证场景 func _ready(): - connect_signals() - show_login_panel() - update_login_mode_ui() # 初始化登录模式UI - await get_tree().process_frame - print("认证系统已加载") - test_network_connection() - -func test_network_connection(): - print("=== 测试网络连接 ===") + _setup_controllers() # 初始化控制器 + _connect_signals() # 连接信号 + _setup_ui() # 设置UI初始状态 - var request_id = NetworkManager.get_app_status(_on_network_test_response) - if request_id != "": - active_request_ids.append(request_id) - print("网络测试请求已发送,ID: ", request_id) - else: - print("网络连接测试失败") -func connect_signals(): - # 主要按钮 + print("认证场景视图已加载") + + # 测试网络连接 + auth_manager.test_network_connection() + +# 设置控制器 +func _setup_controllers(): + # 创建业务逻辑管理器 + auth_manager = AuthManager.new() + + # 创建Toast管理器 + toast_manager = ToastManager.new() + toast_manager.setup(toast_container) + + # 连接管理器信号 + _connect_controller_signals() + +# 连接管理器信号 +func _connect_controller_signals(): + # 登录相关信号 + auth_manager.login_success.connect(_on_controller_login_success) + auth_manager.login_failed.connect(_on_controller_login_failed) + + # 注册相关信号 + auth_manager.register_success.connect(_on_controller_register_success) + auth_manager.register_failed.connect(_on_controller_register_failed) + + # 验证码相关信号 + auth_manager.verification_code_sent.connect(_on_controller_verification_code_sent) + auth_manager.verification_code_failed.connect(_on_controller_verification_code_failed) + + # 表单验证信号 + auth_manager.form_validation_failed.connect(_on_controller_form_validation_failed) + + # 网络状态信号 + auth_manager.network_status_changed.connect(_on_controller_network_status_changed) + + # 按钮状态信号 + auth_manager.button_state_changed.connect(_on_controller_button_state_changed) + + # Toast消息信号 + auth_manager.show_toast_message.connect(_on_controller_show_toast_message) + +# 设置UI初始状态 +func _setup_ui(): + show_login_panel() + _update_login_mode_ui() + +# 连接UI信号 +func _connect_signals(): + # 主要按钮信号 main_btn.pressed.connect(_on_main_button_pressed) - # 登录界面按钮 - login_btn.pressed.connect(_on_login_pressed) + # 登录界面按钮信号 + login_btn.pressed.connect(_on_login_mode_toggle_pressed) forgot_password_btn.pressed.connect(_on_forgot_password_pressed) register_link_btn.pressed.connect(_on_register_link_pressed) get_code_btn.pressed.connect(_on_get_login_code_pressed) - # 注册界面按钮 + # 注册界面按钮信号 register_btn.pressed.connect(_on_register_pressed) to_login_btn.pressed.connect(_on_to_login_pressed) send_code_btn.pressed.connect(_on_send_code_pressed) - # 回车键登录 + # 回车键快捷登录 login_password.text_submitted.connect(_on_login_enter) - # 登录表单失焦验证 + # 表单失焦验证事件 + _connect_validation_signals() + + # 实时输入验证事件 + _connect_input_change_signals() + +# 连接表单验证信号 +func _connect_validation_signals(): + # 登录表单 login_username.focus_exited.connect(_on_login_username_focus_exited) login_password.focus_exited.connect(_on_login_password_focus_exited) login_verification.focus_exited.connect(_on_login_verification_focus_exited) - # 注册表单失焦验证 + # 注册表单 register_username.focus_exited.connect(_on_register_username_focus_exited) register_email.focus_exited.connect(_on_register_email_focus_exited) register_password.focus_exited.connect(_on_register_password_focus_exited) register_confirm.focus_exited.connect(_on_register_confirm_focus_exited) verification_input.focus_exited.connect(_on_verification_focus_exited) - - # 实时输入验证 + +# 连接输入变化信号 +func _connect_input_change_signals(): register_username.text_changed.connect(_on_register_username_text_changed) register_email.text_changed.connect(_on_register_email_text_changed) register_password.text_changed.connect(_on_register_password_text_changed) register_confirm.text_changed.connect(_on_register_confirm_text_changed) verification_input.text_changed.connect(_on_verification_text_changed) +# ============ UI面板管理 ============ +# 显示登录面板 func show_login_panel(): login_panel.visible = true register_panel.visible = false login_username.grab_focus() +# 显示注册面板 func show_register_panel(): login_panel.visible = false register_panel.visible = true register_username.grab_focus() +# ============ 登录模式管理 ============ + # 更新登录模式UI -func update_login_mode_ui(): - if current_login_mode == LoginMode.PASSWORD: +func _update_login_mode_ui(): + current_login_mode = auth_manager.get_current_login_mode() + + if current_login_mode == AuthManager.LoginMode.PASSWORD: # 密码登录模式 login_btn.text = "验证码登录" forgot_password_btn.text = "忘记密码" @@ -145,7 +228,7 @@ func update_login_mode_ui(): # 清空验证码输入框和错误提示 login_verification.text = "" - hide_field_error(login_verification_error) + _hide_field_error(login_verification_error) else: # VERIFICATION mode # 验证码登录模式 @@ -158,341 +241,185 @@ func update_login_mode_ui(): # 清空密码输入框和错误提示 login_password.text = "" - hide_field_error(login_password_error) - # 这里需要根据实际UI结构调整 - -# 切换登录模式 -func toggle_login_mode(): - if current_login_mode == LoginMode.PASSWORD: - current_login_mode = LoginMode.VERIFICATION - else: - current_login_mode = LoginMode.PASSWORD - - update_login_mode_ui() - - # 清空输入框 - login_username.text = "" - login_password.text = "" - hide_field_error(login_username_error) - hide_field_error(login_password_error) + _hide_field_error(login_password_error) # ============ 按钮事件处理 ============ +# 主按钮点击事件 func _on_main_button_pressed(): - # 根据当前登录模式执行不同的登录逻辑 - if current_login_mode == LoginMode.PASSWORD: + if current_login_mode == AuthManager.LoginMode.PASSWORD: _execute_password_login() else: _execute_verification_login() +# 执行密码登录 func _execute_password_login(): - if not validate_login_form(): - return - var username = login_username.text.strip_edges() var password = login_password.text - show_loading(main_btn, "登录中...") - show_toast('正在验证登录信息...', true) - - var request_id = NetworkManager.login(username, password, _on_login_response) - if request_id != "": - active_request_ids.append(request_id) - else: - restore_button(main_btn, "进入小镇") - show_toast('网络请求失败', false) + auth_manager.execute_password_login(username, password) +# 执行验证码登录 func _execute_verification_login(): var identifier = login_username.text.strip_edges() var verification_code = login_verification.text.strip_edges() - if identifier.is_empty(): - show_field_error(login_username_error, "请输入用户名/手机/邮箱") - login_username.grab_focus() - return - - if verification_code.is_empty(): - show_field_error(login_verification_error, "请输入验证码") - login_verification.grab_focus() - return - - show_loading(main_btn, "登录中...") - show_toast('正在验证验证码...', true) - - var request_id = NetworkManager.verification_code_login(identifier, verification_code, _on_verification_login_response) - if request_id != "": - active_request_ids.append(request_id) - else: - restore_button(main_btn, "进入小镇") - show_toast('网络请求失败', false) + auth_manager.execute_verification_login(identifier, verification_code) -func _on_login_pressed(): - # 现在这个按钮用于切换登录模式 - toggle_login_mode() +# 登录模式切换按钮 +func _on_login_mode_toggle_pressed(): + auth_manager.toggle_login_mode() + _update_login_mode_ui() + + # 清空输入框和错误提示 + login_username.text = "" + login_password.text = "" + login_verification.text = "" + _hide_field_error(login_username_error) + _hide_field_error(login_password_error) + _hide_field_error(login_verification_error) +# 注册按钮点击事件 func _on_register_pressed(): - print("注册按钮被点击") - - if not validate_register_form(): - print("注册表单验证失败") - show_toast('请检查并完善注册信息', false) - return - - print("注册表单验证通过,开始注册流程") - var username = register_username.text.strip_edges() var email = register_email.text.strip_edges() var password = register_password.text + var confirm_password = register_confirm.text var verification_code = verification_input.text.strip_edges() - show_loading(register_btn, "注册中...") - show_toast('正在创建账户...', true) - - # 直接调用注册接口,让服务器端处理验证码验证 - send_register_request(username, email, password, verification_code) + auth_manager.execute_register(username, email, password, confirm_password, verification_code) +# 发送邮箱验证码按钮 func _on_send_code_pressed(): var email = register_email.text.strip_edges() + auth_manager.send_email_verification_code(email) - var email_validation = validate_email(email) - if not email_validation.valid: - show_toast(email_validation.message, false) - register_email.grab_focus() - return - - hide_field_error(register_email_error) - - # 检查冷却时间 - var current_time = Time.get_time_dict_from_system() - var current_timestamp = current_time.hour * 3600 + current_time.minute * 60 + current_time.second - - if verification_codes_sent.has(email): - var email_data = verification_codes_sent[email] - if email_data.sent and (current_timestamp - email_data.time) < code_cooldown: - var remaining = code_cooldown - (current_timestamp - email_data.time) - show_toast('该邮箱请等待 %d 秒后再次发送' % remaining, false) - return - - if current_email != email: - stop_current_cooldown() - current_email = email - - if not verification_codes_sent.has(email): - verification_codes_sent[email] = {} - - verification_codes_sent[email].sent = true - verification_codes_sent[email].time = current_timestamp - start_cooldown_timer(email) - - var request_id = NetworkManager.send_email_verification(email, _on_send_code_response) - if request_id != "": - active_request_ids.append(request_id) - else: - show_toast('网络请求失败', false) - reset_verification_button() - -func _on_register_link_pressed(): - show_register_panel() + # 开始冷却计时器 + _start_cooldown_timer(email) +# 获取登录验证码按钮 func _on_get_login_code_pressed(): var identifier = login_username.text.strip_edges() - - if identifier.is_empty(): - show_field_error(login_username_error, "请先输入用户名/手机/邮箱") - login_username.grab_focus() - return - - show_loading(get_code_btn, "发送中...") - show_toast('正在发送登录验证码...', true) - - var request_id = NetworkManager.send_login_verification_code(identifier, _on_send_login_code_response) - if request_id != "": - active_request_ids.append(request_id) - else: - restore_button(get_code_btn, "获取验证码") - show_toast('网络请求失败', false) - -func _on_to_login_pressed(): - show_login_panel() - -func _on_login_enter(_text: String): - _on_login_pressed() + auth_manager.send_login_verification_code(identifier) +# 忘记密码按钮 func _on_forgot_password_pressed(): var identifier = login_username.text.strip_edges() - if identifier.is_empty(): - show_toast('请先输入邮箱或手机号', false) - login_username.grab_focus() - return - - if not is_valid_email(identifier) and not is_valid_phone(identifier): - show_toast('请输入有效的邮箱或手机号', false) - login_username.grab_focus() - return - - if current_login_mode == LoginMode.PASSWORD: + if current_login_mode == AuthManager.LoginMode.PASSWORD: # 密码登录模式:发送密码重置验证码 - show_loading(forgot_password_btn, "发送中...") - show_toast('正在发送密码重置验证码...', true) - - var request_id = NetworkManager.forgot_password(identifier, _on_forgot_password_response) - if request_id != "": - active_request_ids.append(request_id) - else: - restore_button(forgot_password_btn, "忘记密码") - show_toast('网络请求失败', false) + auth_manager.send_password_reset_code(identifier) else: # 验证码登录模式:发送登录验证码 - show_loading(forgot_password_btn, "发送中...") - show_toast('正在发送登录验证码...', true) - - var request_id = NetworkManager.send_login_verification_code(identifier, _on_send_login_code_response) - if request_id != "": - active_request_ids.append(request_id) - else: - restore_button(forgot_password_btn, "获取验证码") - show_toast('网络请求失败', false) -# ============ 网络响应处理 ============ + auth_manager.send_login_verification_code(identifier) -func send_register_request(username: String, email: String, password: String, verification_code: String = ""): - var request_id = NetworkManager.register(username, password, username, email, verification_code, _on_register_response) - if request_id != "": - active_request_ids.append(request_id) - else: - show_toast('网络请求失败', false) - restore_button(register_btn, "注册") +# 跳转到注册面板 +func _on_register_link_pressed(): + show_register_panel() -func _on_network_test_response(success: bool, data: Dictionary, error_info: Dictionary): - print("=== 网络测试响应处理 ===") - print("成功: ", success) - print("数据: ", data) - - var result = ResponseHandler.handle_network_test_response(success, data, error_info) - - if result.should_show_toast: - show_toast(result.message, result.success) +# 返回登录面板 +func _on_to_login_pressed(): + show_login_panel() -func _on_login_response(success: bool, data: Dictionary, error_info: Dictionary): - print("=== 登录响应处理 ===") - print("成功: ", success) - print("数据: ", data) - print("错误信息: ", error_info) - - restore_button(main_btn, "进入小镇") - - var result = ResponseHandler.handle_login_response(success, data, error_info) - - if result.should_show_toast: - show_toast(result.message, result.success) - - if result.success: - var username = login_username.text.strip_edges() - if data.has("data") and data.data.has("user") and data.data.user.has("username"): - username = data.data.user.username - - login_username.text = "" - login_password.text = "" - hide_field_error(login_username_error) - hide_field_error(login_password_error) - - await get_tree().create_timer(1.0).timeout - login_success.emit(username) +# 回车键登录 +func _on_login_enter(_text: String): + _on_main_button_pressed() -func _on_send_code_response(success: bool, data: Dictionary, error_info: Dictionary): - print("=== 发送验证码响应处理 ===") - print("成功: ", success) - print("数据: ", data) - - var result = ResponseHandler.handle_send_verification_code_response(success, data, error_info) - - if result.should_show_toast: - show_toast(result.message, result.success) - - if not result.success: - reset_verification_button() +# ============ 控制器信号处理 ============ -func _on_send_login_code_response(success: bool, data: Dictionary, error_info: Dictionary): - print("=== 发送登录验证码响应处理 ===") - print("成功: ", success) - print("数据: ", data) +# 登录成功处理 +func _on_controller_login_success(username: String): + # 清空表单 + login_username.text = "" + login_password.text = "" + login_verification.text = "" + _hide_field_error(login_username_error) + _hide_field_error(login_password_error) + _hide_field_error(login_verification_error) - # 恢复按钮状态 - if current_login_mode == LoginMode.PASSWORD: - restore_button(forgot_password_btn, "忘记密码") - else: - restore_button(forgot_password_btn, "获取验证码") - restore_button(get_code_btn, "获取验证码") - - var result = ResponseHandler.handle_send_login_code_response(success, data, error_info) - - if result.should_show_toast: - show_toast(result.message, result.success) + # 发送登录成功信号给上层 + login_success.emit(username) -func _on_verification_login_response(success: bool, data: Dictionary, error_info: Dictionary): - print("=== 验证码登录响应处理 ===") - print("成功: ", success) - print("数据: ", data) - print("错误信息: ", error_info) - - restore_button(main_btn, "进入小镇") - - var result = ResponseHandler.handle_verification_code_login_response(success, data, error_info) - - if result.should_show_toast: - show_toast(result.message, result.success) - - if result.success: - var username = login_username.text.strip_edges() - if data.has("data") and data.data.has("user") and data.data.user.has("username"): - username = data.data.user.username - - login_username.text = "" - hide_field_error(login_username_error) - - await get_tree().create_timer(1.0).timeout - login_success.emit(username) +# 登录失败处理 +func _on_controller_login_failed(_message: String): + # 登录失败时不需要额外处理,Toast已经显示了错误信息 + pass -func _on_forgot_password_response(success: bool, data: Dictionary, error_info: Dictionary): - print("=== 忘记密码响应处理 ===") - print("成功: ", success) - print("数据: ", data) - - restore_button(forgot_password_btn, "忘记密码") - - # 使用通用的发送验证码响应处理 - var result = ResponseHandler.handle_send_login_code_response(success, data, error_info) - - if result.should_show_toast: - show_toast(result.message, result.success) - - if result.success: - show_toast("密码重置验证码已发送,请查收邮件", true) +# 注册成功处理 +func _on_controller_register_success(_message: String): + # 清空注册表单 + _clear_register_form() + # 切换到登录面板 + show_login_panel() + # 将注册时的用户名填入登录框 + login_username.text = register_username.text.strip_edges() + +# 注册失败处理 +func _on_controller_register_failed(_message: String): + # 注册失败时不需要额外处理,Toast已经显示了错误信息 + pass + +# 验证码发送成功处理 +func _on_controller_verification_code_sent(_message: String): + # 验证码发送成功,冷却计时器已经在按钮点击时启动 + pass + +# 验证码发送失败处理 +func _on_controller_verification_code_failed(_message: String): + # 重置验证码按钮状态 + _reset_verification_button() + +# 表单验证失败处理 +func _on_controller_form_validation_failed(field: String, message: String): + match field: + "username": + _show_field_error(login_username_error, message) + login_username.grab_focus() + "password": + _show_field_error(login_password_error, message) + login_password.grab_focus() + "verification": + if current_login_mode == AuthManager.LoginMode.VERIFICATION: + _show_field_error(login_verification_error, message) + login_verification.grab_focus() + else: + _show_field_error(verification_error, message) + verification_input.grab_focus() + "email": + _show_field_error(register_email_error, message) + register_email.grab_focus() + "confirm": + _show_field_error(register_confirm_error, message) + register_confirm.grab_focus() + +# 网络状态变化处理 +func _on_controller_network_status_changed(network_connected: bool, message: String): + # 可以在这里添加网络状态指示器 + print("网络状态: ", "连接" if network_connected else "断开", " - ", message) + +# 按钮状态变化处理 +func _on_controller_button_state_changed(button_name: String, is_loading: bool, text: String): + match button_name: + "main_btn": + _set_button_state(main_btn, is_loading, text) + "register_btn": + _set_button_state(register_btn, is_loading, text) + "get_code_btn": + _set_button_state(get_code_btn, is_loading, text) + "forgot_password_btn": + _set_button_state(forgot_password_btn, is_loading, text) + +# Toast消息处理 +func _on_controller_show_toast_message(message: String, is_success: bool): + toast_manager.show_toast(message, is_success) -func _on_register_response(success: bool, data: Dictionary, error_info: Dictionary): - print("=== 注册响应处理 ===") - print("成功: ", success) - print("数据: ", data) - - restore_button(register_btn, "注册") - - var result = ResponseHandler.handle_register_response(success, data, error_info) - - if result.should_show_toast: - show_toast(result.message, result.success) - - if result.success: - clear_register_form() - show_login_panel() - login_username.text = register_username.text.strip_edges() # 使用注册时的用户名 # ============ 验证码冷却管理 ============ -func start_cooldown_timer(email: String): +# 开始冷却计时器 +func _start_cooldown_timer(_email: String): if cooldown_timer != null: cooldown_timer.queue_free() - current_email = email - send_code_btn.disabled = true send_code_btn.text = "重新发送(60)" @@ -502,531 +429,181 @@ func start_cooldown_timer(email: String): cooldown_timer.timeout.connect(_on_cooldown_timer_timeout) cooldown_timer.start() +# 冷却计时器超时处理 func _on_cooldown_timer_timeout(): - var input_email = register_email.text.strip_edges() + var remaining_time = auth_manager.get_remaining_cooldown_time(register_email.text.strip_edges()) - if input_email != current_email: - stop_current_cooldown() - return - - if verification_codes_sent.has(current_email): - var current_time = Time.get_time_dict_from_system() - var current_timestamp = current_time.hour * 3600 + current_time.minute * 60 + current_time.second - var email_data = verification_codes_sent[current_email] - var remaining = code_cooldown - (current_timestamp - email_data.time) + if remaining_time > 0: + send_code_btn.text = "重新发送(%d)" % remaining_time + else: + send_code_btn.text = "重新发送" + send_code_btn.disabled = false - if remaining > 0: - send_code_btn.text = "重新发送(%d)" % remaining - else: - send_code_btn.text = "重新发送" - send_code_btn.disabled = false - - if cooldown_timer != null: - cooldown_timer.queue_free() - cooldown_timer = null - current_email = "" + if cooldown_timer != null: + cooldown_timer.queue_free() + cooldown_timer = null -func stop_current_cooldown(): +# 重置验证码按钮状态 +func _reset_verification_button(): if cooldown_timer != null: cooldown_timer.queue_free() cooldown_timer = null send_code_btn.disabled = false send_code_btn.text = "发送验证码" - current_email = "" -func reset_verification_button(): - if current_email != "" and verification_codes_sent.has(current_email): - verification_codes_sent[current_email].sent = false - - stop_current_cooldown() +# ============ UI工具方法 ============ -func clear_register_form(): +# 设置按钮状态 +func _set_button_state(button: Button, is_loading: bool, text: String): + button.disabled = is_loading + button.text = text + +# 显示字段错误 +func _show_field_error(error_label: Label, message: String): + error_label.text = message + error_label.visible = true + +# 隐藏字段错误 +func _hide_field_error(error_label: Label): + error_label.visible = false + +# 清空注册表单 +func _clear_register_form(): register_username.text = "" register_email.text = "" register_password.text = "" register_confirm.text = "" verification_input.text = "" - stop_current_cooldown() - verification_codes_sent.clear() + _reset_verification_button() - hide_field_error(register_username_error) - hide_field_error(register_email_error) - hide_field_error(register_password_error) - hide_field_error(register_confirm_error) - hide_field_error(verification_error) - -# ============ Toast消息系统 ============ - -# 检测文本是否包含中文字符 -func contains_chinese(text: String) -> bool: - for i in range(text.length()): - var char_code = text.unicode_at(i) - # 中文字符的Unicode范围 - if (char_code >= 0x4E00 and char_code <= 0x9FFF) or \ - (char_code >= 0x3400 and char_code <= 0x4DBF) or \ - (char_code >= 0x20000 and char_code <= 0x2A6DF): - return true - return false - -func show_toast(message: String, is_success: bool = true): - print("显示Toast消息: ", message, " 成功: ", is_success) - - if toast_container == null: - print("错误: toast_container 节点不存在") - return - - # 异步创建Toast实例 - create_toast_instance(message, is_success) - -func create_toast_instance(message: String, is_success: bool): - toast_counter += 1 - - # Web平台字体处理 - var is_web = OS.get_name() == "Web" - - # 1. 创建Toast Panel(方框UI) - var toast_panel = Panel.new() - toast_panel.name = "Toast_" + str(toast_counter) - - # 设置Toast样式 - var style = StyleBoxFlat.new() - if is_success: - style.bg_color = Color(0.15, 0.7, 0.15, 0.95) - style.border_color = Color(0.2, 0.9, 0.2, 0.9) - else: - style.bg_color = Color(0.7, 0.15, 0.15, 0.95) - style.border_color = Color(0.9, 0.2, 0.2, 0.9) - - style.border_width_left = 3 - style.border_width_top = 3 - style.border_width_right = 3 - style.border_width_bottom = 3 - style.corner_radius_top_left = 12 - style.corner_radius_top_right = 12 - style.corner_radius_bottom_left = 12 - style.corner_radius_bottom_right = 12 - style.shadow_color = Color(0, 0, 0, 0.3) - style.shadow_size = 4 - style.shadow_offset = Vector2(2, 2) - - toast_panel.add_theme_stylebox_override("panel", style) - - # 设置Toast基本尺寸 - var toast_width = 320 - toast_panel.size = Vector2(toast_width, 60) - - # 2. 创建VBoxContainer - var vbox = VBoxContainer.new() - vbox.add_theme_constant_override("separation", 0) - vbox.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT) - vbox.alignment = BoxContainer.ALIGNMENT_CENTER - - # 3. 创建CenterContainer - var center_container = CenterContainer.new() - center_container.size_flags_horizontal = Control.SIZE_EXPAND_FILL - center_container.size_flags_vertical = Control.SIZE_SHRINK_CENTER - - # 4. 创建Label(文字控件) - var text_label = Label.new() - text_label.text = message - text_label.add_theme_color_override("font_color", Color(1, 1, 1, 1)) - text_label.add_theme_font_size_override("font_size", 14) - - # 平台特定的字体处理 - if is_web: - print("Web平台Toast字体处理") - # Web平台使用主题文件 - var chinese_theme = load("res://assets/ui/chinese_theme.tres") - if chinese_theme: - text_label.theme = chinese_theme - print("Web平台应用中文主题") - else: - print("Web平台中文主题加载失败") - else: - print("桌面平台Toast字体处理") - # 桌面平台直接加载中文字体 - var desktop_chinese_font = load("res://assets/fonts/msyh.ttc") - if desktop_chinese_font: - text_label.add_theme_font_override("font", desktop_chinese_font) - print("桌面平台使用中文字体") - - text_label.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART - text_label.custom_minimum_size = Vector2(280, 0) - text_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER - text_label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER - - # 组装控件层级 - center_container.add_child(text_label) - vbox.add_child(center_container) - toast_panel.add_child(vbox) - - # 计算位置 - var margin = 20 - var start_x = get_viewport().get_visible_rect().size.x - var final_x = get_viewport().get_visible_rect().size.x - toast_width - margin - - # 计算Y位置 - var y_position = margin - for existing_toast in active_toasts: - if is_instance_valid(existing_toast): - y_position += existing_toast.size.y + 15 - - # 设置初始位置 - toast_panel.position = Vector2(start_x, y_position) - - # 添加到容器 - toast_container.add_child(toast_panel) - active_toasts.append(toast_panel) - - # 等待一帧让布局系统计算尺寸 - await get_tree().process_frame - - # 让Toast高度自适应内容 - var content_size = vbox.get_combined_minimum_size() - var final_height = max(60, content_size.y + 20) # 最小60,加20像素边距 - toast_panel.size.y = final_height - - # 重新排列所有Toast - rearrange_toasts() - - # 开始动画 - animate_toast_in(toast_panel, final_x) - -func animate_toast_in(toast_panel: Panel, final_x: float): - var tween = create_tween() - tween.set_ease(Tween.EASE_OUT) - tween.set_trans(Tween.TRANS_BACK) - - tween.parallel().tween_property(toast_panel, "position:x", final_x, 0.6) - tween.parallel().tween_property(toast_panel, "modulate:a", 1.0, 0.4) - - toast_panel.modulate.a = 0.0 - - await get_tree().create_timer(3.0).timeout - animate_toast_out(toast_panel) - -func animate_toast_out(toast_panel: Panel): - if not is_instance_valid(toast_panel): - return - - var tween = create_tween() - tween.set_ease(Tween.EASE_IN) - tween.set_trans(Tween.TRANS_QUART) - - var end_x = get_viewport().get_visible_rect().size.x + 50 - tween.parallel().tween_property(toast_panel, "position:x", end_x, 0.4) - tween.parallel().tween_property(toast_panel, "modulate:a", 0.0, 0.3) - - await tween.finished - cleanup_toast(toast_panel) - -func cleanup_toast(toast_panel: Panel): - if not is_instance_valid(toast_panel): - return - - active_toasts.erase(toast_panel) - rearrange_toasts() - toast_panel.queue_free() - -func rearrange_toasts(): - var margin = 20 - var current_y = margin - - for i in range(active_toasts.size()): - var toast = active_toasts[i] - if is_instance_valid(toast): - var tween = create_tween() - tween.tween_property(toast, "position:y", current_y, 0.2) - current_y += toast.size.y + 15 - -# ============ UI工具方法 ============ - -func show_loading(button: Button, loading_text: String): - button.disabled = true - button.text = loading_text - -func restore_button(button: Button, original_text: String): - button.disabled = false - button.text = original_text - -func show_field_error(error_label: Label, message: String): - error_label.text = message - error_label.visible = true - -func hide_field_error(error_label: Label): - error_label.visible = false - -func is_valid_email(email: String) -> bool: - return StringUtils.is_valid_email(email) - -func is_valid_phone(phone: String) -> bool: - var regex = RegEx.new() - regex.compile("^\\+?[1-9]\\d{1,14}$") - return regex.search(phone) != null - -# ============ 表单验证方法 ============ - -func validate_username(username: String) -> Dictionary: - var result = {"valid": false, "message": ""} - - if username.is_empty(): - result.message = "用户名不能为空" - return result - - if not StringUtils.is_valid_username(username): - if username.length() > 50: - result.message = "用户名长度不能超过50字符" - else: - result.message = "用户名只能包含字母、数字和下划线" - return result - - result.valid = true - return result - -func validate_email(email: String) -> Dictionary: - var result = {"valid": false, "message": ""} - - if email.is_empty(): - result.message = "邮箱不能为空" - return result - - if not StringUtils.is_valid_email(email): - result.message = "请输入有效的邮箱地址" - return result - - result.valid = true - return result - -func validate_password(password: String) -> Dictionary: - return StringUtils.validate_password_strength(password) - -func validate_confirm_password(password: String, confirm: String) -> Dictionary: - var result = {"valid": false, "message": ""} - - if confirm.is_empty(): - result.message = "确认密码不能为空" - return result - - if password != confirm: - result.message = "两次输入的密码不一致" - return result - - result.valid = true - return result - -func validate_verification_code(code: String) -> Dictionary: - var result = {"valid": false, "message": ""} - - if code.is_empty(): - result.message = "验证码不能为空" - return result - - if code.length() != 6: - result.message = "验证码必须是6位数字" - return result - - for i in range(code.length()): - var character = code[i] - if not (character >= '0' and character <= '9'): - result.message = "验证码必须是6位数字" - return result - - result.valid = true - return result - -# ============ 表单验证事件 ============ + _hide_field_error(register_username_error) + _hide_field_error(register_email_error) + _hide_field_error(register_password_error) + _hide_field_error(register_confirm_error) + _hide_field_error(verification_error) +# ============ 表单验证事件处理 ============ +# 登录用户名失焦验证 func _on_login_username_focus_exited(): var username = login_username.text.strip_edges() if username.is_empty(): - show_field_error(login_username_error, "用户名不能为空") + _show_field_error(login_username_error, "用户名不能为空") else: - hide_field_error(login_username_error) + _hide_field_error(login_username_error) +# 登录密码失焦验证 func _on_login_password_focus_exited(): var password = login_password.text if password.is_empty(): - show_field_error(login_password_error, "密码不能为空") + _show_field_error(login_password_error, "密码不能为空") else: - hide_field_error(login_password_error) + _hide_field_error(login_password_error) +# 登录验证码失焦验证 func _on_login_verification_focus_exited(): var verification_code = login_verification.text.strip_edges() - if verification_code.is_empty(): - show_field_error(login_verification_error, "验证码不能为空") - elif verification_code.length() != 6: - show_field_error(login_verification_error, "验证码必须是6位数字") - elif not verification_code.is_valid_int(): - show_field_error(login_verification_error, "验证码只能包含数字") + var validation = auth_manager.validate_verification_code(verification_code) + if not validation.valid: + _show_field_error(login_verification_error, validation.message) else: - hide_field_error(login_verification_error) + _hide_field_error(login_verification_error) +# 注册用户名失焦验证 func _on_register_username_focus_exited(): var username = register_username.text.strip_edges() - var validation = validate_username(username) + var validation = auth_manager.validate_username(username) if not validation.valid: - show_field_error(register_username_error, validation.message) + _show_field_error(register_username_error, validation.message) else: - hide_field_error(register_username_error) + _hide_field_error(register_username_error) +# 注册邮箱失焦验证 func _on_register_email_focus_exited(): var email = register_email.text.strip_edges() - var validation = validate_email(email) + var validation = auth_manager.validate_email(email) if not validation.valid: - show_field_error(register_email_error, validation.message) + _show_field_error(register_email_error, validation.message) else: - hide_field_error(register_email_error) + _hide_field_error(register_email_error) +# 注册密码失焦验证 func _on_register_password_focus_exited(): var password = register_password.text - var validation = validate_password(password) + var validation = auth_manager.validate_password(password) if not validation.valid: - show_field_error(register_password_error, validation.message) + _show_field_error(register_password_error, validation.message) else: - hide_field_error(register_password_error) + _hide_field_error(register_password_error) + # 如果确认密码不为空,重新验证确认密码 if not register_confirm.text.is_empty(): _on_register_confirm_focus_exited() +# 注册确认密码失焦验证 func _on_register_confirm_focus_exited(): var password = register_password.text var confirm = register_confirm.text - var validation = validate_confirm_password(password, confirm) + var validation = auth_manager.validate_confirm_password(password, confirm) if not validation.valid: - show_field_error(register_confirm_error, validation.message) + _show_field_error(register_confirm_error, validation.message) else: - hide_field_error(register_confirm_error) + _hide_field_error(register_confirm_error) +# 验证码失焦验证 func _on_verification_focus_exited(): var code = verification_input.text.strip_edges() - var validation = validate_verification_code(code) + var validation = auth_manager.validate_verification_code(code) if not validation.valid: - show_field_error(verification_error, validation.message) + _show_field_error(verification_error, validation.message) else: - hide_field_error(verification_error) + _hide_field_error(verification_error) # ============ 实时输入验证事件 ============ +# 注册用户名输入变化 func _on_register_username_text_changed(new_text: String): if register_username_error.visible and not new_text.is_empty(): - hide_field_error(register_username_error) + _hide_field_error(register_username_error) +# 注册邮箱输入变化 func _on_register_email_text_changed(new_text: String): if register_email_error.visible and not new_text.is_empty(): - hide_field_error(register_email_error) + _hide_field_error(register_email_error) +# 注册密码输入变化 func _on_register_password_text_changed(new_text: String): if register_password_error.visible and not new_text.is_empty(): - hide_field_error(register_password_error) + _hide_field_error(register_password_error) +# 注册确认密码输入变化 func _on_register_confirm_text_changed(new_text: String): if register_confirm_error.visible and not new_text.is_empty(): - hide_field_error(register_confirm_error) + _hide_field_error(register_confirm_error) +# 验证码输入变化 func _on_verification_text_changed(new_text: String): if verification_error.visible and not new_text.is_empty(): - hide_field_error(verification_error) -# ============ 表单整体验证 ============ - -func validate_login_form() -> bool: - var is_valid = true - - var username = login_username.text.strip_edges() - var password = login_password.text - - if username.is_empty(): - show_field_error(login_username_error, "用户名不能为空") - is_valid = false - else: - hide_field_error(login_username_error) - - if password.is_empty(): - show_field_error(login_password_error, "密码不能为空") - is_valid = false - else: - hide_field_error(login_password_error) - - return is_valid - -func validate_register_form() -> bool: - print("开始验证注册表单") - var is_valid = true - - var username = register_username.text.strip_edges() - var email = register_email.text.strip_edges() - var password = register_password.text - var confirm = register_confirm.text - var verification_code = verification_input.text.strip_edges() - - print("表单数据: 用户名='%s', 邮箱='%s', 密码长度=%d, 确认密码长度=%d, 验证码='%s'" % [username, email, password.length(), confirm.length(), verification_code]) - - var username_validation = validate_username(username) - if not username_validation.valid: - print("用户名验证失败: ", username_validation.message) - show_field_error(register_username_error, username_validation.message) - is_valid = false - else: - hide_field_error(register_username_error) - - var email_validation = validate_email(email) - if not email_validation.valid: - print("邮箱验证失败: ", email_validation.message) - show_field_error(register_email_error, email_validation.message) - is_valid = false - else: - hide_field_error(register_email_error) - - var password_validation = validate_password(password) - if not password_validation.valid: - print("密码验证失败: ", password_validation.message) - show_field_error(register_password_error, password_validation.message) - is_valid = false - else: - hide_field_error(register_password_error) - - var confirm_validation = validate_confirm_password(password, confirm) - if not confirm_validation.valid: - print("确认密码验证失败: ", confirm_validation.message) - show_field_error(register_confirm_error, confirm_validation.message) - is_valid = false - else: - hide_field_error(register_confirm_error) - - var code_validation = validate_verification_code(verification_code) - if not code_validation.valid: - print("验证码格式验证失败: ", code_validation.message) - show_field_error(verification_error, code_validation.message) - is_valid = false - else: - hide_field_error(verification_error) - - var current_email_input = register_email.text.strip_edges() - var has_sent_code = false - - if verification_codes_sent.has(current_email_input): - var email_data = verification_codes_sent[current_email_input] - has_sent_code = email_data.get("sent", false) - - if not has_sent_code: - print("当前邮箱验证码未发送,email = ", current_email_input) - show_toast("请先获取邮箱验证码", false) - is_valid = false - - print("表单验证结果: ", is_valid) - return is_valid + _hide_field_error(verification_error) # ============ 资源清理 ============ +# 节点销毁时清理资源 func _exit_tree(): - for request_id in active_request_ids: - NetworkManager.cancel_request(request_id) - active_request_ids.clear() + # 清理控制器 + if auth_manager: + auth_manager.cleanup() + # 清理Toast管理器 + if toast_manager: + toast_manager.clear_all_toasts() + + # 清理计时器 if cooldown_timer != null: cooldown_timer.queue_free() cooldown_timer = null +# 处理ESC键退出 func _input(event): if event.is_action_pressed("ui_cancel"): - get_tree().quit() \ No newline at end of file + get_tree().quit()