using System;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using Kill.Managers;
using Kill.Network;
using Kill.UI.Components;
namespace Kill.UI.Pages
{
///
/// 注销账号页面(提示确认 -> 验证码(复用SelfVerificationCodePanel) -> 最终确认)
/// 在 SelfInfoPage 中通过 cancelAccountPagePrefab 实例化,Init 传入返回回调
///
public class SelfCancelAccountPage : MonoBehaviour
{
[Header("提示界面1(提示确认)")]
public GameObject stepConfirm;
public Button confirmNextBtn; // 确认,进入验证码
public Button step1CancelBtn; // 取消,关闭页面
[Header("验证码页(复用 SelfVerificationCodePanel)")]
public SelfVerificationCodePanel verificationCodePanelPrefab;
[Header("提示界面2(最终确认)")]
public GameObject stepFinal;
public Button finalConfirmBtn; // 确认注销
public Button step3BackBtn; // 返回
private Action onBackCallback;
private string verifyCode; // 校验通过的验证码
private bool isSubmitting = false;
private SelfVerificationCodePanel verificationCodePanel;
public void Init(Action onBack)
{
onBackCallback = onBack;
UIManager.Instance.RegisterBackAction(OnBack);
// 提示界面1
if (confirmNextBtn != null)
{
confirmNextBtn.onClick.RemoveAllListeners();
confirmNextBtn.onClick.AddListener(OnConfirmNext);
}
if (step1CancelBtn != null)
{
step1CancelBtn.onClick.RemoveAllListeners();
step1CancelBtn.onClick.AddListener(OnBack);
}
// 提示界面2
if (finalConfirmBtn != null)
{
finalConfirmBtn.onClick.RemoveAllListeners();
finalConfirmBtn.onClick.AddListener(OnFinalConfirm);
}
if (step3BackBtn != null)
{
step3BackBtn.onClick.RemoveAllListeners();
step3BackBtn.onClick.AddListener(OnBack);
}
// 默认显示提示界面1
ShowStep(stepConfirm);
}
///
/// 切换显示步骤
///
private void ShowStep(GameObject step)
{
if (stepConfirm != null) stepConfirm.SetActive(step == stepConfirm);
if (stepFinal != null) stepFinal.SetActive(step == stepFinal);
}
///
/// 提示界面1确认,打开验证码面板(注销场景)
///
private void OnConfirmNext()
{
stepConfirm.SetActive(false);
if (verificationCodePanelPrefab == null)
return;
verificationCodePanel = Instantiate(verificationCodePanelPrefab, transform);
verificationCodePanel.gameObject.SetActive(true);
verificationCodePanel.Init(OnVerifySuccess, OnBack, SelfVerificationCodePanel.VerificationScene.CancelAccount);
}
///
/// 验证码校验成功,进入最终确认
///
private void OnVerifySuccess()
{
verifyCode = verificationCodePanel != null ? verificationCodePanel.VerifiedCode : "";
if (verificationCodePanel != null)
{
Destroy(verificationCodePanel.gameObject);
verificationCodePanel = null;
}
ShowStep(stepFinal);
}
///
/// 提示界面2最终确认,执行注销
///
private async void OnFinalConfirm()
{
if (isSubmitting)
return;
isSubmitting = true;
LoadingUI.Show();
var requestData = new CancelAccountRequest
{
email = DataManager.Instance.userInfo.email,
code = verifyCode
};
try
{
var response = await NetworkCtrl.Instance.Post("/api/v1/user/cancel", requestData);
LoadingUI.Hide();
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
isSubmitting = false;
DataManager.Instance.ClearInfo();
UIManager.Instance.OpenMainPage(UIManager.PageName.loginPage);
},
onError: (code, message) =>
{
isSubmitting = false;
// 验证码错误或已过期
if (code == 603)
{
ToastUI.Show("100342");
}
// 其他原因注销失败
else
{
ToastUI.Show("100343");
}
// 回到注销账号页最初状态(提示界面1)
verifyCode = "";
ShowStep(stepConfirm);
}
);
}
catch (Exception ex)
{
LoadingUI.Hide();
isSubmitting = false;
Debug.LogError("注销账号异常: " + ex.Message);
ToastUI.ShowText(ex.Message);
}
}
public void OnBack()
{
UIManager.Instance.RegisterBackAction(onBackCallback);
Destroy(gameObject);
}
}
}