|
@@ -14,11 +14,14 @@ using Newtonsoft.Json.Linq;
|
|
|
|
|
|
public class HttpTool : MonoSingleton<HttpTool>
|
|
|
{
|
|
|
+ public static bool is20 = false;
|
|
|
|
|
|
-
|
|
|
+ private string baseUrl20 = "https://office.ghz-tech.com:3424/api";
|
|
|
+
|
|
|
+
|
|
|
private string baseUrl = "http://office.ghz-tech.com:9904/api";
|
|
|
private string UrlPointCloud = "https://pro.qulivr.com/mr-navigator/v1";
|
|
|
- private string token = "";
|
|
|
+
|
|
|
Dictionary<string, string> requestHeader = new Dictionary<string, string>();
|
|
|
void Awake()
|
|
|
{
|
|
@@ -27,10 +30,10 @@ public class HttpTool : MonoSingleton<HttpTool>
|
|
|
|
|
|
}
|
|
|
|
|
|
- public string Token
|
|
|
- {
|
|
|
- get { return token; }
|
|
|
- }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
public string BaseUrl
|
|
|
{
|
|
@@ -45,76 +48,12 @@ public class HttpTool : MonoSingleton<HttpTool>
|
|
|
|
|
|
public void Get(string methodName, Action<string> callback)
|
|
|
{
|
|
|
- StartCoroutine(GetRequest(methodName, callback));
|
|
|
- }
|
|
|
- private IEnumerator GetRequest(string methodName, Action<string> callback)
|
|
|
- {
|
|
|
-
|
|
|
- string url = baseUrl + methodName;
|
|
|
-
|
|
|
- using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
|
|
|
- {
|
|
|
- webRequest.SetRequestHeader("authorization", token);
|
|
|
-
|
|
|
- foreach (var v in requestHeader)
|
|
|
- {
|
|
|
- webRequest.SetRequestHeader(v.Key, v.Value);
|
|
|
- }
|
|
|
- yield return webRequest.SendWebRequest();
|
|
|
-
|
|
|
- if (webRequest.isHttpError || webRequest.isNetworkError)
|
|
|
- {
|
|
|
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
|
|
|
- if (callback != null)
|
|
|
- {
|
|
|
- callback(null);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (callback != null)
|
|
|
- {
|
|
|
-
|
|
|
- callback(webRequest.downloadHandler.text);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ StartCoroutine(SendHttp(methodName,"", callback,false, false));
|
|
|
}
|
|
|
|
|
|
- public void GetPoint(string methodName, Action<string> callback)
|
|
|
+ public void GetPoint(string methodName, Action<string> callback,bool isCloud=false)
|
|
|
{
|
|
|
- StartCoroutine(GetPointRequest(methodName, callback));
|
|
|
- }
|
|
|
- private IEnumerator GetPointRequest(string methodName, Action<string> callback)
|
|
|
- {
|
|
|
- string url = UrlPointCloud + methodName;
|
|
|
-
|
|
|
- using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
|
|
|
- {
|
|
|
- webRequest.SetRequestHeader("authorization", token);
|
|
|
-
|
|
|
- foreach (var v in requestHeader)
|
|
|
- {
|
|
|
- webRequest.SetRequestHeader(v.Key, v.Value);
|
|
|
- }
|
|
|
- yield return webRequest.SendWebRequest();
|
|
|
-
|
|
|
- if (webRequest.isHttpError || webRequest.isNetworkError)
|
|
|
- {
|
|
|
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
|
|
|
- if (callback != null)
|
|
|
- {
|
|
|
- callback(null);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (callback != null)
|
|
|
- {
|
|
|
- callback(webRequest.downloadHandler.text);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ StartCoroutine(SendHttp(methodName,"", callback,false, isCloud));
|
|
|
}
|
|
|
|
|
|
public void GetPointFile(string methodName, string projectId, string fileType, Action<string> callback)
|
|
@@ -124,228 +63,100 @@ public class HttpTool : MonoSingleton<HttpTool>
|
|
|
UriBuilder uriBuilder = new UriBuilder(url);
|
|
|
|
|
|
uriBuilder.Query = "projectId=" + projectId + "&fileType=" + fileType;
|
|
|
-
|
|
|
- UnityWebRequest webRequest = UnityWebRequest.Get(uriBuilder.Uri);
|
|
|
- webRequest.SetRequestHeader("Authorization", token);
|
|
|
-
|
|
|
- StartCoroutine(GetPointGetPointFileRequest(webRequest, callback));
|
|
|
- }
|
|
|
- private IEnumerator GetPointGetPointFileRequest(UnityWebRequest webRequest, Action<string> callback)
|
|
|
- {
|
|
|
- yield return webRequest.SendWebRequest();
|
|
|
|
|
|
- if (webRequest.result != UnityWebRequest.Result.Success)
|
|
|
- {
|
|
|
- Debug.LogError("GetPointRequest failed: " + webRequest.error);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
-
|
|
|
- callback?.Invoke(webRequest.downloadHandler.text);
|
|
|
- }
|
|
|
+ StartCoroutine(SendHttp(uriBuilder.Uri.ToString(),"", callback,false));
|
|
|
}
|
|
|
public void PostTest(string methodName, string jsonString, Action<string> CallBack)
|
|
|
{
|
|
|
- StartCoroutine(PostRequestTest(methodName, jsonString, CallBack));
|
|
|
+ StartCoroutine(SendHttp(methodName, jsonString, CallBack));
|
|
|
}
|
|
|
- private IEnumerator PostRequestTest(string methodName, string jsonString, Action<string> CallBack)
|
|
|
+ public void Post(string methodName, string jsonString, Action<string> CallBack)
|
|
|
{
|
|
|
- string url = baseUrl + methodName;
|
|
|
- Debug.Log(url + jsonString);
|
|
|
-
|
|
|
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
|
|
|
- {
|
|
|
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
|
|
|
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
|
|
|
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
|
|
|
-
|
|
|
- webRequest.SetRequestHeader("authorization", token);
|
|
|
- foreach (var v in requestHeader)
|
|
|
- {
|
|
|
- webRequest.SetRequestHeader(v.Key, v.Value);
|
|
|
- Debug.Log(v.Value + " " + methodName);
|
|
|
- }
|
|
|
- yield return webRequest.SendWebRequest();
|
|
|
-
|
|
|
- if (webRequest.isHttpError || webRequest.isNetworkError)
|
|
|
- {
|
|
|
- CallBack?.Invoke("UnityWebRequest Error");
|
|
|
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
|
|
|
-
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Debug.Log(webRequest.downloadHandler.text);
|
|
|
- CallBack(webRequest.downloadHandler.text);
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
+ StartCoroutine(SendHttp(methodName, jsonString, CallBack));
|
|
|
}
|
|
|
-
|
|
|
- public void PostSnInfo(string methodName, string jsonString, int index, Action<int, string> CallBack)
|
|
|
+ public void GetAllMaterials(string methodName, string jsonString, Action<String> CallBack)
|
|
|
{
|
|
|
- StartCoroutine(PostnInfoRequest(methodName, jsonString, index, CallBack));
|
|
|
+ StartCoroutine(SendHttp(methodName, "", CallBack,false));
|
|
|
}
|
|
|
- public IEnumerator PostnInfoRequest(string methodName, string jsonString, int index, Action<int, string> CallBack)
|
|
|
- {
|
|
|
- string url = baseUrl + methodName;
|
|
|
- Debug.Log("URL:" + url);
|
|
|
-
|
|
|
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
|
|
|
- {
|
|
|
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
|
|
|
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
|
|
|
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
|
|
|
-
|
|
|
- webRequest.SetRequestHeader("authorization", token);
|
|
|
- foreach (var v in requestHeader)
|
|
|
- {
|
|
|
- webRequest.SetRequestHeader(v.Key, v.Value);
|
|
|
-
|
|
|
- }
|
|
|
- yield return webRequest.SendWebRequest();
|
|
|
|
|
|
- if (webRequest.isHttpError || webRequest.isNetworkError)
|
|
|
- {
|
|
|
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
|
|
|
-
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Debug.Log(webRequest.downloadHandler.text);
|
|
|
- CallBack(index, webRequest.downloadHandler.text);
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public void PostLogin(string methodName, string jsonString, Action<string> CallBack)
|
|
|
+ {
|
|
|
+ StartCoroutine(SendHttp(methodName, jsonString, CallBack));
|
|
|
}
|
|
|
|
|
|
- public IEnumerator PostRequestSpoitDetail(string methodName, string jsonString, Action<string> CallBack)
|
|
|
+ public void initHead()
|
|
|
{
|
|
|
- string url = baseUrl + methodName;
|
|
|
- Debug.Log(url);
|
|
|
-
|
|
|
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
|
|
|
+ requestHeader.Clear();
|
|
|
+ if (UserInfo.Instance.Token!="" && UserInfo.Instance.Token!=null)
|
|
|
{
|
|
|
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
|
|
|
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
|
|
|
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
|
|
|
-
|
|
|
- webRequest.SetRequestHeader("authorization", token);
|
|
|
- foreach (var v in requestHeader)
|
|
|
- {
|
|
|
- webRequest.SetRequestHeader(v.Key, v.Value);
|
|
|
-
|
|
|
- }
|
|
|
- yield return webRequest.SendWebRequest();
|
|
|
-
|
|
|
- if (webRequest.isHttpError || webRequest.isNetworkError)
|
|
|
- {
|
|
|
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
|
|
|
-
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
-
|
|
|
- CallBack(webRequest.downloadHandler.text);
|
|
|
-
|
|
|
- }
|
|
|
+ requestHeader.Add("x-token", UserInfo.Instance.Token);
|
|
|
+ requestHeader.Add("authorization", UserInfo.Instance.Token);
|
|
|
}
|
|
|
+ requestHeader.Add("Content-Type", "application/json");
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public void Post(string methodName, string jsonString, Action<string> CallBack)
|
|
|
- {
|
|
|
- StartCoroutine(PostRequest(methodName, jsonString, CallBack));
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- private IEnumerator PostRequest(string methodName, string jsonString, Action<string> CallBack)
|
|
|
+ public IEnumerator SendHttp(string methodName, string jsonString, Action<string> CallBack,bool isPost=true,bool isCloud=false)
|
|
|
{
|
|
|
- string url = baseUrl + methodName;
|
|
|
-
|
|
|
-
|
|
|
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
|
|
|
+ Debug.Log("Start Queue SendHttp " + methodName);
|
|
|
+ string url;
|
|
|
+ if (methodName.Contains("http"))
|
|
|
{
|
|
|
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
|
|
|
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
|
|
|
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
|
|
|
-
|
|
|
- webRequest.SetRequestHeader("authorization", token);
|
|
|
- foreach (var v in requestHeader)
|
|
|
+ url =methodName;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if(isCloud)
|
|
|
+ {
|
|
|
+ url = UrlPointCloud + methodName;
|
|
|
+ }else
|
|
|
{
|
|
|
- webRequest.SetRequestHeader(v.Key, v.Value);
|
|
|
-
|
|
|
- }
|
|
|
- yield return webRequest.SendWebRequest();
|
|
|
+ if(is20)
|
|
|
+ {
|
|
|
+ url = baseUrl20 + methodName;
|
|
|
|
|
|
- if (webRequest.isHttpError || webRequest.isNetworkError)
|
|
|
- {
|
|
|
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text + "\n" + methodName);
|
|
|
-
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
-
|
|
|
- CallBack(webRequest.downloadHandler.text);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ url = baseUrl + methodName;
|
|
|
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- public void GetAllMaterials(string methodName, string jsonString, Action<String> CallBack)
|
|
|
- {
|
|
|
- StartCoroutine(GetRequest(methodName, jsonString, CallBack));
|
|
|
- }
|
|
|
- private IEnumerator GetRequest(string methodName, string jsonString, Action<String> CallBack)
|
|
|
- {
|
|
|
- string url = baseUrl + methodName;
|
|
|
-
|
|
|
- token = jsonString;
|
|
|
- HeadAddToken(token);
|
|
|
- using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
|
|
|
+ }
|
|
|
+ Debug.Log("URL:" + url+" isPost "+ isPost);
|
|
|
+ UnityWebRequest webRequest;
|
|
|
+ if (!isPost)
|
|
|
{
|
|
|
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
|
|
|
- webRequest.SetRequestHeader("authorization", jsonString);
|
|
|
- foreach (var v in requestHeader)
|
|
|
- {
|
|
|
-
|
|
|
- webRequest.SetRequestHeader(v.Key, v.Value);
|
|
|
- }
|
|
|
- yield return webRequest.SendWebRequest();
|
|
|
+ webRequest = UnityWebRequest.Get(url);
|
|
|
|
|
|
- if (webRequest.isHttpError || webRequest.isNetworkError)
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ webRequest = new UnityWebRequest(url, "POST");
|
|
|
+ }
|
|
|
+ initHead();
|
|
|
+ using (webRequest)
|
|
|
+ {
|
|
|
+ if (!isPost)
|
|
|
{
|
|
|
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
|
|
|
-
|
|
|
+ webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
|
|
|
+ foreach (var v in requestHeader)
|
|
|
+ {
|
|
|
+ webRequest.SetRequestHeader(v.Key, v.Value);
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
-
|
|
|
- CallBack(webRequest.downloadHandler.text);
|
|
|
+ byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
|
|
|
+ webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
|
|
|
+ webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- public void PostLogin(string methodName, string jsonString, Action<string> CallBack)
|
|
|
- {
|
|
|
- StartCoroutine(PostRequestLogin(methodName, jsonString, CallBack));
|
|
|
- }
|
|
|
- private IEnumerator PostRequestLogin(string methodName, string jsonString, Action<string> CallBack)
|
|
|
- {
|
|
|
- string url = baseUrl + methodName;
|
|
|
- Debug.Log("URL:" + url);
|
|
|
-
|
|
|
- using (UnityWebRequest webRequest = new UnityWebRequest(url, "POST"))
|
|
|
- {
|
|
|
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonString);
|
|
|
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
|
|
|
- webRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
|
|
|
|
|
|
foreach (var v in requestHeader)
|
|
|
{
|
|
@@ -355,7 +166,7 @@ public class HttpTool : MonoSingleton<HttpTool>
|
|
|
|
|
|
if (webRequest.isHttpError || webRequest.isNetworkError)
|
|
|
{
|
|
|
- Debug.LogError(webRequest.error + "\n" + webRequest.downloadHandler.text);
|
|
|
+ Debug.LogError(url + "\n" + webRequest.error + "\n" + webRequest.downloadHandler.text);
|
|
|
string error = webRequest.downloadHandler.text;
|
|
|
JObject jObject = JObject.Parse(error);
|
|
|
CallBack(jObject["message"].ToString());
|
|
@@ -365,47 +176,11 @@ public class HttpTool : MonoSingleton<HttpTool>
|
|
|
|
|
|
var mes = webRequest.downloadHandler.text;
|
|
|
|
|
|
- JObject obj = JObject.Parse(mes);
|
|
|
- if (obj["code"].ToString() == "200")
|
|
|
- {
|
|
|
-
|
|
|
- token = obj["data"]["token"].ToString();
|
|
|
-
|
|
|
- }
|
|
|
CallBack(mes);
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
|
|
|
+ }
|
|
|
public void HeadAddToken(string token)
|
|
|
{
|
|
|
requestHeader.Add("x-token", token);
|