1. 新增加速度传感器设备不稳定错误提示 2. 新增多套密码校验规则与多语言文案 3. 重构个人页面与密码设置面板的校验逻辑 4. 优化设备状态错误展示与UI布局 5. 新增重置密码相关的UI资源与逻辑
279 lines
11 KiB
C#
279 lines
11 KiB
C#
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using Kill.Utils;
|
||
using Kill.Managers;
|
||
using System.Threading.Tasks;
|
||
using System;
|
||
using System.Text.RegularExpressions;
|
||
using UnityEngine.WSA;
|
||
using Kill.UI.Components;
|
||
|
||
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;
|
||
/// <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;
|
||
|
||
private int passwordType = 0;
|
||
private bool isConfirmEnabled = false;
|
||
|
||
// 当前模式
|
||
private SetPasswordMode currentMode = SetPasswordMode.Register;
|
||
|
||
/// <summary>
|
||
/// 设置密码模式
|
||
/// </summary>
|
||
public enum SetPasswordMode
|
||
{
|
||
Register, // 注册设置密码
|
||
ResetPassword // 忘记密码重置密码
|
||
}
|
||
void Start()
|
||
{
|
||
OnInitialize();
|
||
OnShow();
|
||
}
|
||
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);
|
||
}
|
||
protected override void OnShow()
|
||
{
|
||
// 页面显示时初始化密码输入框
|
||
newPasswordInput.text = "";
|
||
confirmPasswordInput.text = "";
|
||
ChangePasswordType(0);
|
||
}
|
||
|
||
private void OnPasswordValueChanged(string value)
|
||
{
|
||
CheckPasswordRules();
|
||
UpdateSubmitButton();
|
||
}
|
||
|
||
/// <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();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新规则容器可见性
|
||
/// </summary>
|
||
private void UpdateRulesContainerVisibility()
|
||
{
|
||
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)
|
||
{
|
||
foreach (var image in rules)
|
||
{
|
||
if (image != null)
|
||
image.sprite = incorrectIconSprite;
|
||
}
|
||
}
|
||
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 : "";
|
||
|
||
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);
|
||
}
|
||
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)
|
||
{
|
||
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;
|
||
}
|
||
}
|
||
|
||
// 两条密码一致
|
||
bool matchValid = !string.IsNullOrEmpty(newPwd) && newPwd == confirmPwd;
|
||
var matchRule = passwordType == 0 ? simpleConfirmMatchRule : complexConfirmMatchRule;
|
||
if (matchRule != null)
|
||
matchRule.sprite = matchValid ? correctIconSprite : incorrectIconSprite;
|
||
|
||
isConfirmEnabled = pwdRulesValid && matchValid;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新提交按钮显示状态
|
||
/// </summary>
|
||
private void UpdateSubmitButton()
|
||
{
|
||
if (confirmBtn != null)
|
||
confirmBtn.gameObject.SetActive(isConfirmEnabled);
|
||
if (confirmBtnDisabled != null)
|
||
confirmBtnDisabled.SetActive(!isConfirmEnabled);
|
||
}
|
||
|
||
/// <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: () =>
|
||
{
|
||
UIManager.Instance.OpenPage(UIManager.PageName.homePage,null,true);
|
||
},
|
||
onError: (code, msg) =>
|
||
{
|
||
ToastUI.ShowText(code.ToString());
|
||
}
|
||
);
|
||
}
|
||
else
|
||
{
|
||
// 重置密码模式 - 调用重置密码接口
|
||
LoginPageCtrl.Instance.ResetPassword(email, newPwd, passwordType,
|
||
onSuccess: () =>
|
||
{
|
||
// 重置成功,回到登录页
|
||
ClearHistoryAndShow(LoginPageCtrl.SubPageType.Login);
|
||
},
|
||
onError: (code, msg) =>
|
||
{
|
||
ToastUI.ShowText(code.ToString());
|
||
}
|
||
);
|
||
}
|
||
}
|
||
}
|
||
}
|