LoginProxy.cs 4.1 KB

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