LoginView.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. using System.Text.RegularExpressions;
  7. public class LoginView : MonoBehaviour
  8. {
  9. //登录方式-左侧切换按钮-按钮纹理
  10. [SerializeField] Sprite[] loginModeSprites;
  11. //登录方式-左侧切换按钮
  12. [SerializeField] GameObject loginNormalTab;
  13. [SerializeField] GameObject loginPhoneTab;
  14. //用户登录
  15. [SerializeField] GameObject loginInUser;
  16. [SerializeField] GameObject loginInPWD;
  17. [SerializeField] GameObject loginInCaptcha1;
  18. //手机登录
  19. [SerializeField] GameObject loginInPhone;
  20. [SerializeField] GameObject loginInCode;
  21. [SerializeField] GameObject loginValidTime;
  22. [SerializeField] GameObject loginInCaptcha2;
  23. //协议栏
  24. [SerializeField] Transform agreementTF;
  25. int loginMode = 1;
  26. //状态记录
  27. public int captcha_Login = -222222222;
  28. public int captcha_LoginPhone = -222222222;
  29. public static LoginView ins;
  30. void Awake() {
  31. ins = this;
  32. if (CommonConfig.needToExamine) {
  33. transform.Find("BtnTabSwitch/LoginPhone").gameObject.SetActive(false); //隐藏手机登录
  34. transform.Find("BtnForgetPWD").gameObject.SetActive(false); //隐藏忘记密码
  35. transform.Find("Logo/Layout/Text1").gameObject.SetActive(false);
  36. }
  37. }
  38. void Start()
  39. {
  40. InitInputLimit();
  41. InitAgreementOnClickListeners();
  42. SelectLoginMode(1);
  43. }
  44. void OnDestroy() {
  45. if (ins == this) ins = null;
  46. }
  47. void Update() {
  48. #if UNITY_EDITOR
  49. if (Input.GetKeyDown(KeyCode.Q)) {
  50. GetInputField(loginInUser).text = "lvjincheng";
  51. GetInputField(loginInPWD).text = "19980301";
  52. GetInputField(loginInCaptcha1).text = captcha_Login.ToString();
  53. LoginNormal();
  54. }
  55. if (Input.GetKeyDown(KeyCode.W)) {
  56. GetInputField(loginInUser).text = "tester";
  57. GetInputField(loginInPWD).text = "123456";
  58. GetInputField(loginInCaptcha1).text = captcha_Login.ToString();
  59. LoginNormal();
  60. }
  61. #endif
  62. }
  63. void InitInputLimit() {
  64. GameObject[] inputNodes = {loginInUser, loginInPWD};
  65. foreach (var inputNode in inputNodes) {
  66. InputField inputField = GetInputField(inputNode);
  67. inputField.onValueChanged.AddListener(delegate(string text) {
  68. Match match = new Regex("[^A-Za-z0-9]").Match(text);
  69. if (match.Success) {
  70. inputField.text = text.Replace(match.Value, "");
  71. }
  72. });
  73. }
  74. }
  75. void InitAgreementOnClickListeners() {
  76. agreementTF.Find("TextA").GetComponent<Button>().onClick.AddListener(delegate() {
  77. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AgreementView"));
  78. o.GetComponent<AgreementView>().EnterUserAgreement();
  79. });
  80. agreementTF.Find("TextB").GetComponent<Button>().onClick.AddListener(delegate() {
  81. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AgreementView"));
  82. o.GetComponent<AgreementView>().EnterPrivacyAgreement();
  83. });
  84. }
  85. public void SelectLoginMode(int mode) {
  86. loginMode = mode;
  87. if (loginMode == 1) {
  88. loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[1];
  89. loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[0];
  90. loginInUser.SetActive(true);
  91. loginInPWD.SetActive(true);
  92. loginInCaptcha1.SetActive(true);
  93. loginInPhone.SetActive(false);
  94. loginInCode.SetActive(false);
  95. loginValidTime.SetActive(false);
  96. loginInCaptcha2.SetActive(false);
  97. if (captcha_Login < 0) {
  98. ChnageCaptcha1();
  99. }
  100. }
  101. else if (loginMode == 2) {
  102. loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[0];
  103. loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[1];
  104. loginInUser.SetActive(false);
  105. loginInPWD.SetActive(false);
  106. loginInCaptcha1.SetActive(false);
  107. loginInPhone.SetActive(true);
  108. loginInCode.SetActive(true);
  109. loginValidTime.SetActive(true);
  110. loginInCaptcha2.SetActive(true);
  111. if (captcha_LoginPhone < 0) {
  112. ChnageCaptcha2();
  113. }
  114. }
  115. }
  116. InputField GetInputField(GameObject inputNode) {
  117. return inputNode.transform.Find("InputField").GetComponent<InputField>();
  118. }
  119. public void ChnageCaptcha1() {
  120. StartCoroutine(CaptchaController.ins.GetCaptcha(
  121. loginInCaptcha1.transform.Find("CodeImage").GetComponent<Image>(),
  122. (code) => { captcha_Login = code; }
  123. ));
  124. }
  125. public void ChnageCaptcha2() {
  126. StartCoroutine(CaptchaController.ins.GetCaptcha(
  127. loginInCaptcha2.transform.Find("CodeImage").GetComponent<Image>(),
  128. (code) => { captcha_LoginPhone = code; }
  129. ));
  130. }
  131. public void Login() {
  132. if (loginMode == 1) {
  133. LoginNormal();
  134. } else if (loginMode == 2) {
  135. LoginByPhone();
  136. }
  137. }
  138. JC.CS.Throttler throttlerLoginNormal = new JC.CS.Throttler(2000);
  139. void LoginNormal() {
  140. InputField user = GetInputField(loginInUser);
  141. if (user.text.Trim().Length == 0) {
  142. PopupMgr.ins.ShowTip("请输入用户名");
  143. return;
  144. }
  145. InputField pwd = GetInputField(loginInPWD);
  146. if (pwd.text.Trim().Length == 0) {
  147. PopupMgr.ins.ShowTip("请输入密码");
  148. return;
  149. }
  150. InputField captcha = GetInputField(loginInCaptcha1);
  151. if (!captcha.text.Equals(captcha_Login.ToString())) {
  152. PopupMgr.ins.ShowTip("验证码错误");
  153. return;
  154. }
  155. if (!agreementTF.Find("Toggle").GetComponent<Toggle>().isOn) {
  156. PopupMgr.ins.ShowTip("登录需要勾选同意App协议");
  157. return;
  158. }
  159. if (throttlerLoginNormal.CanPass() == false) {
  160. PopupMgr.ins.ShowTip("操作过于频繁");
  161. return;
  162. }
  163. StartCoroutine(LoginController.ins.LoginNormal(
  164. user.text, pwd.text, (res) => {
  165. PopupMgr.ins.ShowTip(res.msg);
  166. if (res.code == 0) {
  167. string IdAndToken = (string)res.data;
  168. PlayerPrefs.SetString("IdAndToken", IdAndToken);
  169. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  170. }
  171. }
  172. ));
  173. }
  174. void LoginByPhone() {
  175. }
  176. public void SetAgreementChecked(bool value) {
  177. agreementTF.Find("Toggle").GetComponent<Toggle>().isOn = value;
  178. }
  179. public void FillLoginInput(string username, string password) {
  180. GetInputField(loginInUser).text = username;
  181. GetInputField(loginInPWD).text = password;
  182. }
  183. }