LoginController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using ET;
  2. using UnityEngine;
  3. namespace GFGGame
  4. {
  5. public class LoginController
  6. {
  7. public static string ageTipsTitle = "适龄提示说明";
  8. public static string ageTips = "1)本游戏是一款角色扮演类游戏,适用于年满16周岁及以上的用户,建议未成年人在家长监护下使用游戏产品。\n2)本游戏基于架空的故事背景和幻想世界观,剧情简单且积极向上,没有基于真实历史和现实事件的改编内容游戏玩法以收集不同服装进行搭配展开,有较为简单的竞技和对抗玩法,游戏中有基于文字的陌生人社交系统。\n3)本游戏中有用户实名认证系统,认证为未成年人的用户将接受以下管理:游戏中部分玩法和道具需要付费。未满8周岁的用户不能付费; 8周岁以上未满16周岁的未成年人用户,单次充值金额不得超过50元人民币,每月充值金额累计不得超过200元人民币; 16周岁以上的未成年人用户,单次充值金额不得超过100元人民币,每月充值金额累计不得超过400元人民币。\n4)本游戏以服装收集及搭配为主要玩法,有助于玩家开阔眼界、进一步提升审美能力。游戏中很多服装融入了中华传统元素,可以有助于中华传统文化的传播,体验中国的古典美。";
  9. public static void ShowLogin()
  10. {
  11. ViewManager.Show<LoginView>();
  12. }
  13. public static async ET.ETTask LoginTest(string account, bool cancelDelete = false)
  14. {
  15. ViewManager.Show<ModalStatusView>("登录中...");
  16. (int errorCode, long deleteTime) = await ET.LoginHelper.LoginTest(GameGlobal.zoneScene, GameConfig.LoginAddress, account, cancelDelete);
  17. if (errorCode == ET.ErrorCode.ERR_Success)
  18. {
  19. GameGlobal.isVisitor = false;
  20. ViewManager.Hide<LoginInputView>();
  21. ViewManager.Hide<RegisterView>();
  22. AccountInfoComponent accountInfoComponent = GameGlobal.zoneScene.GetComponent<AccountInfoComponent>();
  23. GameGlobal.userId = accountInfoComponent.AccountId;
  24. GameGlobal.userAge = accountInfoComponent.Age;
  25. if (GameGlobal.isVisitor)
  26. {
  27. //PlayerPrefs.SetFloat(GameConst.VISITOR_ID_KEY, GameGlobal.userId);
  28. }
  29. else
  30. {
  31. PlayerPrefs.SetString(GameConst.ACCOUNT_LAST_LOGIN_KEY, account);
  32. PlayerPrefs.SetString(GameConst.PASSWORD_LAST_LOGIN_KEY, "");
  33. }
  34. GameController.CheckSpecialAccount("sygfg");
  35. LocalCache.SetBool(GameConst.LAST_LOGIN_IS_VISITOR_KEY, GameGlobal.isVisitor);
  36. PlayerPrefs.Save();
  37. await OnLoginSuccess();
  38. }
  39. else
  40. {
  41. OnLoginFail(errorCode, account, deleteTime, null, false);
  42. }
  43. }
  44. public static async ET.ETTask Login(string account, string password, bool isMD5 = false, bool cancelDelete = false)
  45. {
  46. ViewManager.Show<ModalStatusView>("登录中...");
  47. (int errorCode, long deleteTime) = await ET.LoginHelper.Login(GameGlobal.zoneScene, GameConfig.LoginAddress, account, password, isMD5, cancelDelete);
  48. if (errorCode == ET.ErrorCode.ERR_Success)
  49. {
  50. GameGlobal.isVisitor = false;
  51. ViewManager.Hide<LoginInputView>();
  52. ViewManager.Hide<RegisterView>();
  53. AccountInfoComponent accountInfoComponent = GameGlobal.zoneScene.GetComponent<AccountInfoComponent>();
  54. GameGlobal.userId = accountInfoComponent.AccountId;
  55. GameGlobal.userAge = accountInfoComponent.Age;
  56. if (GameGlobal.isVisitor)
  57. {
  58. //PlayerPrefs.SetFloat(GameConst.VISITOR_ID_KEY, GameGlobal.userId);
  59. }
  60. else
  61. {
  62. var passwordMD5 = password;
  63. //密码禁止明文传输
  64. if (!isMD5)
  65. {
  66. passwordMD5 = MD5Helper.stringMD5(password);
  67. }
  68. PlayerPrefs.SetString(GameConst.ACCOUNT_LAST_LOGIN_KEY, account);
  69. PlayerPrefs.SetString(GameConst.PASSWORD_LAST_LOGIN_KEY, passwordMD5);
  70. }
  71. GameController.CheckSpecialAccount(account);
  72. LocalCache.SetBool(GameConst.LAST_LOGIN_IS_VISITOR_KEY, GameGlobal.isVisitor);
  73. PlayerPrefs.Save();
  74. await OnLoginSuccess();
  75. }
  76. else
  77. {
  78. OnLoginFail(errorCode, account, deleteTime, password, isMD5);
  79. }
  80. }
  81. private static async ETTask OnLoginSuccess()
  82. {
  83. await GetServerInfos();
  84. ServerInfosComponent serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  85. EventAgent.DispatchEvent(ConstMessage.SERVER_CHANGE, serverInfosComponent.CurrentServerId);
  86. EventAgent.DispatchEvent(ConstMessage.LOGIN_SUCCESS);
  87. if (AntiAddictionController.CheckAntiAddictionWhenLogin())
  88. {
  89. ViewManager.Hide<ModalStatusView>();
  90. return;
  91. }
  92. await ReqNoticeInfo();
  93. ViewManager.Hide<ModalStatusView>();
  94. }
  95. private static void OnLoginFail(int errorCode, string account, long deleteTime, string password, bool isMD5)
  96. {
  97. ViewManager.Hide<ModalStatusView>();
  98. if(errorCode == ErrorCode.ERR_LoginIsLimit && deleteTime > 0)
  99. {
  100. //删除账号提示
  101. AlertSystem.Show("账号注销处理中,若确定登录将取消账号注销,是否确定登录游戏?")
  102. .SetLeftButton(true, "取消", (o)=>
  103. {
  104. GameController.QuitToLoginView(true);
  105. EventAgent.DispatchEvent(ConstMessage.LOGIN_FAIL, account);
  106. })
  107. .SetRightButton(true, "确定", (o)=>
  108. {
  109. if(string.IsNullOrEmpty(password))
  110. {
  111. LoginTest(account, true).Coroutine();
  112. }
  113. else
  114. {
  115. Login(account, password, isMD5, true).Coroutine();
  116. }
  117. });
  118. return;
  119. }
  120. if (ErrorCodeController.Handler(errorCode))
  121. {
  122. GameController.QuitToLoginView(true);
  123. }
  124. EventAgent.DispatchEvent(ConstMessage.LOGIN_FAIL, account);
  125. }
  126. private static async ETTask ReqNoticeInfo()
  127. {
  128. int result = await LoginHelper.ReqGetLatestNotice();
  129. if (result == ErrorCode.ERR_Success)
  130. {
  131. NoticeInfo noticeInfo = NoticeDataManager.Instance.LastNoticeInfo;
  132. // Debug.Log("noticeTime:" + noticeInfo.time + " currentTime:" + (TimeInfo.Instance.ServerNow() / 1000));
  133. long noticeTime = TimeUtil.GetDayTimeBySec(noticeInfo.time, GlobalCfgArray.globalCfg.refreshTime);
  134. long currentTime = TimeUtil.GetDayTimeBySec(TimeInfo.Instance.ServerNow(), GlobalCfgArray.globalCfg.refreshTime);
  135. // Debug.Log("noticeTime:" + noticeTime + " currentTime:" + currentTime);
  136. if ((currentTime - noticeTime) / 1000 / TimeUtil.SECOND_PER_DAY < 3)
  137. {
  138. ViewManager.Show<SystemNoticeView>(new object[] { NoticeDataManager.Instance.LastNoticeInfo.title, NoticeDataManager.Instance.LastNoticeInfo.content });
  139. }
  140. }
  141. else
  142. {
  143. ErrorCodeController.Handler(result);
  144. }
  145. return;
  146. }
  147. public static async ETTask Register(string account, string password, string name, string identityNum, string code)
  148. {
  149. ViewManager.Show<ModalStatusView>("注册中...");
  150. int errorCode = await LoginHelper.Register(GameGlobal.zoneScene, GameConfig.LoginAddress, account, password, name, identityNum, code);
  151. if (errorCode == ErrorCode.ERR_Success)
  152. {
  153. Login(account, password).Coroutine();
  154. }
  155. else
  156. {
  157. ViewManager.Hide<ModalStatusView>();
  158. ErrorCodeController.Handler(errorCode);
  159. }
  160. }
  161. public static async ETTask GetServerInfos()
  162. {
  163. ViewManager.Show<ModalStatusView>("获取服务器列表...");
  164. int errorCode = await LoginHelper.GetServerInfos(GameGlobal.zoneScene);
  165. if (errorCode != ErrorCode.ERR_Success)
  166. {
  167. ViewManager.Hide<ModalStatusView>();
  168. ErrorCodeController.Handler(errorCode);
  169. await ETTask.Create();
  170. return;
  171. }
  172. var serverInfosComponent = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>();
  173. if (serverInfosComponent.ServerInfoList.Count <= 0)
  174. {
  175. AlertSystem.Show("服务器列表为空:\n请检查网络和服务器状态")
  176. .SetRightButton(true, "知道啦", (object data) =>
  177. {
  178. Application.Quit();
  179. });
  180. await ETTask.Create();
  181. return;
  182. }
  183. if (serverInfosComponent.CurrentServerId <= 0)
  184. {
  185. var serverInfo = serverInfosComponent.ServerInfoList[0];
  186. serverInfosComponent.CurrentServerId = int.Parse(serverInfo.Id.ToString());
  187. }
  188. }
  189. public static async ETTask GetRoles()
  190. {
  191. GameGlobal.OpenServerTime = GameGlobal.zoneScene.GetComponent<ServerInfosComponent>().GetCurrentServerInfo().OpenServerTime;
  192. ViewManager.Show<ModalStatusView>("获取角色信息...");
  193. int errorCode = await LoginHelper.GetRoles(GameGlobal.zoneScene);
  194. if (errorCode != ErrorCode.ERR_Success)
  195. {
  196. ErrorCodeController.Handler(errorCode);
  197. await ETTask.Create();
  198. }
  199. var roleInfosComponent = GameGlobal.zoneScene.GetComponent<RoleInfosComponent>();
  200. if (roleInfosComponent.RoleInfos != null && roleInfosComponent.RoleInfos.Count > 0)
  201. {
  202. var roleInfo = roleInfosComponent.RoleInfos[0];
  203. roleInfosComponent.CurrentRoleId = roleInfo.Id;
  204. await ReqEnterGame();
  205. }
  206. else
  207. {
  208. ViewManager.Hide<ModalStatusView>();
  209. //GameController.ShowCreateRole();不显示创角界面,在剧情中改名
  210. await ReqCreateRole();
  211. }
  212. }
  213. public static async ETTask<string> ReqRandomRoleName()
  214. {
  215. (int result, string name) = await LoginHelper.ReqRandomRoleName();
  216. if (result == ErrorCode.ERR_Success)
  217. {
  218. return name;
  219. }
  220. ErrorCodeController.Handler(result);
  221. return "";
  222. }
  223. public static async ETTask ReqCreateRole(string roleName = null)
  224. {
  225. if(string.IsNullOrEmpty(roleName))
  226. {
  227. roleName = await ReqRandomRoleName();
  228. }
  229. ViewManager.Show<ModalStatusView>("创建角色中...");
  230. int errorCode = await ET.LoginHelper.CreateRole(GameGlobal.zoneScene, roleName);
  231. if (errorCode != ErrorCode.ERR_Success)
  232. {
  233. ViewManager.Hide<ModalStatusView>();
  234. ErrorCodeController.Handler(errorCode);
  235. return;
  236. }
  237. ViewManager.Hide<CreateRoleView>();
  238. var roleInfosComponent = GameGlobal.zoneScene.GetComponent<RoleInfosComponent>();
  239. var roleInfo = roleInfosComponent.RoleInfos[0];
  240. roleInfosComponent.CurrentRoleId = roleInfo.Id;
  241. QDManager.OnCreateRole();
  242. await ReqEnterGame();
  243. }
  244. public static async ETTask ReqEnterGame()
  245. {
  246. BuglyAgent.SetUserId(RoleDataManager.roleName + VersionController.Instance.PackageVersion);
  247. ViewManager.Show<ModalStatusView>("请求进入游戏...");
  248. int errorCode = await LoginHelper.GetRealmKey(GameGlobal.zoneScene);
  249. if (errorCode != ErrorCode.ERR_Success)
  250. {
  251. ViewManager.Hide<ModalStatusView>();
  252. ErrorCodeController.Handler(errorCode);
  253. return;
  254. }
  255. errorCode = await LoginHelper.EnterGame(GameGlobal.zoneScene);
  256. if (errorCode != ErrorCode.ERR_Success)
  257. {
  258. ErrorCodeController.Handler(errorCode);
  259. return;
  260. }
  261. ViewManager.Hide<ModalStatusView>();
  262. ViewManager.Hide<LoginView>();
  263. ViewManager.Show<LoadingView>(0.01f);
  264. LoadingView.Instance.SetProgress(99);
  265. LoadingView.Instance.SetDesc("正在加载数据...");
  266. LogServerHelper.SendNodeLog((int)LogNode.StartEnterGame);
  267. await GameGlobal.zoneScene.GetComponent<ObjectWait>().Wait<ET.WaitType.Wait_SceneChangeFinish>();
  268. AlertSystem.Hide();
  269. await GameController.PreEnterGameAsync();
  270. LogServerHelper.SendNodeLog((int)LogNode.OnEnterGame);
  271. }
  272. public static async ETTask ReqReConnectGate()
  273. {
  274. LogUtil.LogDev("ReqReConnectGate");
  275. ViewManager.Show<ModalStatusView>("重新连接中...");
  276. int errorCode = await LoginHelper.EnterGame(GameGlobal.zoneScene);
  277. if (errorCode != ErrorCode.ERR_Success)
  278. {
  279. string errMessage = "与服务器连接失败。";
  280. Log.Error($"Reconnect fail, errorCode {errorCode}!");
  281. if(errorCode == ErrorCode.ERR_TokenError)
  282. {
  283. errMessage = "登录已过期,请重新登录";
  284. AlertSystem.Show(errMessage)
  285. .SetRightButton(true, "好的", (object data) =>
  286. {
  287. GameController.QuitToLoginView(false);
  288. });
  289. return;
  290. }
  291. else if(errorCode == ErrorCode.ERR_NetWorkError)
  292. {
  293. errMessage = "网络异常,与服务器连接失败。";
  294. }
  295. AlertSystem.Show(errMessage)
  296. .SetRightButton(true, "重新连接", (object data) =>
  297. {
  298. ReqReConnectGate().Coroutine();
  299. });
  300. return;
  301. }
  302. await GameGlobal.zoneScene.GetComponent<ObjectWait>().Wait<ET.WaitType.Wait_SceneChangeFinish>();
  303. EventAgent.DispatchEvent(ConstMessage.NUMERIC_CHANGE, NumericType.All);
  304. GameController.OnReconnected();
  305. AlertSystem.Hide();
  306. ViewManager.Hide<ModalStatusView>();
  307. GameController.CheckUpdateVersion();
  308. }
  309. }
  310. }