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.
 
 
 

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