GatewayHttp.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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-cs.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. string pname = Application.productName.Replace(" ", "");
  26. data.Add("dataId", Application.version);
  27. data.Add("group", pname);
  28. Debug.Log("login==>" + pname);
  29. Debug.Log("login==>" + Application.version);
  30. data.Add("accessToken", jdata["accessToken"].ToString());
  31. StartCoroutine(SendHttp("nacos/v1/cs/configs", data, (string json) =>
  32. {
  33. JsonData backData = JsonMapper.ToObject(json);
  34. HttpSDKAction.Instance.jsonData = json;
  35. string pt = "pro";
  36. #if UNITY_EDITOR
  37. pt = "dev";
  38. #endif
  39. HttpSDKAction.Instance.loginUrl = backData["unity"]["url"][pt]["login"].ToString();
  40. HttpSDKAction.Instance.managerUrl = backData["unity"]["url"][pt]["manager"].ToString();
  41. HttpSDKAction.Instance.officeSocket = backData["unity"]["url"][pt]["officeSocket"].ToString();
  42. HttpSDKAction.Instance.pointCloudUrl = backData["unity"]["url"][pt]["pointCloud"].ToString();
  43. HttpSDKAction.Instance.roomSocket = backData["unity"]["url"][pt]["roomSocket"].ToString();
  44. }, false));
  45. }));
  46. }
  47. void test()
  48. {
  49. JsonData data = new JsonData();
  50. data["unity"] = new JsonData();
  51. data["unity"]["url"] = new JsonData();
  52. data["unity"]["devuser"] = new JsonData();
  53. data["unity"]["url"]["dev"] = new JsonData();
  54. data["unity"]["url"]["fat"] = new JsonData();
  55. data["unity"]["url"]["pro"] = new JsonData();
  56. data["unity"]["url"]["dev"]["login"] = "";
  57. data["unity"]["url"]["dev"]["manager"] = "";
  58. data["unity"]["url"]["dev"]["pointCloud"] = "";
  59. data["unity"]["url"]["dev"]["roomSocket"] = "";
  60. data["unity"]["url"]["dev"]["officeSocket"] = "";
  61. data["unity"]["url"]["fat"]["login"] = "";
  62. data["unity"]["url"]["fat"]["manager"] = "";
  63. data["unity"]["url"]["fat"]["pointCloud"] = "";
  64. data["unity"]["url"]["fat"]["roomSocket"] = "";
  65. data["unity"]["url"]["fat"]["officeSocket"] = "";
  66. data["unity"]["url"]["pro"]["login"] = "";
  67. data["unity"]["url"]["pro"]["manager"] = "";
  68. data["unity"]["url"]["pro"]["pointCloud"] = "";
  69. data["unity"]["url"]["pro"]["roomSocket"] = "";
  70. data["unity"]["url"]["pro"]["officeSocket"] = "";
  71. data["unity"]["user"]["111@111.com"] = "dev";
  72. Debug.Log(data.ToJson());
  73. }
  74. public void initHead()
  75. {
  76. requestHeader.Clear();
  77. requestHeader.Add("Content-Type", "application/x-www-form-urlencoded");
  78. }
  79. public IEnumerator SendHttp(string methodName, Dictionary<string, string> fromDic, Action<string> CallBack, bool isPost = true)
  80. {
  81. Debug.Log("Start Queue SendHttp " + methodName);
  82. string url = baseUrl + methodName;
  83. UnityWebRequest webRequest;
  84. if (!isPost)
  85. {
  86. url += "?";
  87. foreach (var item in fromDic)
  88. {
  89. url += item.Key + "=" + item.Value + "&";
  90. }
  91. webRequest = UnityWebRequest.Get(url);
  92. }
  93. else
  94. {
  95. WWWForm form = new WWWForm();
  96. foreach (var item in fromDic)
  97. {
  98. form.AddField(item.Key, item.Value);
  99. }
  100. webRequest = UnityWebRequest.Post(url, form);
  101. webRequest.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  102. }
  103. yield return webRequest.SendWebRequest();
  104. if (webRequest.result == UnityWebRequest.Result.ConnectionError)
  105. {
  106. Debug.LogError(webRequest.error);
  107. }
  108. else
  109. {
  110. string result = webRequest.downloadHandler.text;
  111. CallBack(result);
  112. }
  113. }
  114. }