LoginInputView.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using UI.Login;
  2. using FairyGUI;
  3. using UnityEngine;
  4. using System.ComponentModel;
  5. using System;
  6. using System.Collections.Generic;
  7. namespace GFGGame
  8. {
  9. public class LoginInputView : BaseWindow
  10. {
  11. private UI_LoginInputUI _ui;
  12. private List<AccountPasswordModel> _accountPasswordList;
  13. public override void Dispose()
  14. {
  15. if (_ui != null)
  16. {
  17. _ui.Dispose();
  18. _ui = null;
  19. }
  20. base.Dispose();
  21. }
  22. protected override void OnInit()
  23. {
  24. base.OnInit();
  25. packageName = UI_LoginInputUI.PACKAGE_NAME;
  26. _ui = UI_LoginInputUI.Create();
  27. this.viewCom = _ui.target;
  28. this.viewCom.Center();
  29. this.modal = true;
  30. _ui.m_btnSure.onClick.Add(OnClickBtnSure);
  31. _ui.m_btnCancel.onClick.Add(OnClickBtnCancel);
  32. _ui.m_btnRegister.onClick.Add(OnClickBtnRegister);
  33. _ui.m_comAccount.m_selBtin.onClick.Add(OnClickOpenAccountListBtn);
  34. _ui.m_comAccount.m_txtAccount.restrict = "[0-9A-Za-z_]";
  35. _ui.m_comAccount.m_list.itemRenderer = RenderListItem;
  36. if (LauncherConfig.netType == LauncherConfig.EnumNetType.LOCAL)
  37. {
  38. _ui.m_inputPassword.promptText = "[color=#B8A492]当前支持免密登录[/color]";
  39. }
  40. InitChanelBox();
  41. }
  42. protected override void OnShown()
  43. {
  44. base.OnShown();
  45. _accountPasswordList = AccountManager.Instance.GetAccounts();
  46. string account = null;
  47. string password = null;
  48. if (_accountPasswordList != null && _accountPasswordList.Count > 0)
  49. {
  50. account = _accountPasswordList[0].Account;
  51. password = _accountPasswordList[0].Password;
  52. }
  53. if (account != null)
  54. {
  55. _ui.m_comAccount.m_txtAccount.text = account;
  56. }
  57. if (password != null)
  58. {
  59. _ui.m_inputPassword.text = password;
  60. }
  61. UpdateChannelBox(false);
  62. }
  63. protected override void OnHide()
  64. {
  65. base.OnHide();
  66. }
  67. protected override void AddEventListener()
  68. {
  69. base.AddEventListener();
  70. EventAgent.AddEventListener(ConstMessage.LOGIN_FAIL, OnLoginFail);
  71. }
  72. protected override void RemoveEventListener()
  73. {
  74. base.RemoveEventListener();
  75. EventAgent.RemoveEventListener(ConstMessage.LOGIN_FAIL, OnLoginFail);
  76. }
  77. private void OnClickBtnSure()
  78. {
  79. var account = _ui.m_comAccount.m_txtAccount.text;
  80. var password = _ui.m_inputPassword.text;
  81. if (string.IsNullOrEmpty(account))
  82. {
  83. PromptController.Instance.ShowFloatTextPrompt("请输入账号");
  84. return;
  85. }
  86. if (!string.IsNullOrEmpty(password))
  87. {
  88. LoginController.Login(account, password).Coroutine();
  89. }
  90. else if (LauncherConfig.netType == LauncherConfig.EnumNetType.LOCAL)
  91. {
  92. LoginController.LoginTest(account).Coroutine();
  93. }
  94. else
  95. {
  96. PromptController.Instance.ShowFloatTextPrompt("请输入密码");
  97. }
  98. }
  99. private void OnClickBtnCancel()
  100. {
  101. this.Hide();
  102. }
  103. private void OnClickBtnRegister()
  104. {
  105. ViewManager.Show<RegisterView>();
  106. }
  107. private void OnClickOpenAccountListBtn()
  108. {
  109. if (_accountPasswordList.Count == 0)
  110. {
  111. _ui.m_comAccount.m_list.selectedIndex = 0;
  112. return;
  113. }
  114. if (_ui.m_comAccount.m_c1.selectedIndex == 0)
  115. {
  116. //显示账号缓存列表
  117. OnShowAccountList();
  118. }
  119. else
  120. {
  121. //隐藏账号缓存列表
  122. OnHideAccountList();
  123. }
  124. }
  125. //显示账号缓存列表
  126. private void OnShowAccountList()
  127. {
  128. _accountPasswordList = AccountManager.Instance.GetAccounts();
  129. _ui.m_comAccount.m_list.numItems = _accountPasswordList.Count;
  130. _ui.m_comAccount.m_c1.selectedIndex = 1;
  131. }
  132. //隐藏账号缓存列表
  133. private void OnHideAccountList()
  134. {
  135. _ui.m_comAccount.m_c1.selectedIndex = 0;
  136. }
  137. //绑定数据到账号缓存列表
  138. private void RenderListItem(int index, GObject obj)
  139. {
  140. AccountPasswordModel accountPassword = _accountPasswordList[index];
  141. UI_ButAccountItem item = UI_ButAccountItem.Proxy(obj);
  142. item.m_txtAccountItem.text = accountPassword.Account;
  143. item.target.data = accountPassword;
  144. item.target.onClick.Add(OnClickSelAccount);
  145. item.m_btnDel.data = accountPassword.Account;
  146. item.m_btnDel.onClick.Add(OnClickDelAccountCache);
  147. UI_ButAccountItem.ProxyEnd();
  148. }
  149. //从账号缓存列表中选择账号
  150. private void OnClickSelAccount(EventContext context)
  151. {
  152. GObject obj = context.sender as GObject;
  153. AccountPasswordModel accountPassword = obj.data as AccountPasswordModel;
  154. _ui.m_comAccount.m_txtAccount.text = accountPassword?.Account;
  155. _ui.m_inputPassword.text = accountPassword?.Password;
  156. OnHideAccountList();
  157. }
  158. //删除账号缓存列表
  159. private void OnClickDelAccountCache(EventContext context)
  160. {
  161. GObject obj = context.sender as GObject;
  162. string account = obj.data.ToString();
  163. AccountManager.Instance.DeleteAccount(account);
  164. _accountPasswordList = AccountManager.Instance.GetAccounts();
  165. _ui.m_comAccount.m_list.numItems = _accountPasswordList.Count;
  166. if (_accountPasswordList.Count == 0)
  167. {
  168. _ui.m_comAccount.m_list.selectedIndex = 0;
  169. }
  170. if (_accountPasswordList.Count == 0)
  171. {
  172. _ui.m_comAccount.m_txtAccount.text = null;
  173. _ui.m_inputPassword.text = null;
  174. }
  175. else
  176. {
  177. _ui.m_comAccount.m_txtAccount.text = _accountPasswordList[0].Account;
  178. _ui.m_inputPassword.text = _accountPasswordList[0].Password;
  179. }
  180. }
  181. private void OnLoginFail(EventContext context)
  182. {
  183. string account = (string)context.data;
  184. _ui.m_comAccount.m_txtAccount.text = account;
  185. }
  186. private void InitChanelBox()
  187. {
  188. #if !UNITY_EDITOR
  189. _ui.m_boxChooseCanal.visible = false;
  190. return;
  191. #endif
  192. _ui.m_boxChooseCanal.visible = LauncherConfig.netType == LauncherConfig.EnumNetType.LOCAL;
  193. if (!_ui.m_boxChooseCanal.visible) return;
  194. var enumType = typeof(ChannelID);
  195. var arr = Enum.GetValues(enumType);
  196. List<string> items = new List<string>();
  197. List<string> numbers = new List<string>();
  198. foreach (var value in arr)
  199. {
  200. var fieldInfo = enumType.GetField((value).ToString());
  201. if (fieldInfo != null && Attribute.IsDefined(fieldInfo, typeof(DescriptionAttribute)))
  202. {
  203. DescriptionAttribute attribute =
  204. (DescriptionAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(DescriptionAttribute));
  205. items.Add(attribute.Description);
  206. numbers.Add(Convert.ToInt32(value).ToString());
  207. }
  208. }
  209. _ui.m_boxChooseCanal.items = items.ToArray();
  210. _ui.m_boxChooseCanal.values = numbers.ToArray();
  211. _ui.m_boxChooseCanal.value = LauncherConfig.ChannelId + "";
  212. _ui.m_boxChooseCanal.onChanged.Add(onBoxChannelChanged);
  213. }
  214. private void onBoxChannelChanged()
  215. {
  216. UpdateChannelBox(true);
  217. }
  218. private void UpdateChannelBox(bool showAlert = true)
  219. {
  220. if (!_ui.m_boxChooseCanal.visible) return;
  221. LauncherConfig.ChannelId = int.Parse(_ui.m_boxChooseCanal.value);
  222. ET.Log.Debug("打印测试======选择的渠道==========" + (_ui.m_boxChooseCanal.value));
  223. if (_ui.m_boxChooseCanal.value == (int)ChannelID.Test + "")
  224. {
  225. if (showAlert)
  226. {
  227. AlertSystem.Show("切换其他渠道后不支持切回测试渠道,请重启游戏!");
  228. }
  229. }
  230. else
  231. {
  232. GameConfig.LoginAddress = "http://10.108.64.127:10005";
  233. }
  234. ET.Log.Debug($"===选择的渠道=== {_ui.m_boxChooseCanal.value} {GameConfig.LoginAddress}");
  235. }
  236. }
  237. }