LoginView.cs 15 KB

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