IUpOrDownloadService.cs 8.2 KB

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