LoginController.cs 16 KB

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