using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; /// /// 添加用户面板 /// 提供用户名、密码和角色选择功能 /// public class AddUserPanel : BasePanel { [Header("输入字段")] public TMP_InputField UsernameInput; // 用户名输入框 public TMP_InputField PasswordInput; // 密码输入框 public TMP_InputField ConfirmPasswordInput; // 确认密码输入框 public TMP_Dropdown RoleDropdown; // 角色下拉框 // [Header("显示标签")] // public TextMeshProUGUI UsernameLabel; // public TextMeshProUGUI PasswordLabel; // public TextMeshProUGUI ConfirmPasswordLabel; // public TextMeshProUGUI RoleLabel; public TextMeshProUGUI StatusText; // 状态显示文本 [Header("操作按钮")] public Button HomeButton; public Button AddButton; // 添加按钮 // public Button CancelButton; // 取消按钮 public Button BackButton; // 返回按钮 [Header("验证设置")] public int MinUsernameLength = 3; public int MinPasswordLength = 6; private IAuthenticationService _authService; public override void Init() { _authService = ServiceLocator.Get(); InitializeUI(); SetupValidation(); ClearInputs(); } private void InitializeUI() { // 设置标签文本 // if (UsernameLabel != null) UsernameLabel.text = "用户名:"; // if (PasswordLabel != null) PasswordLabel.text = "密码:"; // if (ConfirmPasswordLabel != null) ConfirmPasswordLabel.text = "确认密码:"; // if (RoleLabel != null) RoleLabel.text = "角色:"; // 初始化角色下拉框 if (RoleDropdown != null) { RoleDropdown.ClearOptions(); var options = new List { "普通用户", "管理员" }; RoleDropdown.AddOptions(options); RoleDropdown.value = 0; // 默认选择普通用户 } // 设置密码输入框为密码模式 if (PasswordInput != null) { PasswordInput.contentType = TMP_InputField.ContentType.Password; } if (ConfirmPasswordInput != null) { ConfirmPasswordInput.contentType = TMP_InputField.ContentType.Password; } // 设置按钮事件 if (HomeButton != null) { HomeButton.onClick.AddListener(() => { ReturnToHome(); }); } if (AddButton != null) { AddButton.onClick.AddListener(OnAddButtonClicked); } // if (CancelButton != null) // { // CancelButton.onClick.AddListener(OnCancelButtonClicked); // } if (BackButton != null) { BackButton.onClick.AddListener(OnBackButtonClicked); } } private void SetupValidation() { // 添加输入验证监听器和自定义键盘 if (UsernameInput != null) { UsernameInput.onValueChanged.AddListener(OnInputChanged); CustomKeyboardManager.SetupInputField(UsernameInput, KeyboardLayout.Default, (newValue) => { UsernameInput.text = newValue; OnInputChanged(newValue); }, clearOnFirstInput: true); } if (PasswordInput != null) { PasswordInput.onValueChanged.AddListener(OnInputChanged); CustomKeyboardManager.SetupInputField(PasswordInput, KeyboardLayout.Default, (newValue) => { PasswordInput.text = newValue; OnInputChanged(newValue); }, clearOnFirstInput: true); } if (ConfirmPasswordInput != null) { ConfirmPasswordInput.onValueChanged.AddListener(OnInputChanged); CustomKeyboardManager.SetupInputField(ConfirmPasswordInput, KeyboardLayout.Default, (newValue) => { ConfirmPasswordInput.text = newValue; OnInputChanged(newValue); }, clearOnFirstInput: true); } } private void OnInputChanged(string value) { ValidateInputs(); } private bool ValidateInputs() { string username = UsernameInput?.text ?? ""; string password = PasswordInput?.text ?? ""; string confirmPassword = ConfirmPasswordInput?.text ?? ""; // 验证用户名 if (string.IsNullOrEmpty(username)) { SetStatus("请输入用户名", false); return false; } if (username.Length < MinUsernameLength) { SetStatus($"用户名至少需要{MinUsernameLength}个字符", false); return false; } // 检查用户名是否已存在 if (_authService != null) { var existingUsers = _authService.GetAllUsers(); if (existingUsers.Exists(u => u.Username.Equals(username, System.StringComparison.OrdinalIgnoreCase))) { SetStatus("用户名已存在", false); return false; } } // 验证密码 if (string.IsNullOrEmpty(password)) { SetStatus("请输入密码", false); return false; } if (password.Length < MinPasswordLength) { SetStatus($"密码至少需要{MinPasswordLength}个字符", false); return false; } // 验证确认密码 if (password != confirmPassword) { SetStatus("两次输入的密码不一致", false); return false; } SetStatus("输入验证通过", true); return true; } private void SetStatus(string message, bool isSuccess) { if (StatusText != null) { StatusText.text = message; StatusText.color = isSuccess ? Color.green : Color.red; } // 根据验证结果启用/禁用添加按钮 if (AddButton != null) { AddButton.interactable = isSuccess; } } private void OnAddButtonClicked() { if (!ValidateInputs()) return; string username = UsernameInput.text; string password = PasswordInput.text; UserRole role = RoleDropdown.value == 0 ? UserRole.User : UserRole.Admin; // 显示确认对话框 ConfirmDialog.ShowSaveConfirm( $"确定要添加用户 '{username}' 吗?\n角色: {(role == UserRole.Admin ? "管理员" : "普通用户")}", () => AddUser(username, password, role), () => Debug.Log("取消添加用户") ); } private void AddUser(string username, string password, UserRole role) { if (_authService == null) { SetStatus("认证服务不可用", false); return; } bool success = _authService.AddUser(username, password, role); if (success) { SetStatus("用户添加成功!", true); Debug.Log($"成功添加用户: {username} ({role})"); // 延迟关闭面板 StartCoroutine(CloseAfterDelay(1.5f)); } else { SetStatus("添加用户失败", false); } } private System.Collections.IEnumerator CloseAfterDelay(float delay) { yield return new WaitForSeconds(delay); ClosePanel(); } private void OnCancelButtonClicked() { if (HasUnsavedChanges()) { ConfirmDialog.Show("确认取消", "确定要取消添加用户吗?已输入的信息将丢失。", () => UIManager.Instance.HidePanel(), null, "确认", "继续编辑", true); } else { ClosePanel(); } } private void OnBackButtonClicked() { OnCancelButtonClicked(); // 和取消按钮相同逻辑 } private bool HasUnsavedChanges() { return !string.IsNullOrEmpty(UsernameInput?.text) || !string.IsNullOrEmpty(PasswordInput?.text) || !string.IsNullOrEmpty(ConfirmPasswordInput?.text); } private void ClearInputs() { if (UsernameInput != null) UsernameInput.text = ""; if (PasswordInput != null) PasswordInput.text = ""; if (ConfirmPasswordInput != null) ConfirmPasswordInput.text = ""; if (RoleDropdown != null) RoleDropdown.value = 0; SetStatus("请填写用户信息", true); } // public void ClosePanel() // { // UIManager.Instance.HidePanel(); // } void OnDestroy() { // 清理事件监听器 if (UsernameInput != null) UsernameInput.onValueChanged.RemoveListener(OnInputChanged); if (PasswordInput != null) PasswordInput.onValueChanged.RemoveListener(OnInputChanged); if (ConfirmPasswordInput != null) ConfirmPasswordInput.onValueChanged.RemoveListener(OnInputChanged); // 关闭自定义键盘(如果正在显示) if (CustomKeyboardManager.Instance != null && CustomKeyboardManager.Instance.IsKeyboardShowing()) { CustomKeyboardManager.Instance.HideKeyboard(); } } }