LoginProxy.cs 4.2 KB

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