LoginView.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 string captcha_Login_str = "";
  30. public int captcha_LoginPhone = -222222222;
  31. [SerializeField] GameObject appleButton;
  32. public static LoginView ins;
  33. void Awake() {
  34. ins = this;
  35. if (CommonConfig.needToExamine) {
  36. transform.Find("BtnTabSwitch/LoginPhone").gameObject.SetActive(false); //隐藏手机登录
  37. transform.Find("Input Contain/GameObject/BtnForgetPWD").gameObject.SetActive(false); //隐藏忘记密码
  38. transform.Find("Logo/Layout/Text1").gameObject.SetActive(false);
  39. }
  40. if (CommonConfig.banBindRelateAccount)
  41. {
  42. transform.Find("Input Contain/GameObject/BtnForgetPWD").gameObject.SetActive(false); //隐藏忘记密码
  43. }
  44. }
  45. void Start()
  46. {
  47. InitInputLimit();
  48. SelectLoginMode(1);
  49. //设置苹果按钮状态
  50. appleButton.SetActive(AppleLoginHelper.ins.bSupportedPlatform);
  51. //设置微信登录状态按钮
  52. transform.Find("Input Contain/otherLogin/BtnWxLogin").gameObject.SetActive(CommonConfig.AppArea == 0 && WeChatLoginHelper.IsWechatInstalled());
  53. }
  54. void OnDestroy() {
  55. if (ins == this) ins = null;
  56. }
  57. void Update() {
  58. #if UNITY_EDITOR
  59. if (Input.GetKeyDown(KeyCode.F1)) {
  60. GetInputField(loginInUser).text = "lvjincheng";
  61. GetInputField(loginInPWD).text = "19980301";
  62. GetInputField(loginInCaptcha1).text = captcha_Login_str;
  63. LoginNormal();
  64. }
  65. if (Input.GetKeyDown(KeyCode.F2)) {
  66. GetInputField(loginInUser).text = "tester";
  67. GetInputField(loginInPWD).text = "123456";
  68. GetInputField(loginInCaptcha1).text = captcha_Login_str;
  69. LoginNormal();
  70. }
  71. #endif
  72. }
  73. void InitInputLimit() {
  74. //loginInUser
  75. GameObject[] inputNodes = {loginInPWD};
  76. foreach (var inputNode in inputNodes) {
  77. InputField inputField = GetInputField(inputNode);
  78. inputField.onValueChanged.AddListener(delegate(string text) {
  79. Match match = new Regex("[^A-Za-z0-9]").Match(text);
  80. if (match.Success) {
  81. inputField.text = text.Replace(match.Value, "");
  82. }
  83. });
  84. }
  85. }
  86. public void SelectLoginMode(int mode) {
  87. loginMode = mode;
  88. if (loginMode == 1) {
  89. loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[1];
  90. loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[0];
  91. loginInUser.SetActive(true);
  92. loginInPWD.SetActive(true);
  93. loginInCaptcha1.SetActive(true);
  94. loginInPhone.SetActive(false);
  95. loginInCode.SetActive(false);
  96. loginValidTime.SetActive(false);
  97. loginInCaptcha2.SetActive(false);
  98. Transform btnRegister = transform.Find("Input Contain/buttons/BtnRegister");
  99. Vector3 v31 = btnRegister.localPosition; v31.y = -168.5f;
  100. btnRegister.localPosition = v31;
  101. if (captcha_Login_str.Length <= 0) {
  102. ChnageCaptcha1();
  103. }
  104. }
  105. else if (loginMode == 2) {
  106. loginNormalTab.GetComponent<Image>().sprite = loginModeSprites[0];
  107. loginPhoneTab.GetComponent<Image>().sprite = loginModeSprites[1];
  108. loginInUser.SetActive(false);
  109. loginInPWD.SetActive(false);
  110. loginInCaptcha1.SetActive(false);
  111. loginInPhone.SetActive(true);
  112. loginInCode.SetActive(false);
  113. loginValidTime.SetActive(false);
  114. loginInCaptcha2.SetActive(false);
  115. Transform btnInPhone = loginInPhone.transform;
  116. Vector3 v30 = btnInPhone.localPosition; v30.y = -30f;
  117. btnInPhone.localPosition = v30;
  118. Transform btnRegister = transform.Find("Input Contain/buttons/BtnRegister");
  119. Vector3 v31 = btnRegister.localPosition; v31.y = -88;
  120. btnRegister.localPosition = v31;
  121. if (captcha_LoginPhone < 0) {
  122. ChnageCaptcha2();
  123. }
  124. }
  125. }
  126. InputField GetInputField(GameObject inputNode) {
  127. return inputNode.transform.Find("InputField").GetComponent<InputField>();
  128. }
  129. public void ChnageCaptcha1() {
  130. StartCoroutine(CaptchaController.Instance.GetCaptcha(
  131. loginInCaptcha1.transform.parent.Find("CodeImage").GetComponent<Image>(),
  132. (code) => { captcha_Login_str = code.ToString(); }
  133. ));
  134. //captcha_Login_str = CaptchaController.Instance.GetCaptchaV2(loginInCaptcha1.transform.parent.Find("CodeImage").GetComponent<Image>());
  135. }
  136. public void ChnageCaptcha2() {
  137. StartCoroutine(CaptchaController.Instance.GetCaptcha(
  138. loginInCaptcha2.transform.Find("CodeImage").GetComponent<Image>(),
  139. (code) => { captcha_LoginPhone = code; }
  140. ));
  141. }
  142. public void Login() {
  143. if (loginMode == 1) {
  144. LoginNormal();
  145. } else if (loginMode == 2) {
  146. LoginByPhone();
  147. }
  148. }
  149. JCUnityLib.Throttler throttlerLoginNormal = new JCUnityLib.Throttler(2000);
  150. void LoginNormal() {
  151. InputField user = GetInputField(loginInUser);
  152. if (user.text.Trim().Length == 0) {
  153. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请输入账号"));
  154. return;
  155. }
  156. InputField pwd = GetInputField(loginInPWD);
  157. if (pwd.text.Trim().Length == 0) {
  158. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请输入密码"));
  159. return;
  160. }
  161. InputField captcha = GetInputField(loginInCaptcha1);
  162. if (!captcha.text.Equals(captcha_Login_str)) {
  163. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("验证码错误"));
  164. return;
  165. }
  166. if (!AgreenmentOption.ins.IsAgreementChecked()) {
  167. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请阅读并同意App协议"));
  168. return;
  169. }
  170. if (throttlerLoginNormal.CanPass() == false) {
  171. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  172. return;
  173. }
  174. StartCoroutine(LoginController.Instance.LoginNormal(
  175. user.text, pwd.text, (res) => {
  176. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey(res.msg));
  177. if (res.code == 0) {
  178. string loginToken = (string)res.data;
  179. GoToHome(loginToken);
  180. }
  181. }
  182. ));
  183. }
  184. JCUnityLib.Throttler throttlerLoginWX = new JCUnityLib.Throttler(2000);
  185. public void LoginByWX()
  186. {
  187. if (!AgreenmentOption.ins.IsAgreementChecked()) {
  188. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请阅读并同意App协议"));
  189. return;
  190. }
  191. if (throttlerLoginWX.CanPass() == false) {
  192. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  193. return;
  194. }
  195. WeChatLoginHelper.Login();
  196. }
  197. private string _loginToken = null;
  198. public void OnWxLoginResp(string loginCode) {
  199. if (string.IsNullOrWhiteSpace(loginCode)) return;
  200. transform.Find("MaskWxLogin").gameObject.SetActive(true);
  201. StartCoroutine(LoginController.Instance.LoginByWX(loginCode, (res) => {
  202. Debug.Log($"wxlogin service rescode {res.code}, msg {res.msg}");
  203. if (res.code == 0) {
  204. JObject data = (JObject)res.data;
  205. _loginToken = data.Value<string>("token");
  206. bool needBindPhone = data.Value<bool>("needBindPhone");
  207. if (needBindPhone)
  208. {
  209. transform.Find("MaskWxLogin").gameObject.SetActive(false);
  210. //打开手机绑定界面
  211. RelateValidateView relateValidateView =
  212. Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView"))
  213. .GetComponent<RelateValidateView>();
  214. CanvasUtils.PlusSortOrder(gameObject.GetComponentInParent<Canvas>().gameObject, relateValidateView.gameObject, 1);
  215. relateValidateView.InitForPhone2();
  216. relateValidateView.onValidateSuccess = (a, b, c) => {
  217. StartCoroutine(UserController.Instance.SavePhone2(_loginToken, a, b, c, (res) => {
  218. if (res.code == 0) {
  219. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass1"));
  220. relateValidateView?.CloseView();
  221. DoTweenUtil.CallDelay(0.1f, () => GoToHome(_loginToken));
  222. }
  223. }));
  224. };
  225. }
  226. else GoToHome(_loginToken);
  227. } else {
  228. transform.Find("MaskWxLogin").gameObject.SetActive(false);
  229. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("wxlogin_fail"));
  230. }
  231. }));
  232. }
  233. JCUnityLib.Throttler throttlerLoginApple = new JCUnityLib.Throttler(2000);
  234. public void LoginByApple()
  235. {
  236. if (!AgreenmentOption.ins.IsAgreementChecked())
  237. {
  238. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("请阅读并同意App协议"));
  239. return;
  240. }
  241. if (throttlerLoginApple.CanPass() == false)
  242. {
  243. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("操作过于频繁"));
  244. return;
  245. }
  246. //苹果登录
  247. AppleLoginHelper.ins.SignInWithAppleButtonPressed();
  248. }
  249. private string _loginAppleToken = null;
  250. public void OnAppleLoginResp(string _identityToken, string _email, string _fullName)
  251. {
  252. transform.Find("MaskAppleLogin").gameObject.SetActive(true);
  253. StartCoroutine(LoginController.Instance.LoginByApple(_identityToken, _email, _fullName, (res) =>
  254. {
  255. Debug.Log($"LoginByApple service rescode {res.code}, msg {res.msg}");
  256. if (res.code == 0)
  257. {
  258. JObject data = (JObject)res.data;
  259. _loginAppleToken = data.Value<string>("token");
  260. bool needBindPhone = data.Value<bool>("needBindPhone");
  261. if (needBindPhone)
  262. {
  263. transform.Find("MaskAppleLogin").gameObject.SetActive(false);
  264. //打开手机绑定界面
  265. RelateValidateView relateValidateView =
  266. Instantiate(SceneResourceManager.Instance.GetPrefab("RelateValidateView"))
  267. .GetComponent<RelateValidateView>();
  268. CanvasUtils.PlusSortOrder(gameObject.GetComponentInParent<Canvas>().gameObject, relateValidateView.gameObject, 1);
  269. relateValidateView.InitForPhone2();
  270. relateValidateView.onValidateSuccess = (a, b, c) => {
  271. StartCoroutine(UserController.Instance.SavePhone2(_loginAppleToken, a, b, c, (res) => {
  272. if (res.code == 0)
  273. {
  274. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("RelateValidateView-pass1"));
  275. relateValidateView?.CloseView();
  276. DoTweenUtil.CallDelay(0.1f, () => GoToHome(_loginAppleToken));
  277. }
  278. }));
  279. };
  280. }
  281. else GoToHome(_loginAppleToken);
  282. }
  283. else
  284. {
  285. transform.Find("MaskAppleLogin").gameObject.SetActive(false);
  286. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("appleLogin_fail"));
  287. AppleLoginHelper.ins.DeleteAppleKey();
  288. }
  289. }));
  290. }
  291. public void OnAppleLoginError() {
  292. transform.Find("MaskAppleLogin").gameObject.SetActive(false);
  293. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByKey("appleLogin_fail"));
  294. }
  295. private void GoToHome(string loginToken)
  296. {
  297. CommonConfig.businessServerWsURL = loginToken.Split('&')[2];
  298. PlayerPrefs.SetString(LoginMgr.LoginTokenKey, loginToken);
  299. SceneManager.LoadScene("Home", LoadSceneMode.Single);
  300. }
  301. void LoginByPhone() {
  302. InputField user = GetInputField(loginInPhone);
  303. bool isMobilePhone = ValidateHelper.IsMobilePhone(user.text);
  304. if (!isMobilePhone) {
  305. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey("手机号格式不正确"));
  306. return;
  307. }
  308. StartCoroutine(LoginController.Instance.LoginByPhone(
  309. user.text, (res) => {
  310. PopupMgr.ins.ShowTip(TextAutoLanguage2.GetTextByCNKey(res.msg));
  311. if (res.code == 0) {
  312. string loginToken = (string)res.data;
  313. GoToHome(loginToken);
  314. }
  315. }
  316. ));
  317. }
  318. public void FillLoginInput(string username, string password) {
  319. GetInputField(loginInUser).text = username;
  320. GetInputField(loginInPWD).text = password;
  321. }
  322. }