killapp/Assets/Scripts/UI/Pages/Login/SubPages/SetPasswordPanel.cs
2026-04-16 14:57:19 +08:00

181 lines
6.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using UnityEngine.UI;
using Kill.Utils;
using Kill.Managers;
using System.Threading.Tasks;
using System;
namespace Kill.UI.Pages
{
/// <summary>
/// 设置密码页面
/// 用于注册设置密码和忘记密码重置密码
/// </summary>
public class SetPasswordPanel : LoginSubPageBase
{
[Header("输入组件")]
public InputField newPasswordInput;
public InputField confirmPasswordInput;
[Header("显示组件")]
public Text errorText;
/// <summary>
/// 密码类型提示文本0简单密码1复杂密码
/// </summary>
public Text[] passwordTypeTips;
[Header("按钮")]
public Button confirmBtn;
/// <summary>
/// 切换简单/复杂密码按钮 0简单密码选中 1简单密码未选中 2复杂密码选中 3复杂密码未选中 未选中可点击
/// </summary>
public GameObject[] changeTypeBtns;
private int passwordType = 0;
private bool isConfirmEnabled = false;
// 当前模式
private SetPasswordMode currentMode = SetPasswordMode.Register;
/// <summary>
/// 设置密码模式
/// </summary>
public enum SetPasswordMode
{
Register, // 注册设置密码
ResetPassword // 忘记密码重置密码
}
protected override void OnInitialize()
{
if (confirmBtn != null)
confirmBtn.onClick.AddListener(OnConfirm);
if (changeTypeBtns != null && changeTypeBtns.Length == 4)
{
changeTypeBtns[1].GetComponent<Button>().onClick.AddListener(() => ChangePasswordType(0));
changeTypeBtns[3].GetComponent<Button>().onClick.AddListener(() => ChangePasswordType(1));
}
newPasswordInput.onEndEdit.AddListener(CheckPasswordFormat);
confirmPasswordInput.onEndEdit.AddListener(CheckPasswordFormat);
}
protected override void OnShow()
{
// 页面显示时初始化密码输入框
newPasswordInput.text = "";
confirmPasswordInput.text = "";
ChangePasswordType(0);
}
/// <summary>
/// 切换密码类型
/// </summary>
/// <param name="type">密码类型0简单密码1复杂密码</param>
private void ChangePasswordType(int type)
{
passwordType = type;
newPasswordInput.text = "";
confirmPasswordInput.text = "";
if (type == 0)
{
newPasswordInput.characterLimit = 6;
confirmPasswordInput.characterLimit = 6;
newPasswordInput.contentType = InputField.ContentType.Pin;
confirmPasswordInput.contentType = InputField.ContentType.Pin;
changeTypeBtns[0].SetActive(true);
changeTypeBtns[1].SetActive(false);
changeTypeBtns[2].SetActive(false);
changeTypeBtns[3].SetActive(true);
}
else
{
newPasswordInput.characterLimit = 16;
confirmPasswordInput.characterLimit = 16;
newPasswordInput.contentType = InputField.ContentType.Password;
confirmPasswordInput.contentType = InputField.ContentType.Password;
changeTypeBtns[0].SetActive(false);
changeTypeBtns[1].SetActive(true);
changeTypeBtns[2].SetActive(true);
changeTypeBtns[3].SetActive(false);
}
passwordTypeTips[passwordType].gameObject.SetActive(true);
passwordTypeTips[1 - passwordType].gameObject.SetActive(false);
}
public void CheckPasswordFormat(string password)
{
isConfirmEnabled = true;
errorText.text = "";
if (passwordType == 0)
{
if (!ValidationUtils.IsValidSimplePassword(newPasswordInput.text))
{
errorText.text = LanguageManager.Instance.GetLanguage("100025");
isConfirmEnabled = false;
return;
}
}
else
{
if (!ValidationUtils.IsValidComplexPassword(newPasswordInput.text))
{
errorText.text = LanguageManager.Instance.GetLanguage("100029");
isConfirmEnabled = false;
return;
}
}
if (newPasswordInput.text != confirmPasswordInput.text)
{
errorText.text = LanguageManager.Instance.GetLanguage("100028");
isConfirmEnabled = false;
}
}
/// <summary>
/// 设置页面模式(注册或重置密码)
/// </summary>
public void SetMode(SetPasswordMode mode, string code = "")
{
currentMode = mode;
}
/// <summary>
/// 确认设置密码
/// </summary>
private void OnConfirm()
{
if (!isConfirmEnabled)
return;
string newPwd = newPasswordInput != null ? newPasswordInput.text : "";
string email = LoginPageCtrl.Instance.GetCurrentEmail();
if (currentMode == SetPasswordMode.Register)
{
// 注册模式 - 调用注册接口
LoginPageCtrl.Instance.Register(email, newPwd, passwordType,
onSuccess: () =>
{
// 注册成功,回到登录页
ClearHistoryAndShow(LoginPageCtrl.SubPageType.Login);
},
onError: (code, msg) =>
{
errorText.text = LanguageManager.Instance.GetLanguage(code.ToString());
}
);
}
else
{
// 重置密码模式 - 调用重置密码接口
LoginPageCtrl.Instance.ResetPassword(email, newPwd, passwordType,
onSuccess: () =>
{
// 重置成功,回到登录页
ClearHistoryAndShow(LoginPageCtrl.SubPageType.Login);
},
onError: (code, msg) =>
{
errorText.text = LanguageManager.Instance.GetLanguage(code.ToString());
}
);
}
}
}
}