176 lines
5.2 KiB
C#
176 lines
5.2 KiB
C#
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();
|
|
}
|
|
resendBtn.gameObject.SetActive(false);
|
|
countdownText.gameObject.SetActive(true);
|
|
// 开始倒计时
|
|
StartCountdown(countdownSeconds);
|
|
emailText.text = DataManager.Instance.userInfo.email;
|
|
countdownText.text = countdownTip.Replace("{0}", countdownSeconds.ToString());
|
|
}
|
|
|
|
|
|
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()
|
|
{
|
|
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;
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|