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

279 lines
11 KiB
C#
Raw Normal View History

2026-04-16 14:57:19 +08:00
using UnityEngine;
using UnityEngine.UI;
using Kill.Utils;
using Kill.Managers;
using System.Text.RegularExpressions;
using Kill.UI.Components;
2026-04-16 14:57:19 +08:00
namespace Kill.UI.Pages
{
/// <summary>
/// 设置密码页面
/// 用于注册设置密码和忘记密码重置密码
/// </summary>
public class SetPasswordPanel : LoginSubPageBase
{
public InputField newPasswordInput;
public InputField confirmPasswordInput;
/// <summary>
/// 密码类型提示文本0简单密码1复杂密码
/// </summary>
public Text[] passwordTypeTips;
public Button confirmBtn;
public GameObject confirmBtnDisabled;
2026-04-16 14:57:19 +08:00
/// <summary>
/// 切换简单/复杂密码按钮 0简单密码选中 1简单密码未选中 2复杂密码选中 3复杂密码未选中 未选中可点击
/// </summary>
public GameObject[] changeTypeBtns;
[Header("密码规则验证")]
public Sprite correctIconSprite;
public Sprite incorrectIconSprite;
public Image[] simplePasswordRules;
public Image[] complexPasswordRules;
public Image simpleConfirmMatchRule;
public Image complexConfirmMatchRule;
public GameObject simpleRulesContainer;
public GameObject complexRulesContainer;
2026-04-16 14:57:19 +08:00
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.onValueChanged.AddListener(OnPasswordValueChanged);
confirmPasswordInput.onValueChanged.AddListener(OnPasswordValueChanged);
2026-04-16 14:57:19 +08:00
}
protected override void OnShow()
{
// 页面显示时初始化密码输入框
newPasswordInput.text = "";
confirmPasswordInput.text = "";
ChangePasswordType(0);
}
private void OnPasswordValueChanged(string value)
{
CheckPasswordRules();
UpdateSubmitButton();
}
2026-04-16 14:57:19 +08:00
/// <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);
// 切换规则容器并重置显示
UpdateRulesContainerVisibility();
ResetRulesDisplay();
UpdateSubmitButton();
2026-04-16 14:57:19 +08:00
}
/// <summary>
/// 更新规则容器可见性
/// </summary>
private void UpdateRulesContainerVisibility()
2026-04-16 14:57:19 +08:00
{
if (simpleRulesContainer != null)
simpleRulesContainer.SetActive(passwordType == 0);
if (complexRulesContainer != null)
complexRulesContainer.SetActive(passwordType == 1);
}
/// <summary>
/// 重置规则显示为未通过状态
/// </summary>
private void ResetRulesDisplay()
{
var rules = passwordType == 0 ? simplePasswordRules : complexPasswordRules;
if (rules != null)
2026-04-16 14:57:19 +08:00
{
foreach (var image in rules)
2026-04-16 14:57:19 +08:00
{
if (image != null)
image.sprite = incorrectIconSprite;
2026-04-16 14:57:19 +08:00
}
}
var matchRule = passwordType == 0 ? simpleConfirmMatchRule : complexConfirmMatchRule;
if (matchRule != null)
matchRule.sprite = incorrectIconSprite;
}
/// <summary>
/// 逐条检查密码规则并更新UI图标
/// </summary>
private void CheckPasswordRules()
{
string newPwd = newPasswordInput != null ? newPasswordInput.text : "";
string confirmPwd = confirmPasswordInput != null ? confirmPasswordInput.text : "";
// 密码为空时,所有规则显示错误
if (string.IsNullOrEmpty(newPwd))
{
ResetRulesDisplay();
isConfirmEnabled = false;
return;
}
bool pwdRulesValid = false;
if (passwordType == 0)
{
// 简单密码 - 1条规则6位数字
bool isValid = ValidationUtils.IsValidSimplePassword(newPwd);
pwdRulesValid = isValid;
if (simplePasswordRules != null && simplePasswordRules.Length > 0 && simplePasswordRules[0] != null)
simplePasswordRules[0].sprite = isValid ? correctIconSprite : incorrectIconSprite;
Debug.Log(isValid);
}
2026-04-16 14:57:19 +08:00
else
{
// 复杂密码 - 4条规则
// 规则1长度8-16位不能有空格
bool rule1Valid = newPwd.Length >= 8 && newPwd.Length <= 16 && !newPwd.Contains(" ");
// 规则2至少包含大写字母、小写字母、数字、特殊字符中的三种
bool hasUpper = Regex.IsMatch(newPwd, @"[A-Z]");
bool hasLower = Regex.IsMatch(newPwd, @"[a-z]");
bool hasDigit = Regex.IsMatch(newPwd, @"\d");
bool hasSpecial = !string.IsNullOrEmpty(newPwd) && Regex.IsMatch(newPwd, @"[!@#$%^&*()_+\-=\[\]{};':""\\|,.<>\/?]|\p{P}");
int typeCount = (hasUpper ? 1 : 0) + (hasLower ? 1 : 0) + (hasDigit ? 1 : 0) + (hasSpecial ? 1 : 0);
bool rule2Valid = typeCount >= 3;
// 规则3不能包含连续或重复的三个及以上的数字或字母大小写不敏感
bool rule3Valid = ValidationUtils.IsValidNoRepeatOrConsecutive(newPwd);
// 规则4密码不能包含邮箱中任意连续两个字符
string email = LoginPageCtrl.Instance.GetCurrentEmail();
bool rule4Valid = ValidationUtils.IsValidNotEmailPart(newPwd, email);
pwdRulesValid = rule1Valid && rule2Valid && rule3Valid && rule4Valid;
if (complexPasswordRules != null && complexPasswordRules.Length >= 4)
2026-04-16 14:57:19 +08:00
{
if (complexPasswordRules[0] != null) complexPasswordRules[0].sprite = rule1Valid ? correctIconSprite : incorrectIconSprite;
if (complexPasswordRules[1] != null) complexPasswordRules[1].sprite = rule2Valid ? correctIconSprite : incorrectIconSprite;
if (complexPasswordRules[2] != null) complexPasswordRules[2].sprite = rule3Valid ? correctIconSprite : incorrectIconSprite;
if (complexPasswordRules[3] != null) complexPasswordRules[3].sprite = rule4Valid ? correctIconSprite : incorrectIconSprite;
2026-04-16 14:57:19 +08:00
}
}
// 两条密码一致
bool matchValid = !string.IsNullOrEmpty(newPwd) && newPwd == confirmPwd;
var matchRule = passwordType == 0 ? simpleConfirmMatchRule : complexConfirmMatchRule;
if (matchRule != null)
matchRule.sprite = matchValid ? correctIconSprite : incorrectIconSprite;
isConfirmEnabled = pwdRulesValid && matchValid;
}
2026-04-16 14:57:19 +08:00
/// <summary>
/// 更新提交按钮显示状态
/// </summary>
private void UpdateSubmitButton()
{
if (confirmBtn != null)
confirmBtn.gameObject.SetActive(isConfirmEnabled);
if (confirmBtnDisabled != null)
confirmBtnDisabled.SetActive(!isConfirmEnabled);
2026-04-16 14:57:19 +08:00
}
2026-04-16 14:57:19 +08:00
/// <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: () =>
{
2026-04-24 16:57:44 +08:00
UIManager.Instance.OpenPage(UIManager.PageName.homePage,null,true);
2026-04-16 14:57:19 +08:00
},
onError: (code, msg) =>
{
ToastUI.ShowText(code.ToString());
2026-04-16 14:57:19 +08:00
}
);
}
else
{
// 重置密码模式 - 调用重置密码接口
LoginPageCtrl.Instance.ResetPassword(email, newPwd, passwordType,
onSuccess: () =>
{
// 重置成功,回到登录页
ClearHistoryAndShow(LoginPageCtrl.SubPageType.Login);
},
onError: (code, msg) =>
{
ToastUI.ShowText(code.ToString());
2026-04-16 14:57:19 +08:00
}
);
}
}
}
}