LoginComp.cs 5.7 KB

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