using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Api; using Plugins.CxShine.page; using UI.Common; using UI.Loading; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; public class LoginComp : MonoBehaviour { public GameObject LoginUI; public GameObject hallPrefab; public Dropdown Dropdown; public Text PhoneFront; public InputField PhoneInputField; public InputField CodeInputField; public GameObject SendButton; public LoadingComp LoadingComp; private bool _sending = false; private float _sendTime; Dictionary phonePrefixes = new Dictionary { { "India", "+91" }, { "Brunei", "+673" }, { "Cambodia", "+855" }, { "Indonesia", "+62" }, { "Laos", "+856" }, { "Malaysia", "+60" }, { "Myanmar", "+95" }, { "Philippines", "+63" }, { "Singapore", "+65" }, { "Thailand", "+66" }, { "Vietnam", "+84" } }; public string GetPhonePrefixByCountry(string countryName) { if (phonePrefixes.ContainsKey(countryName)) { return phonePrefixes[countryName]; } return null; } void Start() { openLoading(); if (AccountManager.Instance.HasSession()) { AccountManager.Instance.requestSelfInfo(data => { OnLoginSuccess(); }, (code, err) => { TipsComp.ShowTips("Need Login"); initLoginUI(); }); } else { initLoginUI(); } } private void openLoading() { LoginUI.SetActive(false); LoadingComp.gameObject.SetActive(true); } private void closeLoading() { LoginUI.SetActive(true); LoadingComp.gameObject.SetActive(false); } private void OpenHall() { PageManagerComp.singleton.OpenPage(hallPrefab); } private void OnLoginSuccess() { ApiComp.Instance.LoadConfig(() => { Invoke(nameof(OpenHall), 3.0f); }, (a, b) => { closeLoading(); TipsComp.ShowTips("LoadConfigErr" + b); }); } private void initLoginUI() { closeLoading(); Dropdown.options.Clear(); foreach (var key in phonePrefixes.Keys) { Dropdown.options.Add(new Dropdown.OptionData(key + "" + phonePrefixes[key])); } RefreshFrontPhoneUI(); } private void RefreshFrontPhoneUI() { PhoneFront.text = getCurrentSelectValue(); } public void A__OnDropdownSelected() { RefreshFrontPhoneUI(); } private string getCurrentSelectValue() { int i = 0; foreach (var key in phonePrefixes.Keys) { if (i == Dropdown.value) { return phonePrefixes[key]; } i++; } return phonePrefixes["India"]; } private void HideSendButton() { // SendButton.SetActive(false); _sending = true; _sendTime = Time.time; InvokeRepeating(nameof(Repeat1SecondsCheckSendButton), 0.01f, 1.0f); } private void Repeat1SecondsCheckSendButton() { if (_sending) { int seconds = 30 - (int)(Time.time - _sendTime); if (seconds < 0) { _sending = false; CancelInvoke(nameof(Repeat1SecondsCheckSendButton)); SendButton.gameObject.GetComponentInChildren().text = "send"; } else { SendButton.gameObject.GetComponentInChildren().text = seconds + ""; } } } public void A__ClickSendCode() { if (_sending) { TipsComp.ShowTips("wait"); return; } string verify = PhoneInputField.text.Trim().Replace(" ",""); if (String.IsNullOrEmpty(verify) || !float.TryParse(verify, out _)) { TipsComp.ShowTips("Phone Number Error"); return; } openLoading(); string phone = getCurrentSelectValue().Replace("+", "") + PhoneInputField.text.Trim().Replace(" ",""); ApiComp.Instance.NotSessionSendVerifyCode(phone, data => { closeLoading(); TipsComp.ShowTips("Success!Please enter your Code"); HideSendButton(); }, (code, err) => { closeLoading(); TipsComp.ShowTips(err); }); } public void A__ClickLogin() { string verify = PhoneInputField.text.Trim().Replace(" ",""); if (String.IsNullOrEmpty(verify) || !float.TryParse(verify, out _)) { TipsComp.ShowTips("Phone Number Error"); return; } verify = CodeInputField.text.Trim(); if (String.IsNullOrEmpty(verify) || !float.TryParse(verify, out _)) { TipsComp.ShowTips("Code Number Error"); return; } openLoading(); string phone = getCurrentSelectValue().Replace("+", "") + PhoneInputField.text.Trim().Replace(" ",""); string code = CodeInputField.text.Trim(); bool isIndia = getCurrentSelectValue().Equals("+91"); Debug.Log("is India"+isIndia); AccountManager.Instance.login(phone, code, isIndia, data => { OnLoginSuccess(); }, (code, err) => { closeLoading(); TipsComp.ShowTips("LoginError" + err); } ); } }