You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

181 lines
5.3 KiB

  1. using System;
  2. using System.Collections;
  3. using Cysharp.Threading.Tasks;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. using UnityEngine.UI;
  7. using ETHotfix;
  8. public class LoginView : MonoBehaviour
  9. {
  10. public InputField PhoneNumberInputField;
  11. public InputField VerifyCodeInputField;
  12. public Text ErrorText;
  13. public Button SendCodeButton;
  14. public Text SendCodeButtonText;
  15. public Button LoginButton;
  16. public GameObject PhoneTrans;
  17. public GameObject loginBthTrans;
  18. public Button PhoneLoginButton;
  19. public Button WxLoginButton;
  20. public Button WxBindButton;
  21. // Start is called before the first frame update
  22. void Start()
  23. {
  24. SendCodeButton.onClick.AddListener(SendVerifyCode);
  25. LoginButton.onClick.AddListener(Login);
  26. PhoneLoginButton.onClick.AddListener(ClickPhoneLoginButton);
  27. WxLoginButton.onClick.AddListener(ClickWxLoginButton);
  28. WxBindButton.onClick.AddListener(ClickWxBindButton);
  29. string cachedPhoneNumber = PlayerPrefs.GetString("CachedPhoneNumber", "");
  30. string cachedWxUnionId = PlayerPrefs.GetString("CachedWxUnionId", "");
  31. if (!string.IsNullOrEmpty(cachedPhoneNumber) || !string.IsNullOrEmpty(cachedWxUnionId))
  32. {
  33. HttpManager.Instance.phoneNum = cachedPhoneNumber;
  34. HttpManager.Instance.wxUnionID = cachedWxUnionId;
  35. SceneManager.LoadScene("LoadingScreen");
  36. }
  37. else
  38. {
  39. PlayerPrefs.DeleteKey("CachedPhoneNumber");
  40. PlayerPrefs.DeleteKey("CachedWxUnionId");
  41. PlayerPrefs.Save();
  42. NativeBridgeUtil.Instance.loginCallback = WxloginCallback;
  43. }
  44. }
  45. public void OnDestroy()
  46. {
  47. NativeBridgeUtil.Instance.loginCallback = null;
  48. }
  49. public void WxloginCallback(string wxUnionID)
  50. {
  51. // 缓存手机号
  52. PlayerPrefs.SetString("CachedWxUnionId", wxUnionID);
  53. PlayerPrefs.Save();
  54. HttpManager.Instance.wxUnionID = wxUnionID;
  55. isWeChatAuthorized = true;
  56. CheckAuthorized();
  57. }
  58. private void SendVerifyCode()
  59. {
  60. SendVerifyCodeTask().Forget();
  61. }
  62. async UniTask SendVerifyCodeTask()
  63. {
  64. CodeResponse codeResponse = await HttpManager.Instance.RequestPost<CodeRequest, CodeResponse>("request-code", new CodeRequest(){phone = PhoneNumberInputField.text});
  65. if (codeResponse.message == "验证码已发送")
  66. {
  67. SendCodeButton.interactable = false;
  68. Countdown(30).Forget();
  69. }
  70. ErrorText.text = codeResponse.message;
  71. }
  72. private async UniTaskVoid Countdown(int duration)
  73. {
  74. // SendCodeButtonText.color = Color.gray;
  75. while (duration > 0)
  76. {
  77. SendCodeButtonText.text = $"发送验证码({duration})";
  78. await UniTask.Delay(TimeSpan.FromSeconds(1), ignoreTimeScale: false);
  79. duration--;
  80. }
  81. SendCodeButtonText.text = "发送验证码";
  82. SendCodeButton.interactable = true;
  83. // SendCodeButtonText.color = Color.black;
  84. }
  85. void Login()
  86. {
  87. LoginTask().Forget();
  88. }
  89. async UniTask LoginTask()
  90. {
  91. string phonenum = PhoneNumberInputField.text;
  92. LoginResponse loginResponse = await HttpManager.Instance.RequestPost<LoginRequest, LoginResponse>("verify-code", new LoginRequest(){phone = phonenum, code = VerifyCodeInputField.text});
  93. if (loginResponse.message == "验证成功")
  94. {
  95. // 缓存手机号
  96. PlayerPrefs.SetString("CachedPhoneNumber", phonenum);
  97. PlayerPrefs.Save();
  98. //ShowHomeView(phonenum);
  99. //SceneManager.LoadSceneAsync("LoadingScreen");
  100. HttpManager.Instance.phoneNum = phonenum;
  101. isPhoneAuthorized = true;
  102. CheckAuthorized();
  103. }
  104. else
  105. {
  106. ErrorText.text = loginResponse.message;
  107. }
  108. }
  109. private void CheckAuthorized()
  110. {
  111. if (isWeChatAuthorized && isPhoneAuthorized)
  112. {
  113. SceneManager.LoadScene("LoadingScreen");
  114. }
  115. else if (isWeChatAuthorized && !isPhoneAuthorized)
  116. {
  117. WxBindButton.gameObject.SetActive(true);
  118. PhoneTrans.SetActive(false);
  119. loginBthTrans.SetActive(false);
  120. }
  121. else if (isPhoneAuthorized && !isWeChatAuthorized)
  122. {
  123. WxBindButton.gameObject.SetActive(false);
  124. PhoneTrans.SetActive(true);
  125. loginBthTrans.SetActive(false);
  126. }
  127. if (!isPhoneAuthorized && !isWeChatAuthorized)
  128. {
  129. WxBindButton.gameObject.SetActive(false);
  130. PhoneTrans.SetActive(false);
  131. loginBthTrans.SetActive(true);
  132. }
  133. }
  134. // 是否微信授权
  135. private bool isWeChatAuthorized = false;
  136. // 是否手机授权
  137. private bool isPhoneAuthorized = false;
  138. void ClickPhoneLoginButton()
  139. {
  140. WxBindButton.gameObject.SetActive(false);
  141. PhoneTrans.SetActive(true);
  142. loginBthTrans.SetActive(false);
  143. }
  144. void ClickWxLoginButton()
  145. {
  146. NativeBridgeUtil.Instance.WeChatLogin(1);
  147. }
  148. void ClickWxBindButton()
  149. {
  150. NativeBridgeUtil.Instance.WeChatLogin(1);
  151. }
  152. }