LoginComp.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Api;
  6. using Plugins.CxShine.page;
  7. using UI.Common;
  8. using UI.Loading;
  9. using Unity.VisualScripting;
  10. using UnityEngine;
  11. using UnityEngine.UI;
  12. public class LoginComp : MonoBehaviour
  13. {
  14. public GameObject LoginUI;
  15. public GameObject hallPrefab;
  16. public Dropdown Dropdown;
  17. public Text PhoneFront;
  18. public InputField PhoneInputField;
  19. public InputField CodeInputField;
  20. public GameObject SendButton;
  21. public LoadingComp LoadingComp;
  22. private bool _sending = false;
  23. private float _sendTime;
  24. Dictionary<string, string> phonePrefixes = new Dictionary<string, string>
  25. {
  26. { "India", "+91" },
  27. { "Brunei", "+673" },
  28. { "Cambodia", "+855" },
  29. { "Indonesia", "+62" },
  30. { "Laos", "+856" },
  31. { "Malaysia", "+60" },
  32. { "Myanmar", "+95" },
  33. { "Philippines", "+63" },
  34. { "Singapore", "+65" },
  35. { "Thailand", "+66" },
  36. { "Vietnam", "+84" }
  37. };
  38. public string GetPhonePrefixByCountry(string countryName)
  39. {
  40. if (phonePrefixes.ContainsKey(countryName))
  41. {
  42. return phonePrefixes[countryName];
  43. }
  44. return null;
  45. }
  46. void Start()
  47. {
  48. openLoading();
  49. if (AccountManager.Instance.HasSession())
  50. {
  51. AccountManager.Instance.requestSelfInfo(data => { OnLoginSuccess(); }, (code, err) =>
  52. {
  53. TipsComp.ShowTips("Need Login");
  54. initLoginUI();
  55. });
  56. }
  57. else
  58. {
  59. initLoginUI();
  60. }
  61. }
  62. private void openLoading()
  63. {
  64. LoginUI.SetActive(false);
  65. LoadingComp.gameObject.SetActive(true);
  66. }
  67. private void closeLoading()
  68. {
  69. LoginUI.SetActive(true);
  70. LoadingComp.gameObject.SetActive(false);
  71. }
  72. private void OpenHall()
  73. {
  74. PageManagerComp.singleton.OpenPage(hallPrefab);
  75. }
  76. private void OnLoginSuccess()
  77. {
  78. ApiComp.Instance.LoadConfig(() =>
  79. {
  80. Invoke(nameof(OpenHall), 3.0f);
  81. },
  82. (a, b) =>
  83. {
  84. closeLoading();
  85. TipsComp.ShowTips("LoadConfigErr" + b);
  86. });
  87. }
  88. private void initLoginUI()
  89. {
  90. closeLoading();
  91. Dropdown.options.Clear();
  92. foreach (var key in phonePrefixes.Keys)
  93. {
  94. Dropdown.options.Add(new Dropdown.OptionData(key + "" + phonePrefixes[key]));
  95. }
  96. RefreshFrontPhoneUI();
  97. }
  98. private void RefreshFrontPhoneUI()
  99. {
  100. PhoneFront.text = getCurrentSelectValue();
  101. }
  102. public void A__OnDropdownSelected()
  103. {
  104. RefreshFrontPhoneUI();
  105. }
  106. private string getCurrentSelectValue()
  107. {
  108. int i = 0;
  109. foreach (var key in phonePrefixes.Keys)
  110. {
  111. if (i == Dropdown.value)
  112. {
  113. return phonePrefixes[key];
  114. }
  115. i++;
  116. }
  117. return phonePrefixes["India"];
  118. }
  119. private void HideSendButton()
  120. {
  121. // SendButton.SetActive(false);
  122. _sending = true;
  123. _sendTime = Time.time;
  124. InvokeRepeating(nameof(Repeat1SecondsCheckSendButton), 0.01f, 1.0f);
  125. }
  126. private void Repeat1SecondsCheckSendButton()
  127. {
  128. if (_sending)
  129. {
  130. int seconds = 30 - (int)(Time.time - _sendTime);
  131. if (seconds < 0)
  132. {
  133. _sending = false;
  134. CancelInvoke(nameof(Repeat1SecondsCheckSendButton));
  135. SendButton.gameObject.GetComponentInChildren<Text>().text = "send";
  136. }
  137. else
  138. {
  139. SendButton.gameObject.GetComponentInChildren<Text>().text = seconds + "";
  140. }
  141. }
  142. }
  143. public void A__ClickSendCode()
  144. {
  145. if (_sending)
  146. {
  147. TipsComp.ShowTips("wait");
  148. return;
  149. }
  150. string verify = PhoneInputField.text.Trim().Replace(" ","");
  151. if (String.IsNullOrEmpty(verify) || !float.TryParse(verify, out _))
  152. {
  153. TipsComp.ShowTips("Phone Number Error");
  154. return;
  155. }
  156. openLoading();
  157. string phone = getCurrentSelectValue().Replace("+", "") + PhoneInputField.text.Trim().Replace(" ","");
  158. ApiComp.Instance.NotSessionSendVerifyCode(phone,
  159. data =>
  160. {
  161. closeLoading();
  162. TipsComp.ShowTips("Success!Please enter your Code");
  163. HideSendButton();
  164. },
  165. (code, err) =>
  166. {
  167. closeLoading();
  168. TipsComp.ShowTips(err);
  169. });
  170. }
  171. public void A__ClickLogin()
  172. {
  173. string verify = PhoneInputField.text.Trim().Replace(" ","");
  174. if (String.IsNullOrEmpty(verify) || !float.TryParse(verify, out _))
  175. {
  176. TipsComp.ShowTips("Phone Number Error");
  177. return;
  178. }
  179. verify = CodeInputField.text.Trim();
  180. if (String.IsNullOrEmpty(verify) || !float.TryParse(verify, out _))
  181. {
  182. TipsComp.ShowTips("Code Number Error");
  183. return;
  184. }
  185. openLoading();
  186. string phone = getCurrentSelectValue().Replace("+", "") + PhoneInputField.text.Trim().Replace(" ","");
  187. string code = CodeInputField.text.Trim();
  188. bool isIndia = getCurrentSelectValue().Equals("+91");
  189. Debug.Log("is India"+isIndia);
  190. AccountManager.Instance.login(phone, code, isIndia, data =>
  191. {
  192. OnLoginSuccess();
  193. },
  194. (code, err) =>
  195. {
  196. closeLoading();
  197. TipsComp.ShowTips("LoginError" + err);
  198. }
  199. );
  200. }
  201. }