HRB_LoginView.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.Text.RegularExpressions;
  6. using JCUnityLib;
  7. public class HRB_LoginView : ViewBase
  8. {
  9. GameObject loginInUser;
  10. GameObject loginInPWD;
  11. GameObject loginInCaptcha;
  12. GameObject btnLogin;
  13. GameObject btnRegister;
  14. int captcha_Login = -222222222;
  15. void Awake()
  16. {
  17. GameObject.Find("TempCanvas").transform.Find("AgreementPopup").gameObject.SetActive(true);
  18. }
  19. void Start()
  20. {
  21. loginInUser = transform.Find("InUser").gameObject;
  22. loginInPWD = transform.Find("InPWD").gameObject;
  23. loginInCaptcha = transform.Find("InCaptcha").gameObject;
  24. btnLogin = transform.Find("BtnLogin").gameObject;
  25. btnRegister = transform.Find("BtnRegister").gameObject;
  26. btnLogin.GetComponent<Button>().onClick.AddListener(LoginNormal);
  27. btnRegister.GetComponent<Button>().onClick.AddListener(ToRegisterView);
  28. InitInputLimit();
  29. ChnageCaptcha();
  30. }
  31. void Update() {
  32. #if UNITY_EDITOR
  33. if (Input.GetKeyDown(KeyCode.F1)) {
  34. GetInputField(loginInUser).text = "lvjincheng";
  35. GetInputField(loginInPWD).text = "19980301";
  36. GetInputField(loginInCaptcha).text = captcha_Login.ToString();
  37. LoginNormal();
  38. }
  39. #endif
  40. }
  41. void InitInputLimit() {
  42. GameObject[] inputNodes = {loginInUser, loginInPWD};
  43. foreach (var inputNode in inputNodes) {
  44. InputField inputField = GetInputField(inputNode);
  45. inputField.onValueChanged.AddListener(delegate(string text) {
  46. Match match = new Regex("[^A-Za-z0-9]").Match(text);
  47. if (match.Success) {
  48. inputField.text = text.Replace(match.Value, "");
  49. }
  50. });
  51. }
  52. }
  53. InputField GetInputField(GameObject inputNode) {
  54. return inputNode.transform.Find("InputField").GetComponent<InputField>();
  55. }
  56. void ChnageCaptcha() {
  57. StartCoroutine(CaptchaController.Instance.GetCaptcha(
  58. loginInCaptcha.transform.Find("CodeImage").GetComponent<Image>(),
  59. (code) => { captcha_Login = code; }
  60. ));
  61. }
  62. JCUnityLib.Throttler throttlerLoginNormal = new JCUnityLib.Throttler(2000);
  63. void LoginNormal() {
  64. InputField user = GetInputField(loginInUser);
  65. if (user.text.Trim().Length == 0) {
  66. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请输入账号"));
  67. return;
  68. }
  69. InputField pwd = GetInputField(loginInPWD);
  70. if (pwd.text.Trim().Length == 0) {
  71. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请输入密码"));
  72. return;
  73. }
  74. InputField captcha = GetInputField(loginInCaptcha);
  75. if (!captcha.text.Equals(captcha_Login.ToString())) {
  76. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("验证码错误"));
  77. return;
  78. }
  79. if (!AgreenmentOption.ins.IsAgreementChecked()) {
  80. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请阅读并同意App协议"));
  81. return;
  82. }
  83. if (throttlerLoginNormal.CanPass() == false) {
  84. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  85. return;
  86. }
  87. StartCoroutine(LoginController.Instance.LoginNormal(
  88. user.text, pwd.text, (res) => {
  89. if (res.code == 0) {
  90. string loginToken = (string)res.data;
  91. CommonConfig.businessServerWsURL = loginToken.Split('&')[2];
  92. PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken);
  93. HRB_ViewMgr.Instance.DestroyView<HRB_LoginView>();
  94. HRB_ViewMgr.Instance.ShowView<HRB_HomeView>();
  95. } else {
  96. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey(res.msg));
  97. }
  98. }
  99. ));
  100. }
  101. public void ToRegisterView()
  102. {
  103. HRB_ViewMgr.Instance.ShowView<HRB_RegisterView>();
  104. }
  105. public override void OnNotifyView(string eventName, object[] args)
  106. {
  107. if (eventName == "FillLoginInput" && args.Length == 2)
  108. {
  109. GetInputField(loginInUser).text = (string)args[0];
  110. GetInputField(loginInPWD).text = (string)args[1];
  111. }
  112. }
  113. }