LoginController.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using ET;
  2. using UnityEngine;
  3. namespace GFGGame
  4. {
  5. public class LoginController
  6. {
  7. public static void ShowLogin()
  8. {
  9. LauncherView.Instance.Close();
  10. ViewManager.Show<LoginView>();
  11. }
  12. public static async ET.ETTask LoginTest(string account)
  13. {
  14. ViewManager.Show<ModalStatusView>("登录中...");
  15. int errorCode = await ET.LoginHelper.LoginTest(GameGlobal.zoneScene, ET.ConstValue.LoginAddress, account);
  16. if (errorCode == ET.ErrorCode.ERR_Success)
  17. {
  18. GameGlobal.isVisitor = false;
  19. ViewManager.Hide<LoginInputView>();
  20. ViewManager.Hide<RegisterView>();
  21. AccountInfoComponent accountInfoComponent = GameGlobal.zoneScene.GetComponent<AccountInfoComponent>();
  22. GameGlobal.userId = accountInfoComponent.AccountId;
  23. GameGlobal.userAge = accountInfoComponent.Age;
  24. if (GameGlobal.isVisitor)
  25. {
  26. //PlayerPrefs.SetFloat(GameConst.VISITOR_ID_KEY, GameGlobal.userId);
  27. }
  28. else
  29. {
  30. PlayerPrefs.SetString(GameConst.ACCOUNT_LAST_LOGIN_KEY, account);
  31. PlayerPrefs.SetString(GameConst.PASSWORD_LAST_LOGIN_KEY, "");
  32. }
  33. GameController.CheckSpecialAccount("sygfg");
  34. LocalCache.SetBool(GameConst.LAST_LOGIN_IS_VISITOR_KEY, GameGlobal.isVisitor);
  35. PlayerPrefs.Save();
  36. await OnLoginSuccess();
  37. }
  38. else
  39. {
  40. OnLoginFail(errorCode);
  41. }
  42. }
  43. public static async ET.ETTask Login(string account, string password, bool isMD5 = false)
  44. {
  45. ViewManager.Show<ModalStatusView>("登录中...");
  46. int errorCode = await ET.LoginHelper.Login(GameGlobal.zoneScene, ET.ConstValue.LoginAddress, account, password, isMD5);
  47. if (errorCode == ET.ErrorCode.ERR_Success)
  48. {
  49. GameGlobal.isVisitor = false;
  50. ViewManager.Hide<LoginInputView>();
  51. ViewManager.Hide<RegisterView>();
  52. AccountInfoComponent accountInfoComponent = GameGlobal.zoneScene.GetComponent<AccountInfoComponent>();
  53. GameGlobal.userId = accountInfoComponent.AccountId;
  54. GameGlobal.userAge = accountInfoComponent.Age;
  55. if (GameGlobal.isVisitor)
  56. {
  57. //PlayerPrefs.SetFloat(GameConst.VISITOR_ID_KEY, GameGlobal.userId);
  58. }
  59. else
  60. {
  61. var passwordMD5 = password;
  62. //密码禁止明文传输
  63. if (!isMD5)
  64. {
  65. passwordMD5 = MD5Helper.stringMD5(password);
  66. }
  67. PlayerPrefs.SetString(GameConst.ACCOUNT_LAST_LOGIN_KEY, account);
  68. PlayerPrefs.SetString(GameConst.PASSWORD_LAST_LOGIN_KEY, passwordMD5);
  69. }
  70. GameController.CheckSpecialAccount(account);
  71. LocalCache.SetBool(GameConst.LAST_LOGIN_IS_VISITOR_KEY, GameGlobal.isVisitor);
  72. PlayerPrefs.Save();
  73. await OnLoginSuccess();
  74. }
  75. else
  76. {
  77. OnLoginFail(errorCode);
  78. }
  79. }
  80. private static async ETTask OnLoginSuccess()
  81. {
  82. await GetServerInfos();
  83. ServerInfosComponent serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  84. EventAgent.DispatchEvent(ConstMessage.SERVER_CHANGE, serverInfosComponent.CurrentServerId);
  85. await ReqNoticeInfo();
  86. ViewManager.Hide<ModalStatusView>();
  87. }
  88. private static async ETTask ReqNoticeInfo()
  89. {
  90. bool result = await LoginHelper.ReqGetLatestNotice();
  91. if (result)
  92. {
  93. NoticeInfo noticeInfo = NoticeDataManager.Instance.LastNoticeInfo;
  94. Debug.Log("noticeTime:" + noticeInfo.time + " currentTime:" + (TimeInfo.Instance.ServerNow() / 1000));
  95. int noticeTime = TimeUtil.GetDayTimeBySec(noticeInfo.time * 1000, GlobalCfgArray.globalCfg.refreshTime);
  96. int currentTime = TimeUtil.GetDayTimeBySec(TimeInfo.Instance.ServerNow(), GlobalCfgArray.globalCfg.refreshTime);
  97. Debug.Log("noticeTime:" + noticeTime + " currentTime:" + currentTime);
  98. if ((currentTime - noticeTime) / TimeUtil.SECOND_PER_DAY < 3)
  99. {
  100. ViewManager.Show<SystemNoticeView>();
  101. }
  102. }
  103. return;
  104. }
  105. private static void OnLoginFail(int errorCode)
  106. {
  107. ViewManager.Hide<ModalStatusView>();
  108. if (ErrorCodeController.Handler(errorCode))
  109. {
  110. ViewManager.Show<LoginInputView>();
  111. }
  112. }
  113. public static void Logout()
  114. {
  115. GameGlobal.zoneScene.GetComponent<SessionComponent>().AccountSession?.Dispose();
  116. GameGlobal.zoneScene.GetComponent<SessionComponent>().Session?.Dispose();
  117. }
  118. public static async ETTask Register(string account, string password, string name, string identityNum, string code)
  119. {
  120. ViewManager.Show<ModalStatusView>("注册中...");
  121. int errorCode = await LoginHelper.Register(GameGlobal.zoneScene, ET.ConstValue.LoginAddress, account, password, name, identityNum, code);
  122. if (errorCode == ErrorCode.ERR_Success)
  123. {
  124. Login(account, password).Coroutine();
  125. }
  126. else
  127. {
  128. ViewManager.Hide<ModalStatusView>();
  129. ErrorCodeController.Handler(errorCode);
  130. }
  131. }
  132. public static async ETTask GetServerInfos()
  133. {
  134. int errorCode = await LoginHelper.GetServerInfos(GameGlobal.zoneScene);
  135. if (errorCode != ErrorCode.ERR_Success)
  136. {
  137. ErrorCodeController.Handler(errorCode);
  138. await ETTask.Create();
  139. return;
  140. }
  141. var serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  142. if (serverInfosComponent.ServerInfoList.Count <= 0)
  143. {
  144. Alert.Show("服务器列表为空:\n请检查网络和服务器状态")
  145. .SetRightButton(true, "知道啦", (object data) =>
  146. {
  147. Application.Quit();
  148. });
  149. await ETTask.Create();
  150. return;
  151. }
  152. if (serverInfosComponent.CurrentServerId <= 0)
  153. {
  154. var serverInfo = serverInfosComponent.ServerInfoList[0];
  155. serverInfosComponent.CurrentServerId = int.Parse(serverInfo.Id.ToString());
  156. }
  157. }
  158. public static async ETTask GetRoles()
  159. {
  160. int errorCode = await LoginHelper.GetRoles(GameGlobal.zoneScene);
  161. if (errorCode != ErrorCode.ERR_Success)
  162. {
  163. ErrorCodeController.Handler(errorCode);
  164. await ETTask.Create();
  165. }
  166. var roleInfosComponent = GameGlobal.zoneScene.GetComponent<RoleInfosComponent>();
  167. if (roleInfosComponent.RoleInfos != null && roleInfosComponent.RoleInfos.Count > 0)
  168. {
  169. var roleInfo = roleInfosComponent.RoleInfos[0];
  170. roleInfosComponent.CurrentRoleId = roleInfo.Id;
  171. await ReqEnterGame();
  172. }
  173. else
  174. {
  175. GameGlobal.isFirstEntry = true;
  176. ViewManager.Hide<ModalStatusView>();
  177. GameController.ShowCreateRole();
  178. }
  179. }
  180. public static async ETTask ReqCreateRole(string roleName)
  181. {
  182. ViewManager.Show<ModalStatusView>("创建角色中...");
  183. int errorCode = await ET.LoginHelper.CreateRole(GameGlobal.zoneScene, roleName);
  184. if (errorCode != ErrorCode.ERR_Success)
  185. {
  186. ViewManager.Hide<ModalStatusView>();
  187. ErrorCodeController.Handler(errorCode);
  188. return;
  189. }
  190. ViewManager.Hide(ViewName.CREATE_ROLE_VIEW);
  191. var roleInfosComponent = GameGlobal.zoneScene.GetComponent<RoleInfosComponent>();
  192. var roleInfo = roleInfosComponent.RoleInfos[0];
  193. roleInfosComponent.CurrentRoleId = roleInfo.Id;
  194. await ReqEnterGame();
  195. }
  196. public static async ETTask ReqEnterGame()
  197. {
  198. ViewManager.Hide<LoginView>();
  199. ViewManager.Show<LoadingView>();
  200. // LoadingView.Instance.Open();
  201. LoadingView.Instance.SetProgress(99);
  202. LoadingView.Instance.SetDesc("正在进入游戏");
  203. LogServerHelperHttp.SendNodeLog((int)LogNode.StartEnterGame);
  204. int errorCode = await LoginHelper.GetRealmKey(GameGlobal.zoneScene);
  205. if (errorCode != ErrorCode.ERR_Success)
  206. {
  207. ErrorCodeController.Handler(errorCode);
  208. return;
  209. }
  210. errorCode = await LoginHelper.EnterGame(GameGlobal.zoneScene);
  211. if (errorCode != ErrorCode.ERR_Success)
  212. {
  213. ErrorCodeController.Handler(errorCode);
  214. return;
  215. }
  216. await GameController.PreEnterGameAsync();
  217. ViewManager.Hide<ModalStatusView>();
  218. LogServerHelperHttp.SendNodeLog((int)LogNode.OnEnterGame);
  219. }
  220. }
  221. }