LoginProxy.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using UnityEngine;
  2. using System;
  3. using LitJson;
  4. namespace GFGGame
  5. {
  6. public class LoginProxy
  7. {
  8. private const string API_HI = "hi";
  9. private const string API_LOGIN = "login";
  10. private const string API_LOGIN_AS_VISITOR = "loginAsVisitor";
  11. private const string API_REGISTER = "register";
  12. public static void SayHi(Action<LoginResult> callback)
  13. {
  14. //ViewManager.Show(ViewName.MODAL_STATUS_VIEW, "连接中...");
  15. ViewManager.Show<ModalStatusView>("连接中...");
  16. Get(API_HI, (LoginResult result) => {
  17. ViewManager.Hide<ModalStatusView>();
  18. if(result.version == GameConst.SERVER_VERSION || Application.isEditor)
  19. {
  20. callback(result);
  21. }
  22. else
  23. {
  24. Alert.Show("版本已更新,请联系研发获取最新版本")
  25. .SetLeftButton(true, "知道了", (data) => {
  26. Application.Quit();
  27. }); ;
  28. }
  29. });
  30. }
  31. public static void Login(string account, string password)
  32. {
  33. ViewManager.Show<ModalStatusView>("登录中...");
  34. string data = JsonUtil.createJsonStr("account", account, "password", password);
  35. Post(API_LOGIN, data, (LoginResult result) => {
  36. ViewManager.Hide<ModalStatusView>();
  37. if(result == null || result.code == 0) {
  38. GameGlobal.isVisitor = false;
  39. GameController.OnLoginSuccess(result);
  40. } else {
  41. GameController.OnLoginFail(result);
  42. }
  43. });
  44. }
  45. public static void LoginAsVisitor()
  46. {
  47. ViewManager.Show<ModalStatusView>("登录中...");
  48. long id = (long)PlayerPrefs.GetFloat(GameConst.VISITOR_ID_KEY, -1);
  49. string data = JsonUtil.createJsonStr("id", "" + id);
  50. Post(API_LOGIN_AS_VISITOR, data, (LoginResult result) => {
  51. ViewManager.Hide<ModalStatusView>();
  52. if(result == null || result.code == 0)
  53. {
  54. GameGlobal.isVisitor = true;
  55. GameController.OnLoginSuccess(result);
  56. } else {
  57. GameController.OnLoginFail(result);
  58. }
  59. });
  60. }
  61. public static void Register(string account, string password, string name, string identityNum, string code)
  62. {
  63. ViewManager.Show<ModalStatusView>("注册中...");
  64. string data = JsonUtil.createJsonStr("account", account, "password", password, "name", name, "identityNum", identityNum, "code", code);
  65. Post(API_REGISTER, data, (LoginResult result) => {
  66. ViewManager.Hide<ModalStatusView>();
  67. if(result == null || result.code == 0)
  68. {
  69. GameGlobal.isVisitor = false;
  70. GameController.OnLoginSuccess(result);
  71. } else {
  72. GameController.OnLoginFail(result);
  73. }
  74. });
  75. }
  76. private static void Get(string methodName, Action<LoginResult> callback)
  77. {
  78. HttpTool.Instance.Get(GameGlobal.loginApiUrl, methodName, (string data) => {
  79. ResultHandler(data, callback);
  80. });
  81. }
  82. private static void Post(string methodName, string jsonString, Action<LoginResult> callback)
  83. {
  84. HttpTool.Instance.Post(GameGlobal.loginApiUrl, methodName, jsonString, (string data) => {
  85. ResultHandler(data, callback);
  86. });
  87. }
  88. private static void ResultHandler(string data, Action<LoginResult> callback)
  89. {
  90. LoginResult resultObj = null;
  91. if(data != null)
  92. {
  93. resultObj = JsonMapper.ToObject<LoginResult>(data);
  94. }
  95. if(resultObj != null && resultObj.code != 0 && resultObj.message != null && resultObj.message.Length > 0)
  96. {
  97. PromptController.Instance.ShowFloatTextPrompt(resultObj.message);
  98. }
  99. if (callback != null)
  100. {
  101. callback(resultObj);
  102. }
  103. }
  104. }
  105. }