killapp/Assets/Scripts/UI/Pages/SelfPage/SelfSetPasswordPanel.cs

191 lines
6.3 KiB
C#
Raw Normal View History

2026-06-08 08:55:10 +08:00
using UnityEngine;
using UnityEngine.UI;
using Kill.Utils;
using Kill.Managers;
using System.Threading.Tasks;
using System;
using Kill.UI.Components;
using Kill.Network;
namespace Kill.UI.Pages
{
/// <summary>
/// 设置密码页面
/// 用于注册设置密码和忘记密码重置密码
/// </summary>
public class SelfSetPasswordPanel : MonoBehaviour
{
[Header("输入组件")]
public InputField newPasswordInput;
public InputField confirmPasswordInput;
[Header("显示组件")]
public Text errorText;
/// <summary>
/// 密码类型提示文本0简单密码1复杂密码
/// </summary>
public Text[] passwordTypeTips;
[Header("按钮")]
public Button confirmBtn;
public Button backBtn;
private int passwordType = 0;
private bool isConfirmEnabled = false;
Action callback;
public void Init(int type, Action callback)
{
passwordType = type;
this.callback = callback;
if (confirmBtn != null)
{
confirmBtn.onClick.RemoveAllListeners();
confirmBtn.onClick.AddListener(OnConfirm);
}
if (backBtn != null)
{
backBtn.onClick.RemoveAllListeners();
backBtn.onClick.AddListener(OnBack);
}
newPasswordInput.onEndEdit.AddListener(CheckPasswordFormat);
confirmPasswordInput.onEndEdit.AddListener(CheckPasswordFormat);
errorText.text = "";
// 页面显示时初始化密码输入框
newPasswordInput.text = "";
confirmPasswordInput.text = "";
errorText.text = "";
ChangePasswordType(type);
}
/// <summary>
/// 切换密码类型
/// </summary>
/// <param name="type">密码类型0简单密码1复杂密码</param>
private void ChangePasswordType(int type)
{
errorText.text = "";
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;
}
else
{
newPasswordInput.characterLimit = 16;
confirmPasswordInput.characterLimit = 16;
newPasswordInput.contentType = InputField.ContentType.Password;
confirmPasswordInput.contentType = InputField.ContentType.Password;
}
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>
private void OnConfirm()
{
if (!isConfirmEnabled)
return;
string newPwd = newPasswordInput != null ? newPasswordInput.text : "";
string email = DataManager.Instance.userInfo.email;
// 重置密码模式 - 调用重置密码接口
ResetPassword(email, newPwd, passwordType,
onSuccess: () =>
{
if (passwordType == 0)
{
DataManager.Instance.userInfo.has_simple_password = true;
}
else
{
DataManager.Instance.userInfo.has_complex_password = true;
}
callback?.Invoke();
},
onError: (code, msg) =>
{
errorText.text = code.ToString();
}
);
}
public void OnBack()
{
UIManager.Instance.RegisterBackAction(GetComponentInParent<SelfInfoPage>().OnBack);
Destroy(gameObject);
}
/// <summary>
/// 重置密码(忘记密码)
/// </summary>
public async void ResetPassword(string email, string password, int password_type, Action onSuccess, Action<int, string> onError)
{
LoadingUI.Show();
var requestData = new ResetPasswordRequest
{
email = email,
password = password,
password_type = password_type
};
try
{
var response = await NetworkCtrl.Instance.Post<NoDataResponse>("/api/v1/auth/password/reset", 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);
}
}
}
}