using System; using System.Collections; using Cysharp.Threading.Tasks; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using ETHotfix; public class LoginView : MonoBehaviour { public InputField PhoneNumberInputField; public InputField VerifyCodeInputField; public Text ErrorText; public Button SendCodeButton; public Text SendCodeButtonText; public Button LoginButton; public GameObject PhoneTrans; public GameObject loginBthTrans; public Button PhoneLoginButton; public Button WxLoginButton; public Button WxBindButton; // Start is called before the first frame update void Start() { SendCodeButton.onClick.AddListener(SendVerifyCode); LoginButton.onClick.AddListener(Login); PhoneLoginButton.onClick.AddListener(ClickPhoneLoginButton); WxLoginButton.onClick.AddListener(ClickWxLoginButton); WxBindButton.onClick.AddListener(ClickWxBindButton); string cachedPhoneNumber = PlayerPrefs.GetString("CachedPhoneNumber", ""); string cachedWxUnionId = PlayerPrefs.GetString("CachedWxUnionId", ""); if (!string.IsNullOrEmpty(cachedPhoneNumber) || !string.IsNullOrEmpty(cachedWxUnionId)) { HttpManager.Instance.phoneNum = cachedPhoneNumber; HttpManager.Instance.wxUnionID = cachedWxUnionId; SceneManager.LoadScene("LoadingScreen"); } else { PlayerPrefs.DeleteKey("CachedPhoneNumber"); PlayerPrefs.DeleteKey("CachedWxUnionId"); PlayerPrefs.Save(); NativeBridgeUtil.Instance.loginCallback = WxloginCallback; } } public void OnDestroy() { NativeBridgeUtil.Instance.loginCallback = null; } public void WxloginCallback(string wxUnionID) { // 缓存手机号 PlayerPrefs.SetString("CachedWxUnionId", wxUnionID); PlayerPrefs.Save(); HttpManager.Instance.wxUnionID = wxUnionID; isWeChatAuthorized = true; CheckAuthorized(); } private void SendVerifyCode() { SendVerifyCodeTask().Forget(); } async UniTask SendVerifyCodeTask() { CodeResponse codeResponse = await HttpManager.Instance.RequestPost("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("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; isPhoneAuthorized = true; CheckAuthorized(); } else { ErrorText.text = loginResponse.message; } } private void CheckAuthorized() { if (isWeChatAuthorized && isPhoneAuthorized) { SceneManager.LoadScene("LoadingScreen"); } else if (isWeChatAuthorized && !isPhoneAuthorized) { WxBindButton.gameObject.SetActive(true); PhoneTrans.SetActive(false); loginBthTrans.SetActive(false); } else if (isPhoneAuthorized && !isWeChatAuthorized) { WxBindButton.gameObject.SetActive(false); PhoneTrans.SetActive(true); loginBthTrans.SetActive(false); } if (!isPhoneAuthorized && !isWeChatAuthorized) { WxBindButton.gameObject.SetActive(false); PhoneTrans.SetActive(false); loginBthTrans.SetActive(true); } } // 是否微信授权 private bool isWeChatAuthorized = false; // 是否手机授权 private bool isPhoneAuthorized = false; void ClickPhoneLoginButton() { WxBindButton.gameObject.SetActive(false); PhoneTrans.SetActive(true); loginBthTrans.SetActive(false); } void ClickWxLoginButton() { NativeBridgeUtil.Instance.WeChatLogin(1); } void ClickWxBindButton() { NativeBridgeUtil.Instance.WeChatLogin(1); } }