Files
5.24BuYu/Assets/MyGame/aa/UI/Login/SubPanel/RegisterPanel.cs
T
2026-07-13 00:12:44 +08:00

132 lines
4.4 KiB
C#

using Assets.MyGame.Scripts.Net;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class RegisterPanel : SubUIBase
{
public TMP_InputField tMPInput_UserName;
public TMP_InputField tMPInput_Password;
public TMP_InputField tMPInput_QueRenPassword;
public TMP_InputField tMPInput_SFZName;
public TMP_InputField tMPInput_SFZID;
public TMP_InputField tMPInput_YZM;
public Image img_YZM;
public Button btn_Register;
private string _realPassword = "";
private string _realQueRenPassword = "";
private Coroutine _maskCoroutine, _maskCoroutine2;
/// <summary>当前验证码(不区分大小写比对)</summary>
private string _captchaCode = "";
public override void Awake()
{
base.Awake();
// 手机号限制 11 位数字
tMPInput_UserName.characterLimit = 11;
tMPInput_UserName.contentType = TMP_InputField.ContentType.IntegerNumber;
// 身份证限制 18 位
tMPInput_SFZID.characterLimit = 18;
// 密码:Standard 模式 + 延迟遮罩
tMPInput_Password.contentType = TMP_InputField.ContentType.Standard;
tMPInput_Password.onValueChanged.AddListener(v => OnPwdChanged(v, tMPInput_Password, ref _realPassword, ref _maskCoroutine));
tMPInput_QueRenPassword.contentType = TMP_InputField.ContentType.Standard;
tMPInput_QueRenPassword.onValueChanged.AddListener(v => OnPwdChanged(v, tMPInput_QueRenPassword, ref _realQueRenPassword, ref _maskCoroutine2));
// 点击验证码图片刷新
if (img_YZM != null)
{
var yzmBtn = img_YZM.GetComponent<Button>();
if (yzmBtn == null) yzmBtn = img_YZM.gameObject.AddComponent<Button>();
yzmBtn.onClick.AddListener(RefreshCaptcha);
}
btn_Register.onClick.AddListener(() =>
{
// 校验验证码(不区分大小写)
if (string.IsNullOrEmpty(_captchaCode) ||
!string.Equals(tMPInput_YZM.text, _captchaCode, System.StringComparison.OrdinalIgnoreCase))
{
UIManager.Instance.ShowBoardError("验证码错误");
RefreshCaptcha();
return;
}
APIRequestList.Register(new Assets.MyGame.Scripts.Net.APIRequest.Objs.RegisterObj
{
mobile = tMPInput_UserName.text,
passWord = _realPassword,
realName = tMPInput_SFZName.text,
iDCard = tMPInput_SFZID.text
}, usertoke =>
{
WebSocketManager.Instance.Connect(SocketType.login);
(uIManager.parentUIBase as UILoginPanel)?.Succeed();
});
});
RefreshCaptcha();
}
/// <summary>生成并显示新的验证码图片</summary>
private void RefreshCaptcha()
{
if (img_YZM == null) return;
var sprite = CaptchaGenerator.Generate(160, 60, out _captchaCode, 4);
img_YZM.sprite = sprite;
if (tMPInput_YZM != null) tMPInput_YZM.text = "";
}
private void OnPwdChanged(string display, TMP_InputField input, ref string real, ref Coroutine cr)
{
bool isAdd = real.Length < display.Length;
if (isAdd)
{
for (int i = real.Length; i < display.Length; i++)
real += display[i];
}
else if (real.Length > display.Length)
{
real = real.Substring(0, display.Length);
}
int len = real.Length;
if (isAdd && len > 0)
{
string masked = new string('*', len - 1) + real[len - 1];
input.SetTextWithoutNotify(masked);
input.caretPosition = len;
MaskAfterDelay(input, real, ref cr);
}
else
{
input.SetTextWithoutNotify(new string('*', len));
input.caretPosition = len;
}
}
private void MaskAfterDelay(TMP_InputField input, string real, ref Coroutine cr)
{
if (cr != null) StopCoroutine(cr);
cr = StartCoroutine(MaskRoutine(input, real));
}
private IEnumerator MaskRoutine(TMP_InputField input, string real)
{
yield return new WaitForSeconds(0.5f);
if (string.IsNullOrEmpty(real)) yield break;
input.SetTextWithoutNotify(new string('*', real.Length));
input.caretPosition = real.Length;
}
}