LoginView.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using AppUI;
  2. using AppUI.Localization;
  3. using AppUI.Manager;
  4. using AppUI.View.Component;
  5. using JCUnityLib;
  6. using System.Collections;
  7. using System.Text.RegularExpressions;
  8. using TMPro;
  9. using UnityEngine;
  10. using UnityEngine.SceneManagement;
  11. using UnityEngine.UI;
  12. namespace AppUI.View
  13. {
  14. /// <summary>
  15. /// AppUI 登录页:账号 + 密码 +(可选)图形验证码,逻辑对齐旧版 <see cref="LoginView"/> 的 <c>LoginNormal</c>。
  16. /// 第三方登录(微信/Apple 等)暂不接入,见 Docs。
  17. /// </summary>
  18. public class LoginView : MonoBehaviour
  19. {
  20. [Header("输入(可留空,运行时按路径自动绑定)")]
  21. [SerializeField]
  22. TMP_InputField accountInput;
  23. [SerializeField]
  24. TMP_InputField passwordInput;
  25. [Header("验证码(可选,与旧版图形验证码一致;未绑定则跳过校验)")]
  26. [SerializeField]
  27. Image captchaDisplayImage;
  28. [SerializeField]
  29. InputField captchaLegacyInput;
  30. string _expectedCaptchaText = "";
  31. bool _captchaReady;
  32. readonly Throttler _throttlerLoginNormal = new Throttler(2000);
  33. void Awake()
  34. {
  35. TryAutoBindCaptcha();
  36. InitPasswordAlphanumericFilter();
  37. }
  38. void Start()
  39. {
  40. if (IsCaptchaConfigured())
  41. ReloadCaptcha();
  42. }
  43. /// <summary>
  44. /// 与旧版通过父节点名定位验证码一致;支持父节点未激活时的子节点查找。
  45. /// </summary>
  46. void TryAutoBindCaptcha()
  47. {
  48. if (captchaDisplayImage != null && captchaLegacyInput != null)
  49. return;
  50. foreach (var tr in GetComponentsInChildren<Transform>(true))
  51. {
  52. if (captchaDisplayImage == null && tr.name == "CodeImage")
  53. {
  54. var img = tr.GetComponent<Image>();
  55. if (img != null)
  56. captchaDisplayImage = img;
  57. }
  58. if (captchaLegacyInput == null && tr.name == "InputField" && tr.parent != null &&
  59. tr.parent.name == "_InCaptcha1")
  60. {
  61. var field = tr.GetComponent<InputField>();
  62. if (field != null)
  63. captchaLegacyInput = field;
  64. }
  65. }
  66. }
  67. void InitPasswordAlphanumericFilter()
  68. {
  69. if (passwordInput == null)
  70. return;
  71. passwordInput.onValueChanged.AddListener(text =>
  72. {
  73. var match = new Regex("[^A-Za-z0-9]").Match(text);
  74. if (match.Success)
  75. passwordInput.text = text.Replace(match.Value, "");
  76. });
  77. }
  78. bool IsCaptchaConfigured() =>
  79. captchaDisplayImage != null && captchaLegacyInput != null;
  80. /// <summary>供「换一张验证码」按钮调用,对齐旧版 <c>ChnageCaptcha1</c>。</summary>
  81. public void ReloadCaptcha()
  82. {
  83. if (!IsCaptchaConfigured())
  84. return;
  85. _captchaReady = false;
  86. StartCoroutine(CaptchaController.Instance.GetCaptcha(captchaDisplayImage, code =>
  87. {
  88. _expectedCaptchaText = code.ToString();
  89. _captchaReady = true;
  90. }));
  91. }
  92. /// <summary>对齐旧版 <c>LoginNormal</c>:账号/密码登录(不含第三方)。</summary>
  93. public void Login()
  94. {
  95. if (accountInput == null || passwordInput == null)
  96. {
  97. Debug.LogError("[AppUI.LoginView] 未绑定账号或密码 TMP_InputField。");
  98. return;
  99. }
  100. string account = accountInput.text.Trim();
  101. if (account.Length == 0)
  102. {
  103. ModuleViewMgr.ins.Show(AppUILocalization.GetTextByCNKey("请输入账号"));
  104. return;
  105. }
  106. if (CommonConfig.AppArea == 0)
  107. {
  108. if (!ValidateHelper.IsMobilePhone(account))
  109. {
  110. ModuleViewMgr.ins.Show(AppUILocalization.GetTextByKey("RelateValidateView-a1"));
  111. return;
  112. }
  113. }
  114. else
  115. {
  116. if (!ValidateHelper.IsEmail(account))
  117. {
  118. ModuleViewMgr.ins.Show(AppUILocalization.GetTextByKey("RelateValidateView-a0"));
  119. return;
  120. }
  121. }
  122. string pwd = passwordInput.text.Trim();
  123. if (pwd.Length == 0)
  124. {
  125. ModuleViewMgr.ins.Show(AppUILocalization.GetTextByCNKey("请输入密码"));
  126. return;
  127. }
  128. //if (IsCaptchaConfigured())
  129. //{
  130. // if (!_captchaReady)
  131. // {
  132. // PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("验证码错误"));
  133. // return;
  134. // }
  135. // if (!captchaLegacyInput.text.Equals(_expectedCaptchaText))
  136. // {
  137. // PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("验证码错误"));
  138. // return;
  139. // }
  140. //}
  141. //if (!AgreenmentOption.ins.IsAgreementChecked())
  142. //{
  143. // PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请阅读并同意App协议"));
  144. // return;
  145. //}
  146. if (!_throttlerLoginNormal.CanPass())
  147. {
  148. ModuleViewMgr.ins.Show(AppUILocalization.GetTextByCNKey("操作过于频繁"));
  149. return;
  150. }
  151. StartCoroutine(LoginController.Instance.LoginNormal(account, pwd, res =>
  152. {
  153. //ModuleViewMgr.ins.Show(TextAutoLanguage2.GetTextByCNKey(res.msg));
  154. //PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey(res.msg));
  155. if (res.code == 0)
  156. {
  157. string loginToken = (string)res.data;
  158. GoToHome(loginToken);
  159. }
  160. }));
  161. }
  162. void GoToHome(string loginToken)
  163. {
  164. CommonConfig.businessServerWsURL = loginToken.Split('&')[2];
  165. PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken);
  166. ScreenOrientationHelper.SwitchToLandscapeAndLoadScene("Home");
  167. }
  168. /// <summary>对齐旧版 <c>FillLoginInput</c>。</summary>
  169. public void FillLoginInput(string username, string password)
  170. {
  171. if (accountInput != null)
  172. accountInput.text = username;
  173. if (passwordInput != null)
  174. passwordInput.text = password;
  175. }
  176. }
  177. }