|
- using System;
- using Cysharp.Threading.Tasks;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
-
- public class LoginView : MonoBehaviour
- {
- public TMP_InputField PhoneNumberInputField;
- public TMP_InputField VerifyCodeInputField;
- public TMP_Text ErrorText;
- public Button SendCodeButton;
- public TMP_Text SendCodeButtonText;
- public Button LoginButton;
-
- public HomeView homeView;
-
-
- // Start is called before the first frame update
- void Start()
- {
- SendCodeButton.onClick.AddListener(SendVerifyCode);
- LoginButton.onClick.AddListener(Login);
- }
-
- private void SendVerifyCode()
- {
- SendVerifyCodeTask().Forget();
- }
-
- async UniTask SendVerifyCodeTask()
- {
- CodeResponse codeResponse = await HttpManager.Instance.RequestPost<CodeRequest, CodeResponse>("request-code", new CodeRequest(){phone = PhoneNumberInputField.text});
- if (codeResponse.message == "验证码已发送")
- {
- SendCodeButton.interactable = false;
- Countdown(30).Forget();
- }
-
- ErrorText.text = codeResponse.message;
-
- }
-
- private async UniTaskVoid Countdown(int duration)
- {
- SendCodeButtonText.color = Color.gray;
- while (duration > 0)
- {
- SendCodeButtonText.text = $"发送验证码({duration})";
- await UniTask.Delay(TimeSpan.FromSeconds(1), ignoreTimeScale: false);
- duration--;
- }
- SendCodeButtonText.text = "发送验证码";
- SendCodeButton.interactable = true;
- SendCodeButtonText.color = Color.black;
- }
-
-
-
- void Login()
- {
- LoginTask().Forget();
- }
-
- async UniTask LoginTask()
- {
- LoginResponse loginResponse = await HttpManager.Instance.RequestPost<LoginRequest, LoginResponse>("verify-code", new LoginRequest(){phone = PhoneNumberInputField.text, code = VerifyCodeInputField.text});
- if (loginResponse.message == "验证成功")
- {
- ShowHomeView();
- }
- else
- {
- ErrorText.text = loginResponse.message;
- }
- }
-
- private void ShowHomeView()
- {
- gameObject.SetActive(false);
- homeView.Show();
- }
-
- // Update is called once per frame
- void Update()
- {
-
- }
- }
|