killapp/Assets/Scripts/UI/Pages/Login/SubPages/VerificationCodePanel.cs

164 lines
5.0 KiB
C#
Raw Normal View History

2026-04-16 14:57:19 +08:00
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using Kill.UI.Components;
using Kill.Managers;
using System.Threading.Tasks;
namespace Kill.UI.Pages
{
/// <summary>
/// 验证码页面
/// </summary>
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;
/// <summary>
/// 验证码使用场景
/// </summary>
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();
}
/// <summary>
/// 设置当前场景类型
/// </summary>
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;
}
}
}