123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using Blue;
- using LitJson;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- using UnityEngine.Networking;
- public interface IUpOrDownloadService : IService
- {
- List<ScenePosRotInfo> PosRot { get; }
- string URL { get; }
- void GetScenePosRot(string Url);
- void UploadScenePosRot(string Url, string JsonString);
- }
- public class UpOrDownloadService : IUpOrDownloadService
- {
- public List<ScenePosRotInfo> PosRot { get; private set; }
- public string URL { get; private set; }
- public void OnInit()
- {
- }
- void IUpOrDownloadService.GetScenePosRot(string Url)
- {
- RootSystem.Instance.StartCoroutine(GetScenePosRotRequest(Url));
- }
- void IUpOrDownloadService.UploadScenePosRot(string Url, string JsonString)
- {
- RootSystem.Instance.StartCoroutine(RequestUpload(Url,JsonString));
- }
- private IEnumerator GetScenePosRotRequest(string Url)
- {
- using (UnityWebRequest webRequest = UnityWebRequest.Get(Url))
- {
- webRequest.SetRequestHeader("authorization", HttpTool.Instance.Token);
- //??header
- foreach (var v in HttpTool.Instance.RequestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.result== UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
- Debug.LogError("UnityWebRequest Error");
- else
- {
- if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
- {
- string message = webRequest.downloadHandler.text;
- JObject jobject = JObject.Parse(message);
- if (jobject["code"].ToString() == "200")
- {
- message = jobject["data"].ToString();
- if (!string.IsNullOrWhiteSpace(message))
- {
- List<ScenePosRotInfo> ScenePosRotInfoList = JsonConvert.DeserializeObject<List<ScenePosRotInfo>>(message);
- PosRot = ScenePosRotInfoList;
- URL = Url;
- this.TriggerEvent(new GetScenePosRotEvent() { PosRot = PosRot });
- }
- }
- }
- }
- }
- }
- public IEnumerator RequestUpload(string Url, string jsonString)
- {
- using (UnityWebRequest webRequest = new UnityWebRequest(Url, "POST"))
- {
- JsonData data = JsonMapper.ToObject(jsonString);
- JsonData d2 = new JsonData();
- d2["position"] = data;
- byte[] bodyRaw = Encoding.UTF8.GetBytes(d2.ToJson());
- webRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- webRequest.downloadHandler = new DownloadHandlerBuffer();
- webRequest.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
- webRequest.SetRequestHeader("authorization", HttpTool.Instance.Token);
- foreach (var v in HttpTool.Instance.RequestHeader)
- {
- webRequest.SetRequestHeader(v.Key, v.Value);
- }
- yield return webRequest.SendWebRequest();
- if (webRequest.result == UnityWebRequest.Result.ProtocolError || webRequest.result == UnityWebRequest.Result.ConnectionError)
- {
- Debug.LogError(webRequest.downloadHandler.error);
- InstantiateCommand Command = new InstantiateCommand(
- InstantiateSystem.Instance.BlueObject.WarningPopUp,
- InstantiateSystem.Instance.BlueObject.NetErrorText);
- CommandExtensionSystem.Instance.Send(Command);
- }
- else
- {
- Debug.LogError(webRequest.downloadHandler.text);
- if (!string.IsNullOrWhiteSpace(webRequest.downloadHandler.text))
- {
- string message = webRequest.downloadHandler.text;
- JObject jobject = JObject.Parse(message);
- if (jobject["code"].ToString() == "200")
- {
- message = jobject["data"].ToString();
- if (!string.IsNullOrWhiteSpace(message))
- {
- List<ScenePosRotInfo> ScenePosRotInfoList = JsonConvert.DeserializeObject<List<ScenePosRotInfo>>(message);
- PosRot = ScenePosRotInfoList;
- }
- }
- }
- InstantiateCommand Command = new InstantiateCommand(
- InstantiateSystem.Instance.BlueObject.WarningPopUp,
- InstantiateSystem.Instance.BlueObject.SuccessText);
- CommandExtensionSystem.Instance.Send(Command);
- }
- }
- }
- }
|