IUpOrDownloadService.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using Blue;
  2. using LitJson;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using UnityEngine;
  9. using UnityEngine.Networking;
  10. using XRTool.Util;
  11. public interface IUpOrDownloadService : IService
  12. {
  13. List<ScenePosRotInfo> PosRot { get; }
  14. void GetScenePosRot(int projectID);
  15. void UploadScenePosRot(string Url, string JsonString);
  16. }
  17. public class UpOrDownloadService : IUpOrDownloadService
  18. {
  19. public List<ScenePosRotInfo> PosRot { get; private set; }
  20. public int ProjectID { get; private set; }
  21. public void OnInit()
  22. {
  23. }
  24. void IUpOrDownloadService.GetScenePosRot(int projectID)
  25. {
  26. TimerMgr.Instance.CreateTimer(()=> {
  27. Debug.Log("HJJ.GetScenePosRot " + projectID);
  28. CoroutineSystem.Instance.StartCoroutine(GetScenePosRotRequest(projectID));
  29. },1,1);
  30. }
  31. void IUpOrDownloadService.UploadScenePosRot(string Url, string JsonString)
  32. {
  33. CoroutineSystem.Instance.StartCoroutine(RequestUpload(Url,JsonString));
  34. }
  35. private IEnumerator GetScenePosRotRequest(int projectID)
  36. {
  37. Debug.Log("HJJ.GetScenePosRot " + HttpSDKAction.Instance.loginUrl + "/project/position");
  38. using (UnityWebRequest webRequest = new UnityWebRequest(HttpSDKAction.Instance.loginUrl+ "/project/position", "POST"))
  39. {
  40. ProjectID = projectID;
  41. ScenePointBodyInfo TestGet = new ScenePointBodyInfo();
  42. TestGet.id = projectID;
  43. TestGet.unity = false;
  44. string jsonData = JsonConvert.SerializeObject(TestGet);
  45. byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
  46. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  47. webRequest.downloadHandler = new DownloadHandlerBuffer();
  48. webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
  49. webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
  50. yield return webRequest.SendWebRequest();
  51. if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  52. Debug.LogError("UnityWebRequest Error:"+webRequest.downloadHandler.text);
  53. else
  54. {
  55. if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
  56. {
  57. string message = webRequest.downloadHandler.text;
  58. JObject jobject = JObject.Parse(message);
  59. if (jobject["code"].ToString() == "200")
  60. {
  61. message = jobject["data"]["position"].ToString();
  62. Debug.Log("Hjj message===>"+ message);
  63. if (!string.IsNullOrWhiteSpace(message))
  64. {
  65. // TimerMgr.Instance.CreateTimer(() => {
  66. List<ScenePosRotInfo> ScenePosRotInfoList = JsonConvert.DeserializeObject<List<ScenePosRotInfo>>(message);
  67. // GongShi2.F2Dto3D(ScenePosRotInfoList[0].x, ScenePosRotInfoList[0].y, 643f, 1920f, ScenePosRotInfoList[1].x, ScenePosRotInfoList[1].y, 51f, 17.1f);
  68. if(ScenePosRotInfoList[0].x==0 && ScenePosRotInfoList[0].y == 0 && ScenePosRotInfoList[0].z == 0&& ScenePosRotInfoList[1].x == 0 && ScenePosRotInfoList[1].y == 0 && ScenePosRotInfoList[1].z == 0)
  69. {
  70. CommandSystem.Instance.Send(new InitSuccessCommand());
  71. }
  72. else
  73. {
  74. while(DataManager.Instance.CurrentScene == null)
  75. {
  76. yield return null;
  77. }
  78. Debug.Log("Hjj message=1==>" + ScenePosRotInfoList[0].x);
  79. Debug.Log("Hjj message=2==>" + DataManager.Instance.CurrentScene.width);
  80. Debug.Log("Hjj message=3==>" + GameManager.Instance.WebMapSize.x);
  81. GongShi2.F2Dto3D(ScenePosRotInfoList[0].x, ScenePosRotInfoList[0].y, GameManager.Instance.WebMapSize.y, GameManager.Instance.WebMapSize.x, ScenePosRotInfoList[1].x, ScenePosRotInfoList[1].y, DataManager.Instance.CurrentScene.length, DataManager.Instance.CurrentScene.width);
  82. }
  83. // },0.03f,-1);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. public IEnumerator RequestUpload(string Url, string jsonString)
  91. {
  92. Debug.Log("HJJSAVE Url ====>" + Url);
  93. Debug.Log("HJJSAVE jsonString ====>" + jsonString);
  94. using (UnityWebRequest webRequest = new UnityWebRequest(Url, "POST"))
  95. {
  96. JsonData data = JsonMapper.ToObject(jsonString);
  97. JsonData d2 = new JsonData();
  98. d2["position"] = data;
  99. d2["id"] = ProjectID;
  100. d2["unity"] = false;
  101. byte[] bodyRaw = Encoding.UTF8.GetBytes(d2.ToJson());
  102. webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  103. webRequest.downloadHandler = new DownloadHandlerBuffer();
  104. webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
  105. webRequest.SetRequestHeader("authorization", UserInfo.Instance.Token);
  106. foreach (var v in HttpTool.Instance.RequestHeader)
  107. {
  108. webRequest.SetRequestHeader(v.Key, v.Value);
  109. }
  110. yield return webRequest.SendWebRequest();
  111. if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
  112. {
  113. Debug.LogError(webRequest.downloadHandler.error);
  114. Debug.LogError(webRequest.downloadHandler.text);
  115. InstantiateCommand Command = new InstantiateCommand(
  116. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  117. InstantiateSystem.Instance.BlueObject.NetErrorText);
  118. CommandSystem.Instance.Send(Command);
  119. }
  120. else
  121. {
  122. if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
  123. {
  124. string message = webRequest.downloadHandler.text;
  125. JObject jobject = JObject.Parse(message);
  126. if (jobject["code"].ToString() == "200")
  127. {
  128. message = jobject["data"].ToString();
  129. if (!string.IsNullOrWhiteSpace(message))
  130. {
  131. Debug.Log("HJJSave "+ message);
  132. JsonData data2 = JsonMapper.ToObject(message);
  133. List<ScenePosRotInfo> ScenePosRotInfoList = JsonConvert.DeserializeObject<List<ScenePosRotInfo>>(data2["position"].ToJson());
  134. PosRot = ScenePosRotInfoList;
  135. }
  136. }
  137. }
  138. InstantiateCommand Command = new InstantiateCommand(
  139. InstantiateSystem.Instance.BlueObject.WarningPopUp,
  140. InstantiateSystem.Instance.BlueObject.SuccessText);
  141. CommandSystem.Instance.Send(Command);
  142. }
  143. }
  144. }
  145. }