GatewayHttp.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using LitJson;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.Networking;
  8. public class GatewayHttp : MonoBehaviour
  9. {
  10. string baseUrl = "https://api-fat1.ghz-tech.com/";
  11. string username = "unity";
  12. string password = "unitylisten";
  13. Dictionary<string, string> requestHeader = new Dictionary<string, string>(); // header
  14. // Start is called before the first frame update
  15. void Start()
  16. {
  17. Debug.Log("Start Queue SendHttp Start" + Application.version);
  18. Dictionary<string,string> data = new Dictionary<string, string>();
  19. data.Add("username", username);
  20. data.Add("password", password);
  21. StartCoroutine(SendHttp("nacos/v1/auth/users/login", data,(string json)=> {
  22. Debug.Log(json);
  23. JsonData jdata = JsonMapper.ToObject(json);
  24. Dictionary<string, string> data = new Dictionary<string, string>();
  25. data.Add("dataId",Application.version);
  26. data.Add("group", Application.productName);
  27. data.Add("accessToken", jdata["accessToken"].ToString());
  28. StartCoroutine(SendHttp("nacos/v1/cs/configs", data, (string json) =>
  29. {
  30. JsonData backData = JsonMapper.ToObject(json);
  31. HttpSDKAction.Instance.jsonData = json;
  32. string pt = "pro";
  33. #if UNITY_EDITOR
  34. pt = "dev";
  35. #endif
  36. HttpSDKAction.Instance.loginUrl = backData["unity"]["url"][pt]["login"].ToString();
  37. HttpSDKAction.Instance.managerUrl = backData["unity"]["url"][pt]["manager"].ToString();
  38. HttpSDKAction.Instance.officeSocket = backData["unity"]["url"][pt]["officeSocket"].ToString();
  39. HttpSDKAction.Instance.pointCloudUrl = backData["unity"]["url"][pt]["pointCloud"].ToString();
  40. HttpSDKAction.Instance.roomSocket = backData["unity"]["url"][pt]["roomSocket"].ToString();
  41. },false));
  42. }));
  43. }
  44. void test()
  45. {
  46. JsonData data = new JsonData();
  47. data["unity"] = new JsonData();
  48. data["unity"]["url"] = new JsonData();
  49. data["unity"]["devuser"] = new JsonData();
  50. data["unity"]["url"]["dev"] = new JsonData();
  51. data["unity"]["url"]["fat"] = new JsonData();
  52. data["unity"]["url"]["pro"] = new JsonData();
  53. data["unity"]["url"]["dev"]["login"] = "";
  54. data["unity"]["url"]["dev"]["manager"] = "";
  55. data["unity"]["url"]["dev"]["pointCloud"] = "";
  56. data["unity"]["url"]["dev"]["roomSocket"] = "";
  57. data["unity"]["url"]["dev"]["officeSocket"] = "";
  58. data["unity"]["url"]["fat"]["login"] = "";
  59. data["unity"]["url"]["fat"]["manager"] = "";
  60. data["unity"]["url"]["fat"]["pointCloud"] = "";
  61. data["unity"]["url"]["fat"]["roomSocket"] = "";
  62. data["unity"]["url"]["fat"]["officeSocket"] = "";
  63. data["unity"]["url"]["pro"]["login"] = "";
  64. data["unity"]["url"]["pro"]["manager"] = "";
  65. data["unity"]["url"]["pro"]["pointCloud"] = "";
  66. data["unity"]["url"]["pro"]["roomSocket"] = "";
  67. data["unity"]["url"]["pro"]["officeSocket"] = "";
  68. data["unity"]["user"]["111@111.com"] = "dev";
  69. Debug.Log(data.ToJson());
  70. }
  71. public void initHead()
  72. {
  73. requestHeader.Clear();
  74. requestHeader.Add("Content-Type", "application/x-www-form-urlencoded");
  75. }
  76. public IEnumerator SendHttp(string methodName, Dictionary<string, string> fromDic, Action<string> CallBack,bool isPost=true)
  77. {
  78. Debug.Log("Start Queue SendHttp " + methodName);
  79. string url = baseUrl+ methodName;
  80. UnityWebRequest webRequest;
  81. if (!isPost)
  82. {
  83. url += "?";
  84. foreach (var item in fromDic)
  85. {
  86. url+= item.Key +"="+item.Value+ "&";
  87. }
  88. webRequest = UnityWebRequest.Get(url);
  89. }
  90. else
  91. {
  92. WWWForm form = new WWWForm();
  93. foreach (var item in fromDic)
  94. {
  95. form.AddField(item.Key, item.Value);
  96. }
  97. webRequest = UnityWebRequest.Post(url, form);
  98. webRequest.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  99. }
  100. yield return webRequest.SendWebRequest();
  101. if (webRequest.result == UnityWebRequest.Result.ConnectionError)
  102. {
  103. Debug.LogError(webRequest.error);
  104. }
  105. else
  106. {
  107. string result = webRequest.downloadHandler.text;
  108. CallBack(result);
  109. }
  110. }
  111. }