LoginView.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using UnityEngine;
  2. using FairyGUI;
  3. using UI.Login;
  4. using ET;
  5. using GFGGame.Launcher;
  6. using System;
  7. using SimpleJSON;
  8. namespace GFGGame
  9. {
  10. public class LoginView : BaseView
  11. {
  12. private UI_LoginUI _ui;
  13. private GameObject _sceneObject;
  14. private bool autoLogined;
  15. public override void Dispose()
  16. {
  17. if (_sceneObject != null)
  18. {
  19. GameObject.DestroyImmediate(_sceneObject);
  20. _sceneObject = null;
  21. }
  22. if (_ui != null)
  23. {
  24. _ui.Dispose();
  25. _ui = null;
  26. }
  27. base.Dispose();
  28. }
  29. protected override void Init()
  30. {
  31. base.Init();
  32. packageName = UI_LoginUI.PACKAGE_NAME;
  33. _ui = UI_LoginUI.Create();
  34. viewCom = _ui.target;
  35. isfullScreen = true;
  36. }
  37. protected override void OnInit()
  38. {
  39. base.OnInit();
  40. _ui.m_txtVersion.text = GameGlobal.version;
  41. _ui.m_btnNotice.onClick.Add(OnClickBtnNotice);
  42. _ui.m_btnStart.onClick.Add(OnClickBtnStart);
  43. _ui.m_btnAge.onClick.Add(OnClickBtnAge);
  44. _ui.m_btnChange.visible = false;
  45. _ui.m_btnChange.onClick.Add(OnBtnChangeClick);
  46. _ui.m_loaEventa.onClick.Add(() => { FullScreenTextController.Show("event:a"); });
  47. _ui.m_loaEventb.onClick.Add(() => { FullScreenTextController.Show("event:b"); });
  48. _ui.m_loaEventc.onClick.Add(() => { FullScreenTextController.Show("event:c"); });
  49. _ui.m_btnAgree.onClick.Add(OnBtnAgreeCklick);
  50. }
  51. protected override void AddEventListener()
  52. {
  53. base.AddEventListener();
  54. EventAgent.AddEventListener(ConstMessage.SERVER_CHANGE, OnSeverChangeListener);
  55. }
  56. protected override void OnShown()
  57. {
  58. base.OnShown();
  59. _ui.m_btnStart.visible = false;
  60. _ui.m_btnStart.text = "登录中...";
  61. MusicManager.Instance.PlayCroutine(ResPathUtil.GetMusicPath(ConstMusicName.DEFAULT));
  62. if (_sceneObject == null)
  63. {
  64. PrefabManager.Instance.InstantiateAsync(
  65. ResPathUtil.GetLoginResPath("LoginSkin1/SceneLogin"),
  66. (go) =>
  67. {
  68. _sceneObject = go;
  69. if (_sceneObject != null)
  70. {
  71. // 场景对象加载完成后的后续初始化
  72. ContinueAfterSceneLoad();
  73. }
  74. else
  75. {
  76. Debug.LogError("Failed to instantiate login scene");
  77. }
  78. });
  79. }
  80. else
  81. {
  82. // 如果场景对象已存在,直接继续初始化
  83. ContinueAfterSceneLoad();
  84. }
  85. }
  86. private void ContinueAfterSceneLoad()
  87. {
  88. _ui.m_btnAgree.selected = LocalCache.GetBool(GameConst.LAST_LOGIN_IS_VISITOR_KEY, false);
  89. _ui.m_btnAgree.selected = true;
  90. if (GameConfig.openTime > TimeHelper.ClientNow())
  91. {
  92. var date = DateTimeUtil.LongTimeStampToDateTime(GameConfig.openTime);
  93. string minuteText = "";
  94. if (date.Minute > 0)
  95. {
  96. minuteText = date.Minute + "分";
  97. }
  98. _ui.m_btnStart.text = "";
  99. AlertUI.Show($" 亲爱的研究员,万世镜将于{date.Year}年{date.Month}月{date.Day}日{date.Hour}点{minuteText}开放,敬请期待!")
  100. .SetLeftButton(true, "好的", (obj) => { Application.Quit(); });
  101. return;
  102. }
  103. if (!autoLogined)
  104. {
  105. autoLogined = true;
  106. TryLogin(a =>
  107. {
  108. ServerInfosComponent serverInfosComponent =
  109. GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  110. ServerInfo recentlyServerInfo = serverInfosComponent.recentlyServerInfo;
  111. UpdateServer(recentlyServerInfo);
  112. });
  113. }
  114. }
  115. protected override void OnHide()
  116. {
  117. autoLogined = false;
  118. if (_sceneObject != null)
  119. {
  120. PrefabManager.Instance.Restore(_sceneObject);
  121. _sceneObject = null;
  122. }
  123. base.OnHide();
  124. }
  125. protected override void RemoveEventListener()
  126. {
  127. base.RemoveEventListener();
  128. EventAgent.RemoveEventListener(ConstMessage.SERVER_CHANGE, OnSeverChangeListener);
  129. }
  130. private void UpdateServer(ServerInfo info)
  131. {
  132. ServerInfosComponent serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  133. if (serverInfosComponent.ServerInfoList.Count == 0)
  134. {
  135. _ui.m_btnChange.visible = false;
  136. return;
  137. }
  138. _ui.m_btnChange.visible = serverInfosComponent.ServerInfoList.Count > 1;
  139. _ui.m_btnChange.title =
  140. string.Format("{0}区 {1}", NumberUtil.GetChiniseNumberText((int)info.Id), info.ServerName);
  141. LogServerHelperHttp.SendNodeLog((int)LogNode.ShowSelectServer);
  142. }
  143. private void OnSeverChangeListener(EventContext context)
  144. {
  145. ServerInfosComponent serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  146. UpdateServer(serverInfosComponent.ServerInfoList[serverInfosComponent.CurrentServerId]);
  147. }
  148. private void OnBtnChangeClick()
  149. {
  150. ViewManager.Show<ServerListView>();
  151. }
  152. private void OnClickBtnNotice()
  153. {
  154. if (NoticeDataManager.Instance.LastNoticeInfo == null ||
  155. NoticeDataManager.Instance.LastNoticeInfo.noticeId == 0)
  156. {
  157. PromptController.Instance.ShowFloatTextPrompt("暂无公告发布");
  158. return;
  159. }
  160. ViewManager.Show<SystemNoticeView>(new object[]
  161. { NoticeDataManager.Instance.LastNoticeInfo.title, NoticeDataManager.Instance.LastNoticeInfo.content });
  162. }
  163. private void OnClickBtnStart()
  164. {
  165. LoginController.GetRoles().Coroutine();
  166. }
  167. private void OnClickBtnAge()
  168. {
  169. ViewManager.Show<SystemNoticeView>(new object[] { LoginController.ageTipsTitle, LoginController.ageTips });
  170. }
  171. private void TryLogin(Action<bool> action)
  172. {
  173. var url = LauncherConfig.cfgUrl.Replace("{cfgName}", LauncherConfig.cfgName);
  174. Debug.Log($"TryLogin, {url}");
  175. url = url + "?t=" + DateTime.Now.Ticks;
  176. //HttpTool.Instance.Get(url, json =>
  177. //{
  178. // LauncherConfig.InitPlatform(json);
  179. // if (LauncherConfig.serverStatus == 1)
  180. // {
  181. // if (string.IsNullOrEmpty(LauncherConfig.statusPrompt))
  182. // {
  183. // _ui.m_btnStart.text = "维护中";
  184. // LauncherConfig.statusPrompt = "游戏正在更新维护中,请稍后再试。";
  185. // }
  186. // AlertSystem.Show(LauncherConfig.statusPrompt)
  187. // .SetLeftButton(true, "知道了", (data) => { Application.Quit(); });
  188. // return;
  189. // }
  190. // if (!_ui.m_btnAgree.selected)
  191. // {
  192. // PromptController.Instance.ShowFloatTextPrompt("请仔细阅读并同意游戏用户协议、隐私保护指引、儿童隐私政策");
  193. // return;
  194. // }
  195. // LoginHelper.H5Login(GameGlobal.zoneScene, GameConfig.LoginAddress,
  196. // error =>
  197. // {
  198. // LoginController.OnLoginSuccess(action).Coroutine();
  199. // _ui.m_btnStart.visible = true;
  200. // _ui.m_btnStart.text = "开始游戏";
  201. // }).Coroutine();
  202. //});
  203. //-------------------测试--------
  204. string data = "{\"loginApiUrl\":\"129.204.4.238:11005\",\"platformName\":\"\",\"showGM\":\"1\",\"tsStatus\":\"0\",\"openTime\":\"[2024][6][19][05:00:00]\",\"tsServer\":\"1\"}";
  205. LauncherConfig.InitPlatform(data);
  206. if (LauncherConfig.serverStatus == 1)
  207. {
  208. if (string.IsNullOrEmpty(LauncherConfig.statusPrompt))
  209. {
  210. _ui.m_btnStart.text = "维护中";
  211. LauncherConfig.statusPrompt = "游戏正在更新维护中,请稍后再试。";
  212. }
  213. AlertSystem.Show(LauncherConfig.statusPrompt)
  214. .SetLeftButton(true, "知道了", (data) => { Application.Quit(); });
  215. return;
  216. }
  217. if (!_ui.m_btnAgree.selected)
  218. {
  219. PromptController.Instance.ShowFloatTextPrompt("请仔细阅读并同意游戏用户协议、隐私保护指引、儿童隐私政策");
  220. return;
  221. }
  222. LoginHelper.H5Login(GameGlobal.zoneScene, GameConfig.LoginAddress,
  223. error =>
  224. {
  225. LoginController.OnLoginSuccess(action).Coroutine();
  226. _ui.m_btnStart.visible = true;
  227. _ui.m_btnStart.text = "开始游戏";
  228. }).Coroutine();
  229. }
  230. private void OnBtnAgreeCklick()
  231. {
  232. LocalCache.SetBool(LauncherConfig.LAST_LOGIN_IS_AGREE_KEY, _ui.m_btnAgree.selected);
  233. }
  234. }
  235. }