LoginView.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. using Newtonsoft.Json.Linq;
  8. using JCUnityLib;
  9. /* 登录界面 */
  10. public class LoginView : MonoBehaviour
  11. {
  12. //登录方式-左侧切换按钮-按钮纹理
  13. [SerializeField] Sprite[] loginModeSprites;
  14. //登录方式-左侧切换按钮
  15. [SerializeField] GameObject loginNormalTab;
  16. [SerializeField] GameObject loginPhoneTab;
  17. //用户登录
  18. [SerializeField] GameObject loginInUser;
  19. [SerializeField] GameObject loginInPWD;
  20. [SerializeField] GameObject loginInCaptcha1;
  21. //手机登录
  22. [SerializeField] GameObject loginInPhone;
  23. [SerializeField] GameObject loginInCode;
  24. [SerializeField] GameObject loginValidTime;
  25. [SerializeField] GameObject loginInCaptcha2;
  26. int loginMode = 1;
  27. //状态记录
  28. public int captcha_Login = -222222222;
  29. public int captcha_LoginPhone = -222222222;
  30. public static LoginView ins;
  31. void Awake() {
  32. ins = this;
  33. if (CommonConfig.needToExamine) {
  34. transform.Find("BtnTabSwitch/LoginPhone").gameObject.SetActive(false); //隐藏手机登录
  35. transform.Find("BtnForgetPWD").gameObject.SetActive(false); //隐藏忘记密码
  36. transform.Find("Logo/Layout/Text1").gameObject.SetActive(false);
  37. }
  38. if (CommonConfig.banBindRelateAccount)
  39. {
  40. transform.Find("BtnForgetPWD").gameObject.SetActive(false); //隐藏忘记密码
  41. }
  42. transform.Find("BtnWxLogin").gameObject.SetActive(CommonConfig.AppArea == 0);
  43. }
  44. void Start()
  45. {
  46. InitInputLimit();
  47. SelectLoginMode(1);
  48. }
  49. void OnDestroy() {
  50. if (ins == this) ins = null;
  51. }
  52. void Update() {
  53. #if UNITY_EDITOR
  54. if (Input.GetKeyDown(KeyCode.F1)) {
  55. GetInputField(loginInUser).text = "lvjincheng";
  56. GetInputField(loginInPWD).text = "19980301";
  57. GetInputField(loginInCaptcha1).text = captcha_Login.ToString();
  58. LoginNormal();
  59. }
  60. if (Input.GetKeyDown(KeyCode.F2)) {
  61. GetInputField(loginInUser).text = "tester";
  62. GetInputField(loginInPWD).text = "123456";
  63. GetInputField(loginInCaptcha1).text = captcha_Login.ToString();
  64. LoginNormal();
  65. }
  66. #endif
  67. }
  68. void InitInputLimit() {
  69. GameObject[] inputNodes = {loginInUser, loginInPWD};
  70. foreach (var inputNode in inputNodes) {
  71. InputField inputField = GetInputField(inputNode);
  72. inputField.onValueChanged.AddListener(delegate(string text) {
  73. Match match = new Regex("[^A-Za-z0-9]").Match(text);
  74. if (match.Success) {
  75. inputField.text = text.Replace(match.Value, "");
  76. }
  77. });
  78. }
  79. }
  80. public void SelectLoginMode(int mode) {
  81. loginMode = mode;
  82. if (loginMode == 1) {
  83. loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[1];
  84. loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[0];
  85. loginInUser.SetActive(true);
  86. loginInPWD.SetActive(true);
  87. loginInCaptcha1.SetActive(true);
  88. loginInPhone.SetActive(false);
  89. loginInCode.SetActive(false);
  90. loginValidTime.SetActive(false);
  91. loginInCaptcha2.SetActive(false);
  92. Transform btnRegister = transform.Find("BtnRegister");
  93. Vector3 v31 = btnRegister.localPosition; v31.y = -168.5f;
  94. btnRegister.localPosition = v31;
  95. if (captcha_Login < 0) {
  96. ChnageCaptcha1();
  97. }
  98. }
  99. else if (loginMode == 2) {
  100. loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[0];
  101. loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[1];
  102. loginInUser.SetActive(false);
  103. loginInPWD.SetActive(false);
  104. loginInCaptcha1.SetActive(false);
  105. loginInPhone.SetActive(true);
  106. loginInCode.SetActive(false);
  107. loginValidTime.SetActive(false);
  108. loginInCaptcha2.SetActive(false);
  109. Transform btnInPhone = loginInPhone.transform;
  110. Vector3 v30 = btnInPhone.localPosition; v30.y = -30f;
  111. btnInPhone.localPosition = v30;
  112. Transform btnRegister = transform.Find("BtnRegister");
  113. Vector3 v31 = btnRegister.localPosition; v31.y = -88;
  114. btnRegister.localPosition = v31;
  115. if (captcha_LoginPhone < 0) {
  116. ChnageCaptcha2();
  117. }
  118. }
  119. }
  120. InputField GetInputField(GameObject inputNode) {
  121. return inputNode.transform.Find("InputField").GetComponent<InputField>();
  122. }
  123. public void ChnageCaptcha1() {
  124. StartCoroutine(CaptchaController.Instance.GetCaptcha(
  125. loginInCaptcha1.transform.Find("CodeImage").GetComponent<Image>(),
  126. (code) => { captcha_Login = code; }
  127. ));
  128. }
  129. public void ChnageCaptcha2() {
  130. StartCoroutine(CaptchaController.Instance.GetCaptcha(
  131. loginInCaptcha2.transform.Find("CodeImage").GetComponent<Image>(),
  132. (code) => { captcha_LoginPhone = code; }
  133. ));
  134. }
  135. public void Login() {
  136. if (loginMode == 1) {
  137. LoginNormal();
  138. } else if (loginMode == 2) {
  139. LoginByPhone();
  140. }
  141. }
  142. JCUnityLib.Throttler throttlerLoginNormal = new JCUnityLib.Throttler(2000);
  143. void LoginNormal() {
  144. InputField user = GetInputField(loginInUser);
  145. if (user.text.Trim().Length == 0) {
  146. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请输入账号"));
  147. return;
  148. }
  149. InputField pwd = GetInputField(loginInPWD);
  150. if (pwd.text.Trim().Length == 0) {
  151. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请输入密码"));
  152. return;
  153. }
  154. InputField captcha = GetInputField(loginInCaptcha1);
  155. if (!captcha.text.Equals(captcha_Login.ToString())) {
  156. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("验证码错误"));
  157. return;
  158. }
  159. if (!AgreenmentOption.ins.IsAgreementChecked()) {
  160. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请阅读并同意App协议"));
  161. return;
  162. }
  163. if (throttlerLoginNormal.CanPass() == false) {
  164. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  165. return;
  166. }
  167. StartCoroutine(LoginController.Instance.LoginNormal(
  168. user.text, pwd.text, (res) => {
  169. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey(res.msg));
  170. if (res.code == 0) {
  171. string loginToken = (string)res.data;
  172. GoToHome(loginToken);
  173. }
  174. }
  175. ));
  176. }
  177. JCUnityLib.Throttler throttlerLoginWX = new JCUnityLib.Throttler(2000);
  178. public void LoginByWX()
  179. {
  180. if (!AgreenmentOption.ins.IsAgreementChecked()) {
  181. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请阅读并同意App协议"));
  182. return;
  183. }
  184. if (throttlerLoginWX.CanPass() == false) {
  185. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  186. return;
  187. }
  188. WeChatLoginHelper.Login();
  189. }
  190. private string _loginToken = null;
  191. public void OnWxLoginResp(string loginCode) {
  192. if (string.IsNullOrWhiteSpace(loginCode)) return;
  193. transform.Find("MaskWxLogin").gameObject.SetActive(true);
  194. StartCoroutine(LoginController.Instance.LoginByWX(loginCode, (res) => {
  195. Debug.Log($"wxlogin service rescode {res.code}, msg {res.msg}");
  196. if (res.code == 0) {
  197. JObject data = (JObject)res.data;
  198. _loginToken = data.Value<string>("token");
  199. bool needBindPhone = data.Value<bool>("needBindPhone");
  200. if (needBindPhone)
  201. {
  202. transform.Find("MaskWxLogin").gameObject.SetActive(false);
  203. //打开手机绑定界面
  204. RelateValidateView relateValidateView =
  205. Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView"))
  206. .GetComponent<RelateValidateView>();
  207. CanvasUtils.PlusSortOrder(gameObject.GetComponentInParent<Canvas>().gameObject, relateValidateView.gameObject, 1);
  208. relateValidateView.InitForPhone2();
  209. relateValidateView.onValidateSuccess = (a, b, c) => {
  210. StartCoroutine(UserController.Instance.SavePhone2(_loginToken, a, b, c, (res) => {
  211. if (res.code == 0) {
  212. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass1"));
  213. relateValidateView?.CloseView();
  214. DoTweenUtil.CallDelay(0.1f, () => GoToHome(_loginToken));
  215. }
  216. }));
  217. };
  218. }
  219. else GoToHome(_loginToken);
  220. } else {
  221. transform.Find("MaskWxLogin").gameObject.SetActive(false);
  222. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("wxlogin_fail"));
  223. }
  224. }));
  225. }
  226. private void GoToHome(string loginToken)
  227. {
  228. CommonConfig.businessServerWsURL = loginToken.Split('&')[2];
  229. PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken);
  230. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  231. }
  232. void LoginByPhone() {
  233. InputField user = GetInputField(loginInPhone);
  234. bool isMobilePhone = ValidateHelper.IsMobilePhone(user.text);
  235. if (!isMobilePhone) {
  236. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("手机号格式不正确"));
  237. return;
  238. }
  239. StartCoroutine(LoginController.Instance.LoginByPhone(
  240. user.text, (res) => {
  241. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey(res.msg));
  242. if (res.code == 0) {
  243. string loginToken = (string)res.data;
  244. GoToHome(loginToken);
  245. }
  246. }
  247. ));
  248. }
  249. public void FillLoginInput(string username, string password) {
  250. GetInputField(loginInUser).text = username;
  251. GetInputField(loginInPWD).text = password;
  252. }
  253. }