LoginController.cs 9.5 KB

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