IUpOrDownloadService.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Blue;
  2. using LitJson;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Text;
  9. using UnityEngine;
  10. using UnityEngine.Networking;
  11. public interface IUpOrDownloadService : IService
  12. {
  13. List<ScenePosRotInfo> PosRot { get; }
  14. string URL { get; }
  15. void GetScenePosRot(string Url);
  16. void UploadScenePosRot(string Url, string JsonString);
  17. }
  18. public class UpOrDownloadService : IUpOrDownloadService
  19. {
  20. public List<ScenePosRotInfo> PosRot { get; private set; }
  21. public string URL { get; private set; }
  22. public void OnInit()
  23. {
  24. }
  25. void IUpOrDownloadService.GetScenePosRot(string Url)
  26. {
  27. RootSystem.Instance.StartCoroutine(GetScenePosRotRequest(Url));
  28. }
  29. void IUpOrDownloadService.UploadScenePosRot(string Url, string JsonString)
  30. {
  31. RootSystem.Instance.StartCoroutine(RequestUpload(Url,JsonString));
  32. }
  33. private IEnumerator GetScenePosRotRequest(string Url)
  34. {
  35. using (UnityWebRequest webRequest = UnityWebRequest.Get(Url))
  36. {
  37. webRequest.SetRequestHeader("authorization", HttpTool.Instance.Token);
  38. //??header
  39. foreach (var v in HttpTool.Instance.RequestHeader)
  40. {
  41. webRequest.SetRequestHeader(v.Key, v.Value);
  42. }
  43. yield return webRequest.SendWebRequest();
  44. if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  45. Debug.LogError("UnityWebRequest Error");
  46. else
  47. {
  48. if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
  49. {
  50. string message = webRequest.downloadHandler.text;
  51. JObject jobject = JObject.Parse(message);
  52. if (jobject["code"].ToString() == "200")
  53. {
  54. message = jobject["data"].ToString();
  55. if (!string.IsNullOrWhiteSpace(message))
  56. {
  57. List<ScenePosRotInfo> ScenePosRotInfoList = JsonConvert.DeserializeObject<List<ScenePosRotInfo>>(message);
  58. PosRot = ScenePosRotInfoList;
  59. URL = Url;
  60. this.TriggerEvent(new GetScenePosRotEvent() { PosRot = PosRot });
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. public IEnumerator RequestUpload(string Url, string jsonString)
  68. {
  69. using (UnityWebRequest webRequest = new UnityWebRequest(Url, "POST"))
  70. {
  71. JsonData data = JsonMapper.ToObject(jsonString);
  72. JsonData d2 = new JsonData();
  73. d2["position"] = data;
  74. byte[] bodyRaw = Encoding.UTF8.GetBytes(d2.ToJson());
  75. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  76. webRequest.downloadHandler = new DownloadHandlerBuffer();
  77. webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
  78. webRequest.SetRequestHeader("authorization", HttpTool.Instance.Token);
  79. foreach (var v in HttpTool.Instance.RequestHeader)
  80. {
  81. webRequest.SetRequestHeader(v.Key, v.Value);
  82. }
  83. yield return webRequest.SendWebRequest();
  84. if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  85. {
  86. Debug.LogError(webRequest.downloadHandler.error);
  87. InstantiateCommand Command = new InstantiateCommand(
  88. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  89. InstantiateSystem.Instance.BlueObject.NetErrorText);
  90. CommandExtensionSystem.Instance.Send(Command);
  91. }
  92. else
  93. {
  94. Debug.LogError(webRequest.downloadHandler.text);
  95. if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
  96. {
  97. string message = webRequest.downloadHandler.text;
  98. JObject jobject = JObject.Parse(message);
  99. if (jobject["code"].ToString() == "200")
  100. {
  101. message = jobject["data"].ToString();
  102. if (!string.IsNullOrWhiteSpace(message))
  103. {
  104. List<ScenePosRotInfo> ScenePosRotInfoList = JsonConvert.DeserializeObject<List<ScenePosRotInfo>>(message);
  105. PosRot = ScenePosRotInfoList;
  106. }
  107. }
  108. }
  109. InstantiateCommand Command = new InstantiateCommand(
  110. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  111. InstantiateSystem.Instance.BlueObject.SuccessText);
  112. CommandExtensionSystem.Instance.Send(Command);
  113. }
  114. }
  115. }
  116. }