using UnityEngine; using UnityEngine.UI; using Kill.Utils; using Kill.Managers; using System.Threading.Tasks; using System; using Kill.UI.Components; using Kill.Network; namespace Kill.UI.Pages { /// /// 设置密码页面 /// 用于注册设置密码和忘记密码重置密码 /// public class SelfSetPasswordPanel : MonoBehaviour { [Header("输入组件")] public InputField newPasswordInput; public InputField confirmPasswordInput; [Header("显示组件")] public Text errorText; /// /// 密码类型提示文本0:简单密码,1:复杂密码 /// public Text[] passwordTypeTips; [Header("按钮")] public Button confirmBtn; public Button backBtn; private int passwordType = 0; private bool isConfirmEnabled = false; Action callback; public void Init(int type, Action callback) { passwordType = type; this.callback = callback; if (confirmBtn != null) { confirmBtn.onClick.RemoveAllListeners(); confirmBtn.onClick.AddListener(OnConfirm); } if (backBtn != null) { backBtn.onClick.RemoveAllListeners(); backBtn.onClick.AddListener(OnBack); } newPasswordInput.onEndEdit.AddListener(CheckPasswordFormat); confirmPasswordInput.onEndEdit.AddListener(CheckPasswordFormat); errorText.text = ""; // 页面显示时初始化密码输入框 newPasswordInput.text = ""; confirmPasswordInput.text = ""; errorText.text = ""; ChangePasswordType(type); } /// /// 切换密码类型 /// /// 密码类型0:简单密码,1:复杂密码 private void ChangePasswordType(int type) { errorText.text = ""; passwordType = type; newPasswordInput.text = ""; confirmPasswordInput.text = ""; if (type == 0) { newPasswordInput.characterLimit = 6; confirmPasswordInput.characterLimit = 6; newPasswordInput.contentType = InputField.ContentType.Pin; confirmPasswordInput.contentType = InputField.ContentType.Pin; } else { newPasswordInput.characterLimit = 16; confirmPasswordInput.characterLimit = 16; newPasswordInput.contentType = InputField.ContentType.Password; confirmPasswordInput.contentType = InputField.ContentType.Password; } passwordTypeTips[passwordType].gameObject.SetActive(true); passwordTypeTips[1 - passwordType].gameObject.SetActive(false); } public void CheckPasswordFormat(string password) { isConfirmEnabled = true; errorText.text = ""; if (passwordType == 0) { if (!ValidationUtils.IsValidSimplePassword(newPasswordInput.text)) { errorText.text = LanguageManager.Instance.GetLanguage("100025"); isConfirmEnabled = false; return; } } else { if (!ValidationUtils.IsValidComplexPassword(newPasswordInput.text)) { errorText.text = LanguageManager.Instance.GetLanguage("100029"); isConfirmEnabled = false; return; } } if (newPasswordInput.text != confirmPasswordInput.text) { errorText.text = LanguageManager.Instance.GetLanguage("100028"); isConfirmEnabled = false; } } /// /// 确认设置密码 /// private void OnConfirm() { if (!isConfirmEnabled) return; string newPwd = newPasswordInput != null ? newPasswordInput.text : ""; string email = DataManager.Instance.userInfo.email; // 重置密码模式 - 调用重置密码接口 ResetPassword(email, newPwd, passwordType, onSuccess: () => { if (passwordType == 0) { DataManager.Instance.userInfo.has_simple_password = true; } else { DataManager.Instance.userInfo.has_complex_password = true; } callback?.Invoke(); }, onError: (code, msg) => { errorText.text = code.ToString(); } ); } public void OnBack() { UIManager.Instance.RegisterBackAction(GetComponentInParent().OnBack); Destroy(gameObject); } /// /// 重置密码(忘记密码) /// public async void ResetPassword(string email, string password, int password_type, Action onSuccess, Action onError) { LoadingUI.Show(); var requestData = new ResetPasswordRequest { email = email, password = password, password_type = password_type }; try { var response = await NetworkCtrl.Instance.Post("/api/v1/auth/password/reset", requestData); LoadingUI.Hide(); ResponseCodeHandler.HandleResponse(response, onSuccess: (data) => { Debug.Log("重置密码成功"); onSuccess?.Invoke(); }, onError: onError ); } catch (Exception ex) { LoadingUI.Hide(); Debug.LogError("重置密码异常: " + ex.Message); onError?.Invoke(-1, ex.Message); } } } }