LoginView.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /* 登录界面 */
  8. public class LoginView : MonoBehaviour
  9. {
  10. //登录方式-左侧切换按钮-按钮纹理
  11. [SerializeField] Sprite[] loginModeSprites;
  12. //登录方式-左侧切换按钮
  13. [SerializeField] GameObject loginNormalTab;
  14. [SerializeField] GameObject loginPhoneTab;
  15. //用户登录
  16. [SerializeField] GameObject loginInUser;
  17. [SerializeField] GameObject loginInPWD;
  18. [SerializeField] GameObject loginInCaptcha1;
  19. //手机登录
  20. [SerializeField] GameObject loginInPhone;
  21. [SerializeField] GameObject loginInCode;
  22. [SerializeField] GameObject loginValidTime;
  23. [SerializeField] GameObject loginInCaptcha2;
  24. int loginMode = 1;
  25. //状态记录
  26. public int captcha_Login = -222222222;
  27. public int captcha_LoginPhone = -222222222;
  28. public static LoginView ins;
  29. void Awake() {
  30. ins = this;
  31. if (CommonConfig.needToExamine) {
  32. transform.Find("BtnTabSwitch/LoginPhone").gameObject.SetActive(false); //隐藏手机登录
  33. transform.Find("BtnForgetPWD").gameObject.SetActive(false); //隐藏忘记密码
  34. transform.Find("Logo/Layout/Text1").gameObject.SetActive(false);
  35. }
  36. if (CommonConfig.banBindRelateAccount)
  37. {
  38. transform.Find("BtnForgetPWD").gameObject.SetActive(false); //隐藏忘记密码
  39. }
  40. }
  41. void Start()
  42. {
  43. InitInputLimit();
  44. SelectLoginMode(1);
  45. }
  46. void OnDestroy() {
  47. if (ins == this) ins = null;
  48. }
  49. void Update() {
  50. #if UNITY_EDITOR
  51. if (Input.GetKeyDown(KeyCode.F1)) {
  52. GetInputField(loginInUser).text = "lvjincheng";
  53. GetInputField(loginInPWD).text = "19980301";
  54. GetInputField(loginInCaptcha1).text = captcha_Login.ToString();
  55. LoginNormal();
  56. }
  57. if (Input.GetKeyDown(KeyCode.F2)) {
  58. GetInputField(loginInUser).text = "tester";
  59. GetInputField(loginInPWD).text = "123456";
  60. GetInputField(loginInCaptcha1).text = captcha_Login.ToString();
  61. LoginNormal();
  62. }
  63. #endif
  64. }
  65. void InitInputLimit() {
  66. GameObject[] inputNodes = {loginInUser, loginInPWD};
  67. foreach (var inputNode in inputNodes) {
  68. InputField inputField = GetInputField(inputNode);
  69. inputField.onValueChanged.AddListener(delegate(string text) {
  70. Match match = new Regex("[^A-Za-z0-9]").Match(text);
  71. if (match.Success) {
  72. inputField.text = text.Replace(match.Value, "");
  73. }
  74. });
  75. }
  76. }
  77. public void SelectLoginMode(int mode) {
  78. loginMode = mode;
  79. if (loginMode == 1) {
  80. loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[1];
  81. loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[0];
  82. loginInUser.SetActive(true);
  83. loginInPWD.SetActive(true);
  84. loginInCaptcha1.SetActive(true);
  85. loginInPhone.SetActive(false);
  86. loginInCode.SetActive(false);
  87. loginValidTime.SetActive(false);
  88. loginInCaptcha2.SetActive(false);
  89. Transform btnRegister = transform.Find("BtnRegister");
  90. Vector3 v31 = btnRegister.localPosition; v31.y = -168.5f;
  91. btnRegister.localPosition = v31;
  92. if (captcha_Login < 0) {
  93. ChnageCaptcha1();
  94. }
  95. }
  96. else if (loginMode == 2) {
  97. loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[0];
  98. loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[1];
  99. loginInUser.SetActive(false);
  100. loginInPWD.SetActive(false);
  101. loginInCaptcha1.SetActive(false);
  102. loginInPhone.SetActive(true);
  103. loginInCode.SetActive(false);
  104. loginValidTime.SetActive(false);
  105. loginInCaptcha2.SetActive(false);
  106. Transform btnInPhone = loginInPhone.transform;
  107. Vector3 v30 = btnInPhone.localPosition; v30.y = -30f;
  108. btnInPhone.localPosition = v30;
  109. Transform btnRegister = transform.Find("BtnRegister");
  110. Vector3 v31 = btnRegister.localPosition; v31.y = -88;
  111. btnRegister.localPosition = v31;
  112. if (captcha_LoginPhone < 0) {
  113. ChnageCaptcha2();
  114. }
  115. }
  116. }
  117. InputField GetInputField(GameObject inputNode) {
  118. return inputNode.transform.Find("InputField").GetComponent<InputField>();
  119. }
  120. public void ChnageCaptcha1() {
  121. StartCoroutine(CaptchaController.Instance.GetCaptcha(
  122. loginInCaptcha1.transform.Find("CodeImage").GetComponent<Image>(),
  123. (code) => { captcha_Login = code; }
  124. ));
  125. }
  126. public void ChnageCaptcha2() {
  127. StartCoroutine(CaptchaController.Instance.GetCaptcha(
  128. loginInCaptcha2.transform.Find("CodeImage").GetComponent<Image>(),
  129. (code) => { captcha_LoginPhone = code; }
  130. ));
  131. }
  132. public void Login() {
  133. if (loginMode == 1) {
  134. LoginNormal();
  135. } else if (loginMode == 2) {
  136. LoginByPhone();
  137. }
  138. }
  139. JCUnityLib.Throttler throttlerLoginNormal = new JCUnityLib.Throttler(2000);
  140. void LoginNormal() {
  141. InputField user = GetInputField(loginInUser);
  142. if (user.text.Trim().Length == 0) {
  143. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请输入账号"));
  144. return;
  145. }
  146. InputField pwd = GetInputField(loginInPWD);
  147. if (pwd.text.Trim().Length == 0) {
  148. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请输入密码"));
  149. return;
  150. }
  151. InputField captcha = GetInputField(loginInCaptcha1);
  152. if (!captcha.text.Equals(captcha_Login.ToString())) {
  153. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("验证码错误"));
  154. return;
  155. }
  156. if (!AgreenmentOption.ins.IsAgreementChecked()) {
  157. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请阅读并同意App协议"));
  158. return;
  159. }
  160. if (throttlerLoginNormal.CanPass() == false) {
  161. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  162. return;
  163. }
  164. StartCoroutine(LoginController.Instance.LoginNormal(
  165. user.text, pwd.text, (res) => {
  166. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey(res.msg));
  167. if (res.code == 0) {
  168. string loginToken = (string)res.data;
  169. CommonConfig.businessServerWsURL = loginToken.Split('&')[2];
  170. PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken);
  171. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  172. }
  173. }
  174. ));
  175. }
  176. void LoginByPhone() {
  177. InputField user = GetInputField(loginInPhone);
  178. Regex regex = new Regex(@"^(((13[0-9]{1})|(15[0-35-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$");
  179. bool isMobilePhone = regex.IsMatch(user.text);
  180. if (!isMobilePhone) {
  181. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("手机号格式不正确"));
  182. return;
  183. }
  184. StartCoroutine(LoginController.Instance.LoginByPhone(
  185. user.text, (res) => {
  186. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey(res.msg));
  187. if (res.code == 0) {
  188. string loginToken = (string)res.data;
  189. CommonConfig.businessServerWsURL = loginToken.Split('&')[2];
  190. PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken);
  191. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  192. }
  193. }
  194. ));
  195. }
  196. public void FillLoginInput(string username, string password) {
  197. GetInputField(loginInUser).text = username;
  198. GetInputField(loginInPWD).text = password;
  199. }
  200. }