LoginView.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. [SerializeField] Text loginTip;
  24. //协议栏
  25. [SerializeField] Transform agreementTF;
  26. int loginMode = 1;
  27. void Start()
  28. {
  29. InitInputLimit();
  30. InitAgreementOnClickListeners();
  31. SelectLoginMode(1);
  32. }
  33. void InitInputLimit() {
  34. GameObject[] inputNodes = {loginInUser, loginInPWD};
  35. foreach (var inputNode in inputNodes) {
  36. InputField inputField = GetInputField(inputNode);
  37. inputField.onValueChanged.AddListener(delegate(string text) {
  38. Match match = new Regex("[^A-Za-z0-9]").Match(text);
  39. if (match.Success) {
  40. inputField.text = text.Replace(match.Value, "");
  41. }
  42. });
  43. }
  44. }
  45. void InitAgreementOnClickListeners() {
  46. agreementTF.Find("TextA").GetComponent<Button>().onClick.AddListener(delegate() {
  47. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AgreementView"));
  48. o.GetComponent<AgreementView>().EnterUserAgreement();
  49. });
  50. agreementTF.Find("TextB").GetComponent<Button>().onClick.AddListener(delegate() {
  51. GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AgreementView"));
  52. o.GetComponent<AgreementView>().EnterPrivacyAgreement();
  53. });
  54. }
  55. public void SelectLoginMode(int mode) {
  56. loginMode = mode;
  57. if (loginMode == 1) {
  58. loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[1];
  59. loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[0];
  60. loginInUser.SetActive(true);
  61. loginInPWD.SetActive(true);
  62. loginInCaptcha1.SetActive(true);
  63. loginInPhone.SetActive(false);
  64. loginInCode.SetActive(false);
  65. loginValidTime.SetActive(false);
  66. loginInCaptcha2.SetActive(false);
  67. if (CaptchaController.ins.captcha_Login < 0) {
  68. StartCoroutine(CaptchaController.ins.GetCaptcha(
  69. CaptchaController.CaptchaType.Login,
  70. loginInCaptcha1.transform.Find("CodeImage").GetComponent<Image>()
  71. ));
  72. }
  73. }
  74. else if (loginMode == 2) {
  75. loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[0];
  76. loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[1];
  77. loginInUser.SetActive(false);
  78. loginInPWD.SetActive(false);
  79. loginInCaptcha1.SetActive(false);
  80. loginInPhone.SetActive(true);
  81. loginInCode.SetActive(true);
  82. loginValidTime.SetActive(true);
  83. loginInCaptcha2.SetActive(true);
  84. if (CaptchaController.ins.captcha_LoginPhone < 0) {
  85. StartCoroutine(CaptchaController.ins.GetCaptcha(
  86. CaptchaController.CaptchaType.LoginPhone,
  87. loginInCaptcha2.transform.Find("CodeImage").GetComponent<Image>()
  88. ));
  89. }
  90. }
  91. loginTip.GetComponent<TextAutoLanguage>().SetText(0);
  92. }
  93. InputField GetInputField(GameObject inputNode) {
  94. return inputNode.transform.Find("InputField").GetComponent<InputField>();
  95. }
  96. public void Login() {
  97. if (loginMode == 1) {
  98. LoginNormal();
  99. } else if (loginMode == 2) {
  100. LoginByPhone();
  101. }
  102. }
  103. void LoginNormal() {
  104. InputField user = GetInputField(loginInUser);
  105. if (user.text.Trim().Length == 0) {
  106. loginTip.color = Color.yellow;
  107. loginTip.GetComponent<TextAutoLanguage>().SetText(41);
  108. return;
  109. }
  110. InputField pwd = GetInputField(loginInPWD);
  111. if (pwd.text.Trim().Length == 0) {
  112. loginTip.color = Color.yellow;
  113. loginTip.GetComponent<TextAutoLanguage>().SetText(42);
  114. return;
  115. }
  116. InputField captcha = GetInputField(loginInCaptcha1);
  117. if (!captcha.text.Equals(CaptchaController.ins.captcha_Login.ToString())) {
  118. Debug.Log("验证码错误");
  119. return;
  120. }
  121. if (!agreementTF.Find("Toggle").GetComponent<Toggle>().isOn) {
  122. Debug.Log("登录需要勾选同意App协议");
  123. return;
  124. }
  125. StartCoroutine(LoginController.ins.LoginNormal(
  126. user.text, pwd.text, (res) => {
  127. Debug.Log(res.msg);
  128. if (res.code == 0) {
  129. string IdAndToken = (string)res.data;
  130. PlayerPrefs.SetString("IdAndToken", IdAndToken);
  131. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  132. }
  133. }
  134. ));
  135. // UserInfos userInfos = GetUserInfos();
  136. // foreach (var userInfo in userInfos.list)
  137. // {
  138. // if (userInfo.user == user.text) {
  139. // if (userInfo.pwd == pwd.text) {
  140. // loginTip.color = Color.green;
  141. // loginTip.GetComponent<TextAutoLanguage>().SetText(43);
  142. // myUserInfo = userInfo;
  143. // PlayerPrefs.SetString("LoginRecord_User_" + userInfo_version, userInfo.user);
  144. // PlayerPrefs.SetString("LoginRecord_PWD_" + userInfo_version, userInfo.pwd);
  145. //
  146. // } else {
  147. // loginTip.color = Color.red;
  148. // loginTip.GetComponent<TextAutoLanguage>().SetText(44);
  149. // }
  150. // return;
  151. // }
  152. // }
  153. // loginTip.color = Color.yellow;
  154. // loginTip.GetComponent<TextAutoLanguage>().SetText(45);
  155. }
  156. void LoginByPhone() {
  157. }
  158. }