using System.Collections; using UnityEngine; using UnityEngine.UI; using Kill.UI.Components; using Kill.Managers; using System.Threading.Tasks; using System; using Kill.Network; namespace Kill.UI.Pages { /// /// 验证码页面 /// public class SelfVerificationCodePanel : MonoBehaviour { [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; public Button backButton; Action onSuccessCallback; public void Init(Action onsuccess) { onSuccessCallback=onsuccess; UIManager.Instance.RegisterBackAction(OnBack); backButton.onClick.RemoveAllListeners(); backButton.onClick.AddListener(OnBack); if (codeInput != null) { codeInput.OnCodeCompleted += OnCodeCompleted; } if (resendBtn != null) resendBtn.onClick.AddListener(OnResend); countdownTip = LanguageManager.Instance.GetLanguage("100014"); // 清空验证码 if (codeInput != null) { codeInput.Clear(); codeInput.Focus(); } resendBtn.gameObject.SetActive(true); countdownText.gameObject.SetActive(false); emailText.text = DataManager.Instance.userInfo.email; countdownText.text = countdownTip.Replace("{0}", countdownSeconds.ToString()); OnResend(); } private void OnDestroy() { // 停止倒计时 if (countdownCoroutine != null) { StopCoroutine(countdownCoroutine); countdownCoroutine = null; } } private void OnCodeCompleted(string code) { Debug.Log($"验证码输入完成: {code}"); // 可以自动提交 OnConfirm(); } private async Task OnConfirm() { string code = codeInput != null ? codeInput.GetCode() : ""; // 验证验证码 VerifyCode(emailText.text, code, onSuccess: () => { onSuccessCallback?.Invoke(); }, onError: (code, message) => { // 显示错误 errorText.text = LanguageManager.Instance.GetLanguage("100017"); } ); } private async void OnResend() { LoadingUI.Show(); var requestData = new EmailCodeRequest { email = emailText.text }; try { var response = await NetworkCtrl.Instance.Post("/api/v1/auth/email/code", requestData); LoadingUI.Hide(); ResponseCodeHandler.HandleResponse(response, onSuccess: (data) => { Debug.Log("验证码发送成功"); resendBtn.gameObject.SetActive(false); countdownText.gameObject.SetActive(true); StartCountdown(countdownSeconds); }, onError: null ); } catch (Exception ex) { LoadingUI.Hide(); Debug.LogError("发送验证码异常: " + ex.Message); } } 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; } public void OnBack() { UIManager.Instance.RegisterBackAction(GetComponentInParent().OnBack); Destroy(gameObject); } /// /// 校验验证码 /// public async void VerifyCode(string email, string code, Action onSuccess, Action onError) { LoadingUI.Show(); var requestData = new VerifyCodeRequest { email = email, code = code }; try { var response = await NetworkCtrl.Instance.Post("/api/v1/auth/email/verify", 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); } } } }