LoginView.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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(Application.platform == RuntimePlatform.Android && 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. #if UNITY_ANDROID
  189. using (var sdk = new AndroidJavaClass("com.xmjssvr.BowArrow.mi.WeChatSDK")) sdk.CallStatic("WeChatLogin");
  190. #endif
  191. }
  192. private string _loginToken = null;
  193. public void OnWxLoginResp(string loginCode) {
  194. if (string.IsNullOrWhiteSpace(loginCode)) return;
  195. transform.Find("MaskWxLogin").gameObject.SetActive(true);
  196. StartCoroutine(LoginController.Instance.LoginByWX(loginCode, (res) => {
  197. Debug.Log($"wxlogin service rescode {res.code}, msg {res.msg}");
  198. if (res.code == 0) {
  199. JObject data = (JObject)res.data;
  200. _loginToken = data.Value<string>("token");
  201. bool needBindPhone = data.Value<bool>("needBindPhone");
  202. if (needBindPhone)
  203. {
  204. transform.Find("MaskWxLogin").gameObject.SetActive(false);
  205. //打开手机绑定界面
  206. RelateValidateView relateValidateView =
  207. Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView"))
  208. .GetComponent<RelateValidateView>();
  209. CanvasUtils.PlusSortOrder(gameObject.GetComponentInParent<Canvas>().gameObject, relateValidateView.gameObject, 1);
  210. relateValidateView.InitForPhone2();
  211. relateValidateView.onValidateSuccess = (a, b, c) => {
  212. StartCoroutine(UserController.Instance.SavePhone2(_loginToken, a, b, c, (res) => {
  213. if (res.code == 0) {
  214. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass1"));
  215. relateValidateView?.CloseView();
  216. DoTweenUtil.CallDelay(0.1f, () => GoToHome(_loginToken));
  217. }
  218. }));
  219. };
  220. }
  221. else GoToHome(_loginToken);
  222. } else {
  223. transform.Find("MaskWxLogin").gameObject.SetActive(false);
  224. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("wxlogin_fail"));
  225. }
  226. }));
  227. }
  228. private void GoToHome(string loginToken)
  229. {
  230. CommonConfig.businessServerWsURL = loginToken.Split('&')[2];
  231. PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken);
  232. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  233. }
  234. void LoginByPhone() {
  235. InputField user = GetInputField(loginInPhone);
  236. Regex regex = new Regex(@"^(((13[0-9]{1})|(15[0-35-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$");
  237. bool isMobilePhone = regex.IsMatch(user.text);
  238. if (!isMobilePhone) {
  239. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("手机号格式不正确"));
  240. return;
  241. }
  242. StartCoroutine(LoginController.Instance.LoginByPhone(
  243. user.text, (res) => {
  244. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey(res.msg));
  245. if (res.code == 0) {
  246. string loginToken = (string)res.data;
  247. GoToHome(loginToken);
  248. }
  249. }
  250. ));
  251. }
  252. public void FillLoginInput(string username, string password) {
  253. GetInputField(loginInUser).text = username;
  254. GetInputField(loginInPWD).text = password;
  255. }
  256. }