using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement; using System.Text.RegularExpressions; using Newtonsoft.Json.Linq; using JCUnityLib; using AdaptUI; /* 登录界面 */ 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; int loginMode = 1; //状态记录 public int captcha_Login = -222222222; public string captcha_Login_str = ""; public int captcha_LoginPhone = -222222222; [SerializeField] GameObject appleButton; public static LoginView ins; void Awake() { ins = this; if (CommonConfig.needToExamine) { transform.Find("BtnTabSwitch/LoginPhone").gameObject.SetActive(false); //隐藏手机登录 transform.Find("Input Contain/GameObject/BtnForgetPWD").gameObject.SetActive(false); //隐藏忘记密码 transform.Find("Logo/Layout/Text1").gameObject.SetActive(false); } if (CommonConfig.banBindRelateAccount) { transform.Find("Input Contain/GameObject/BtnForgetPWD").gameObject.SetActive(false); //隐藏忘记密码 } } void Start() { InitInputLimit(); SelectLoginMode(1); //设置苹果按钮状态 appleButton.SetActive(AppleLoginHelper.ins.bSupportedPlatform); //设置微信登录状态按钮 transform.Find("Input Contain/otherLogin/BtnWxLogin").gameObject.SetActive(CommonConfig.AppArea == 0 && WeChatLoginHelper.IsWechatInstalled()); //这里初始化时候,分别配置当前的微信AppID和UniversalLink if (WeChatLoginHelper.IsWechatInstalled()) { string applicationIdentifier = Application.identifier; // 真机下这样更稳定 if (applicationIdentifier.Contains("PadBowArrow")) { // iPad版 WeChatLoginHelper.InitWeChat("wx64d1835dbd218aec", "https://xmjssvr.cn/PadBowArrow/"); } else { // 默认普通版 WeChatLoginHelper.InitWeChat("wxfe29f3f64e3c5d16", "https://xmjssvr.cn/BowArrow/"); } } } void OnDestroy() { if (ins == this) ins = null; } void Update() { #if UNITY_EDITOR if (Input.GetKeyDown(KeyCode.F1)) { GetInputField(loginInUser).text = "lvjincheng"; GetInputField(loginInPWD).text = "19980301"; GetInputField(loginInCaptcha1).text = captcha_Login_str; LoginNormal(); } if (Input.GetKeyDown(KeyCode.F2)) { GetInputField(loginInUser).text = "tester"; GetInputField(loginInPWD).text = "123456"; GetInputField(loginInCaptcha1).text = captcha_Login_str; LoginNormal(); } #endif } void InitInputLimit() { //loginInUser GameObject[] inputNodes = {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, ""); } }); } } public void SelectLoginMode(int mode) { loginMode = mode; if (loginMode == 1) { loginNormalTab.GetComponent().sprite = loginModeSprites[1]; loginPhoneTab.GetComponent().sprite = loginModeSprites[0]; loginInUser.SetActive(true); loginInPWD.SetActive(true); loginInCaptcha1.SetActive(true); loginInPhone.SetActive(false); loginInCode.SetActive(false); loginValidTime.SetActive(false); loginInCaptcha2.SetActive(false); Transform btnRegister = transform.Find("Input Contain/buttons/BtnRegister"); Vector3 v31 = btnRegister.localPosition; v31.y = -168.5f; btnRegister.localPosition = v31; if (captcha_Login_str.Length <= 0) { ChnageCaptcha1(); } } else if (loginMode == 2) { loginNormalTab.GetComponent().sprite = loginModeSprites[0]; loginPhoneTab.GetComponent().sprite = loginModeSprites[1]; loginInUser.SetActive(false); loginInPWD.SetActive(false); loginInCaptcha1.SetActive(false); loginInPhone.SetActive(true); loginInCode.SetActive(false); loginValidTime.SetActive(false); loginInCaptcha2.SetActive(false); Transform btnInPhone = loginInPhone.transform; Vector3 v30 = btnInPhone.localPosition; v30.y = -30f; btnInPhone.localPosition = v30; Transform btnRegister = transform.Find("Input Contain/buttons/BtnRegister"); Vector3 v31 = btnRegister.localPosition; v31.y = -88; btnRegister.localPosition = v31; if (captcha_LoginPhone < 0) { ChnageCaptcha2(); } } } InputField GetInputField(GameObject inputNode) { return inputNode.transform.Find("InputField").GetComponent(); } public void ChnageCaptcha1() { StartCoroutine(CaptchaController.Instance.GetCaptcha( loginInCaptcha1.transform.parent.Find("CodeImage").GetComponent(), (code) => { captcha_Login_str = code.ToString(); } )); //captcha_Login_str = CaptchaController.Instance.GetCaptchaV2(loginInCaptcha1.transform.parent.Find("CodeImage").GetComponent()); } public void ChnageCaptcha2() { StartCoroutine(CaptchaController.Instance.GetCaptcha( loginInCaptcha2.transform.Find("CodeImage").GetComponent(), (code) => { captcha_LoginPhone = code; } )); } public void Login() { if (loginMode == 1) { LoginNormal(); } else if (loginMode == 2) { LoginByPhone(); } } JCUnityLib.Throttler throttlerLoginNormal = new JCUnityLib.Throttler(2000); void LoginNormal() { InputField user = GetInputField(loginInUser); if (user.text.Trim().Length == 0) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请输入账号")); return; } InputField pwd = GetInputField(loginInPWD); if (pwd.text.Trim().Length == 0) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请输入密码")); return; } InputField captcha = GetInputField(loginInCaptcha1); if (!captcha.text.Equals(captcha_Login_str)) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("验证码错误")); return; } if (!AgreenmentOption.ins.IsAgreementChecked()) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请阅读并同意App协议")); return; } if (throttlerLoginNormal.CanPass() == false) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁")); return; } StartCoroutine(LoginController.Instance.LoginNormal( user.text, pwd.text, (res) => { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey(res.msg)); if (res.code == 0) { string loginToken = (string)res.data; GoToHome(loginToken); } } )); } JCUnityLib.Throttler throttlerLoginWX = new JCUnityLib.Throttler(2000); public void LoginByWX() { if (!AgreenmentOption.ins.IsAgreementChecked()) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请阅读并同意App协议")); return; } if (throttlerLoginWX.CanPass() == false) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁")); return; } WeChatLoginHelper.Login(); } private string _loginToken = null; public void OnWxLoginResp(string loginCode) { if (string.IsNullOrWhiteSpace(loginCode)) return; string currentAppID = WeChatLoginHelper.CurrentAppID; transform.Find("MaskWxLogin").gameObject.SetActive(true); StartCoroutine(LoginController.Instance.LoginByWX_WithAppID(loginCode, currentAppID, (res) => { Debug.Log($"wxlogin service rescode {res.code}, msg {res.msg}"); if (res.code == 0) { JObject data = (JObject)res.data; _loginToken = data.Value("token"); bool needBindPhone = data.Value("needBindPhone"); if (needBindPhone) { transform.Find("MaskWxLogin").gameObject.SetActive(false); //打开手机绑定界面 RelateValidateView relateValidateView = Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView")) .GetComponent(); CanvasUtils.PlusSortOrder(gameObject.GetComponentInParent().gameObject, relateValidateView.gameObject, 1); relateValidateView.InitForPhone2(); relateValidateView.onValidateSuccess = (a, b, c) => { StartCoroutine(UserController.Instance.SavePhone2(_loginToken, a, b, c, (res) => { if (res.code == 0) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass1")); relateValidateView?.CloseView(); DoTweenUtil.CallDelay(0.1f, () => GoToHome(_loginToken)); } })); }; } else GoToHome(_loginToken); } else { transform.Find("MaskWxLogin").gameObject.SetActive(false); PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("wxlogin_fail")); } })); } JCUnityLib.Throttler throttlerLoginApple = new JCUnityLib.Throttler(2000); public void LoginByApple() { if (!AgreenmentOption.ins.IsAgreementChecked()) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请阅读并同意App协议")); return; } if (throttlerLoginApple.CanPass() == false) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁")); return; } //苹果登录 AppleLoginHelper.ins.SignInWithAppleButtonPressed(); } private string _loginAppleToken = null; public void OnAppleLoginResp(string _identityToken, string _email, string _fullName) { transform.Find("MaskAppleLogin").gameObject.SetActive(true); StartCoroutine(LoginController.Instance.LoginByApple(_identityToken, _email, _fullName, (res) => { Debug.Log($"LoginByApple service rescode {res.code}, msg {res.msg}"); if (res.code == 0) { JObject data = (JObject)res.data; _loginAppleToken = data.Value("token"); bool needBindPhone = data.Value("needBindPhone"); if (needBindPhone) { transform.Find("MaskAppleLogin").gameObject.SetActive(false); //打开手机绑定界面 RelateValidateView relateValidateView = Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView")) .GetComponent(); CanvasUtils.PlusSortOrder(gameObject.GetComponentInParent().gameObject, relateValidateView.gameObject, 1); relateValidateView.InitForPhone2(); relateValidateView.onValidateSuccess = (a, b, c) => { StartCoroutine(UserController.Instance.SavePhone2(_loginAppleToken, a, b, c, (res) => { if (res.code == 0) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass1")); relateValidateView?.CloseView(); DoTweenUtil.CallDelay(0.1f, () => GoToHome(_loginAppleToken)); } })); }; } else GoToHome(_loginAppleToken); } else { transform.Find("MaskAppleLogin").gameObject.SetActive(false); PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("appleLogin_fail")); AppleLoginHelper.ins.DeleteAppleKey(); } })); } public void OnAppleLoginError() { transform.Find("MaskAppleLogin").gameObject.SetActive(false); PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("appleLogin_fail")); } private void GoToHome(string loginToken) { CommonConfig.businessServerWsURL = loginToken.Split('&')[2]; PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken); SceneManager.LoadScene("Home", LoadSceneMode.Single); } void LoginByPhone() { InputField user = GetInputField(loginInPhone); bool isMobilePhone = ValidateHelper.IsMobilePhone(user.text); if (!isMobilePhone) { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("手机号格式不正确")); return; } StartCoroutine(LoginController.Instance.LoginByPhone( user.text, (res) => { PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey(res.msg)); if (res.code == 0) { string loginToken = (string)res.data; GoToHome(loginToken); } } )); } public void FillLoginInput(string username, string password) { GetInputField(loginInUser).text = username; GetInputField(loginInPWD).text = password; } }