| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.SceneManagement;
- using System.Text.RegularExpressions;
- public class LoginView : MonoBehaviour
- {
- //登录方式-左侧切换按钮-按钮纹理
- [SerializeField] Sprite[] loginModeSprites;
- //登录方式-左侧切换按钮
- [SerializeField] GameObject loginNormalTab;
- [SerializeField] GameObject loginPhoneTab;
- //用户登录
- [SerializeField] GameObject loginInUser;
- [SerializeField] GameObject loginInPWD;
- [SerializeField] GameObject loginInCaptcha1;
- //手机登录
- [SerializeField] GameObject loginInPhone;
- [SerializeField] GameObject loginInCode;
- [SerializeField] GameObject loginValidTime;
- [SerializeField] GameObject loginInCaptcha2;
- [SerializeField] Text loginTip;
- //协议栏
- [SerializeField] Transform agreementTF;
- int loginMode = 1;
- //状态记录
- public int captcha_Login = -222222222;
- public int captcha_LoginPhone = -222222222;
- void Start()
- {
- InitInputLimit();
- InitAgreementOnClickListeners();
- SelectLoginMode(1);
- }
- void InitInputLimit() {
- GameObject[] inputNodes = {loginInUser, loginInPWD};
- foreach (var inputNode in inputNodes) {
- InputField inputField = GetInputField(inputNode);
- inputField.onValueChanged.AddListener(delegate(string text) {
- Match match = new Regex("[^A-Za-z0-9]").Match(text);
- if (match.Success) {
- inputField.text = text.Replace(match.Value, "");
- }
- });
- }
- }
- void InitAgreementOnClickListeners() {
- agreementTF.Find("TextA").GetComponent<Button>().onClick.AddListener(delegate() {
- GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AgreementView"));
- o.GetComponent<AgreementView>().EnterUserAgreement();
- });
- agreementTF.Find("TextB").GetComponent<Button>().onClick.AddListener(delegate() {
- GameObject o = GameObject.Instantiate(Resources.Load<GameObject>("Prefabs/Views/AgreementView"));
- o.GetComponent<AgreementView>().EnterPrivacyAgreement();
- });
- }
- public void SelectLoginMode(int mode) {
- loginMode = mode;
- if (loginMode == 1) {
- loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[1];
- loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[0];
- loginInUser.SetActive(true);
- loginInPWD.SetActive(true);
- loginInCaptcha1.SetActive(true);
- loginInPhone.SetActive(false);
- loginInCode.SetActive(false);
- loginValidTime.SetActive(false);
- loginInCaptcha2.SetActive(false);
- if (captcha_Login < 0) {
- StartCoroutine(CaptchaController.ins.GetCaptcha(
- loginInCaptcha1.transform.Find("CodeImage").GetComponent<Image>(),
- (code) => { captcha_Login = code; }
- ));
- }
- }
- else if (loginMode == 2) {
- loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[0];
- loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[1];
- loginInUser.SetActive(false);
- loginInPWD.SetActive(false);
- loginInCaptcha1.SetActive(false);
- loginInPhone.SetActive(true);
- loginInCode.SetActive(true);
- loginValidTime.SetActive(true);
- loginInCaptcha2.SetActive(true);
- if (captcha_LoginPhone < 0) {
- StartCoroutine(CaptchaController.ins.GetCaptcha(
- loginInCaptcha2.transform.Find("CodeImage").GetComponent<Image>(),
- (code) => { captcha_LoginPhone = code; }
- ));
- }
- }
- loginTip.GetComponent<TextAutoLanguage>().SetText(0);
- }
- InputField GetInputField(GameObject inputNode) {
- return inputNode.transform.Find("InputField").GetComponent<InputField>();
- }
- public void Login() {
- if (loginMode == 1) {
- LoginNormal();
- } else if (loginMode == 2) {
- LoginByPhone();
- }
- }
- void LoginNormal() {
- InputField user = GetInputField(loginInUser);
- if (user.text.Trim().Length == 0) {
- loginTip.color = Color.yellow;
- loginTip.GetComponent<TextAutoLanguage>().SetText(41);
- return;
- }
- InputField pwd = GetInputField(loginInPWD);
- if (pwd.text.Trim().Length == 0) {
- loginTip.color = Color.yellow;
- loginTip.GetComponent<TextAutoLanguage>().SetText(42);
- return;
- }
- InputField captcha = GetInputField(loginInCaptcha1);
- if (!captcha.text.Equals(captcha_Login.ToString())) {
- Debug.Log("验证码错误");
- return;
- }
- if (!agreementTF.Find("Toggle").GetComponent<Toggle>().isOn) {
- Debug.Log("登录需要勾选同意App协议");
- return;
- }
- StartCoroutine(LoginController.ins.LoginNormal(
- user.text, pwd.text, (res) => {
- Debug.Log(res.msg);
- if (res.code == 0) {
- string IdAndToken = (string)res.data;
- PlayerPrefs.SetString("IdAndToken", IdAndToken);
- SceneManager.LoadScene("Home", LoadSceneMode.Single);
- }
- }
- ));
- // UserInfos userInfos = GetUserInfos();
- // foreach (var userInfo in userInfos.list)
- // {
- // if (userInfo.user == user.text) {
- // if (userInfo.pwd == pwd.text) {
- // loginTip.color = Color.green;
- // loginTip.GetComponent<TextAutoLanguage>().SetText(43);
- // myUserInfo = userInfo;
- // PlayerPrefs.SetString("LoginRecord_User_" + userInfo_version, userInfo.user);
- // PlayerPrefs.SetString("LoginRecord_PWD_" + userInfo_version, userInfo.pwd);
- //
- // } else {
- // loginTip.color = Color.red;
- // loginTip.GetComponent<TextAutoLanguage>().SetText(44);
- // }
- // return;
- // }
- // }
- // loginTip.color = Color.yellow;
- // loginTip.GetComponent<TextAutoLanguage>().SetText(45);
- }
- void LoginByPhone() {
- }
- }
|