LoginView.cs 8.2 KB

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