LoginView.cs 15 KB

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