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.
 
 
 

116 lines
3.4 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. // Start is called before the first frame update
  21. void Start()
  22. {
  23. SendCodeButton.onClick.AddListener(SendVerifyCode);
  24. LoginButton.onClick.AddListener(Login);
  25. PhoneLoginButton.onClick.AddListener(ClickPhoneLoginButton);
  26. WxLoginButton.onClick.AddListener(ClickWxLoginButton);
  27. // 读取缓存的手机号
  28. if (PlayerPrefs.HasKey("CachedPhoneNumber"))
  29. {
  30. string cachedPhoneNumber = PlayerPrefs.GetString("CachedPhoneNumber");
  31. if (!string.IsNullOrEmpty(cachedPhoneNumber))
  32. {
  33. HttpManager.Instance.phoneNum = cachedPhoneNumber;
  34. SceneManager.LoadScene("LoadingScreen");
  35. }
  36. }
  37. }
  38. private void SendVerifyCode()
  39. {
  40. SendVerifyCodeTask().Forget();
  41. }
  42. async UniTask SendVerifyCodeTask()
  43. {
  44. CodeResponse codeResponse = await HttpManager.Instance.RequestPost<CodeRequest, CodeResponse>("request-code", new CodeRequest(){phone = PhoneNumberInputField.text});
  45. if (codeResponse.message == "验证码已发送")
  46. {
  47. SendCodeButton.interactable = false;
  48. Countdown(30).Forget();
  49. }
  50. ErrorText.text = codeResponse.message;
  51. }
  52. private async UniTaskVoid Countdown(int duration)
  53. {
  54. // SendCodeButtonText.color = Color.gray;
  55. while (duration > 0)
  56. {
  57. SendCodeButtonText.text = $"发送验证码({duration})";
  58. await UniTask.Delay(TimeSpan.FromSeconds(1), ignoreTimeScale: false);
  59. duration--;
  60. }
  61. SendCodeButtonText.text = "发送验证码";
  62. SendCodeButton.interactable = true;
  63. // SendCodeButtonText.color = Color.black;
  64. }
  65. void Login()
  66. {
  67. LoginTask().Forget();
  68. NativeBridgeUtil.Instance.WeChatLogin(1);
  69. }
  70. async UniTask LoginTask()
  71. {
  72. string phonenum = PhoneNumberInputField.text;
  73. LoginResponse loginResponse = await HttpManager.Instance.RequestPost<LoginRequest, LoginResponse>("verify-code", new LoginRequest(){phone = phonenum, code = VerifyCodeInputField.text});
  74. if (loginResponse.message == "验证成功")
  75. {
  76. // 缓存手机号
  77. PlayerPrefs.SetString("CachedPhoneNumber", phonenum);
  78. PlayerPrefs.Save();
  79. //ShowHomeView(phonenum);
  80. //SceneManager.LoadSceneAsync("LoadingScreen");
  81. HttpManager.Instance.phoneNum = phonenum;
  82. SceneManager.LoadScene("LoadingScreen");
  83. }
  84. else
  85. {
  86. ErrorText.text = loginResponse.message;
  87. }
  88. }
  89. void ClickPhoneLoginButton()
  90. {
  91. }
  92. void ClickWxLoginButton()
  93. {
  94. NativeBridgeUtil.Instance.WeChatLogin(1);
  95. }
  96. }