using System.Collections; using UnityEngine; using UnityEngine.UI; using Kill.UI.Components; using Kill.Managers; using System.Threading.Tasks; namespace Kill.UI.Pages { /// /// 验证码页面 /// public class VerificationCodePanel : LoginSubPageBase { [Header("显示")] public Text countdownText; public Text emailText; public Text errorText; public int countdownSeconds=60; [Header("输入")] public VerificationCodeInput codeInput; [Header("按钮")] public Button resendBtn; // 倒计时协程 private Coroutine countdownCoroutine; private string countdownTip; // 当前场景类型 private CodeSceneType currentSceneType = CodeSceneType.Register; /// /// 验证码使用场景 /// public enum CodeSceneType { Register, // 注册 ResetPassword // 忘记密码重置 } protected override void OnInitialize() { if (codeInput != null) { codeInput.OnCodeCompleted += OnCodeCompleted; } if (resendBtn != null) resendBtn.onClick.AddListener(OnResend); countdownTip=LanguageManager.Instance.GetLanguage("100014"); } protected override void OnShow() { // 清空验证码 if (codeInput != null) { codeInput.Clear(); codeInput.Focus(); } resendBtn.gameObject.SetActive(false); countdownText.gameObject.SetActive(true); // 开始倒计时 StartCountdown(countdownSeconds); emailText.text = LoginPageCtrl.Instance.GetCurrentEmail(); countdownText.text=countdownTip.Replace("{0}",countdownSeconds.ToString()); } protected override void OnHide() { // 停止倒计时 if (countdownCoroutine != null) { StopCoroutine(countdownCoroutine); countdownCoroutine = null; } } private void OnCodeCompleted(string code) { Debug.Log($"验证码输入完成: {code}"); // 可以自动提交 OnConfirm(); } /// /// 设置当前场景类型 /// public void SetSceneType(CodeSceneType type) { currentSceneType = type; } private void OnConfirm() { string code = codeInput != null ? codeInput.GetCode() : ""; // 验证验证码 LoginPageCtrl.Instance.VerifyCode(emailText.text, code, onSuccess: () => { // 验证成功,跳转到设置密码页 var setPasswordPanel = GetSubPage(LoginPageCtrl.SubPageType.SetPassword) as SetPasswordPanel; if (setPasswordPanel != null) { // 根据场景设置不同模式 if (currentSceneType == CodeSceneType.Register) { setPasswordPanel.SetMode(SetPasswordPanel.SetPasswordMode.Register); } else { setPasswordPanel.SetMode(SetPasswordPanel.SetPasswordMode.ResetPassword); } } ClearHistoryAndShow(LoginPageCtrl.SubPageType.SetPassword); }, onError: (code, message) => { // 显示错误 errorText.text = LanguageManager.Instance.GetLanguage("100017"); } ); } private async void OnResend() { await LoginPageCtrl.Instance.SendCode(emailText.text, () => { resendBtn.gameObject.SetActive(false); countdownText.gameObject.SetActive(true); Debug.Log("重新发送验证码"); StartCountdown(countdownSeconds); }); } private void StartCountdown(int seconds) { if (countdownCoroutine != null) { StopCoroutine(countdownCoroutine); } countdownCoroutine = StartCoroutine(CountdownCoroutine(seconds)); } private IEnumerator CountdownCoroutine(int seconds) { int remaining = seconds; while (remaining > 0) { string tip=countdownTip.Replace("{0}",remaining.ToString()); if (countdownText != null) countdownText.text = tip; yield return new WaitForSeconds(1); remaining--; } resendBtn.gameObject.SetActive(true); countdownText.gameObject.SetActive(false); countdownCoroutine = null; } } }