killapp/Assets/Scripts/UI/Pages/SelfPage/SelfVerificationCodePanel.cs

197 lines
5.8 KiB
C#
Raw Permalink Normal View History

2026-06-08 08:55:10 +08:00
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
{
/// <summary>
/// 验证码页面
/// </summary>
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();
}
2026-06-10 15:04:14 +08:00
resendBtn.gameObject.SetActive(true);
countdownText.gameObject.SetActive(false);
2026-06-08 08:55:10 +08:00
emailText.text = DataManager.Instance.userInfo.email;
countdownText.text = countdownTip.Replace("{0}", countdownSeconds.ToString());
2026-06-10 15:04:14 +08:00
OnResend();
2026-06-08 08:55:10 +08:00
}
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()
{
2026-06-10 15:04:14 +08:00
LoadingUI.Show();
var requestData = new EmailCodeRequest
{
email = emailText.text
};
try
{
var response = await NetworkCtrl.Instance.Post<NoDataResponse>("/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)
2026-06-08 08:55:10 +08:00
{
2026-06-10 15:04:14 +08:00
LoadingUI.Hide();
Debug.LogError("发送验证码异常: " + ex.Message);
}
2026-06-08 08:55:10 +08:00
}
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<SelfInfoPage>().OnBack);
Destroy(gameObject);
}
/// <summary>
/// 校验验证码
/// </summary>
public async void VerifyCode(string email, string code, Action onSuccess, Action<int, string> onError)
{
LoadingUI.Show();
var requestData = new VerifyCodeRequest
{
email = email,
code = code
};
try
{
var response = await NetworkCtrl.Instance.Post<NoDataResponse>("/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);
}
}
}
}