LoginView.cs 6.4 KB

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