|
- using System;
- using System.Collections;
- using Cysharp.Threading.Tasks;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
-
- public class LoginView : MonoBehaviour
- {
- public InputField PhoneNumberInputField;
- public InputField VerifyCodeInputField;
- public Text ErrorText;
- public Button SendCodeButton;
- public Text SendCodeButtonText;
- public Button LoginButton;
-
-
- // Start is called before the first frame update
- void Start()
- {
- SendCodeButton.onClick.AddListener(SendVerifyCode);
- LoginButton.onClick.AddListener(Login);
- // 读取缓存的手机号
- if (PlayerPrefs.HasKey("CachedPhoneNumber"))
- {
- string cachedPhoneNumber = PlayerPrefs.GetString("CachedPhoneNumber");
- if (!string.IsNullOrEmpty(cachedPhoneNumber))
- {
- HttpManager.Instance.phoneNum = cachedPhoneNumber;
- SceneManager.LoadScene("LoadingScreen");
- }
- }
- }
-
- 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()
- {
- string phonenum = PhoneNumberInputField.text;
- LoginResponse loginResponse = await HttpManager.Instance.RequestPost<LoginRequest, LoginResponse>("verify-code", new LoginRequest(){phone = phonenum, code = VerifyCodeInputField.text});
- if (loginResponse.message == "验证成功")
- {
- // 缓存手机号
- PlayerPrefs.SetString("CachedPhoneNumber", phonenum);
- PlayerPrefs.Save();
-
- //ShowHomeView(phonenum);
- //SceneManager.LoadSceneAsync("LoadingScreen");
- HttpManager.Instance.phoneNum = phonenum;
- SceneManager.LoadScene("LoadingScreen");
- }
- else
- {
- ErrorText.text = loginResponse.message;
- }
- }
-
- }
|