E_ElecCompetition/Electrical_inspectionCompet.../Assets/Adam/Net/AsyncWebReq.cs

117 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using System.Text;
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
/// <summary>
/// 异步封装unityWebRequest请求
/// </summary>
public static class AsyncWebReq
{
public static async UniTask<T> Get<T>(string endpoint)
{
var getRequest = CreateRequest(endpoint);
await getRequest.SendWebRequest();
#if UNITY_EDITOR
Debug.Log("async req : " + getRequest.downloadHandler.text);
#endif
T result = JsonUtility.FromJson<T>(getRequest.downloadHandler.text);
getRequest.Dispose();
return result;
}
public static void SetValue(string jsonStr, out JObject _jbsx, out JObject _jsxx, out List<JObject> _jlzzxx)
{
var json = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonStr);
var data = json["data"] as JObject;
var jbsxArr = data["jbsx"] as JArray;
var jbsx = jbsxArr[0] as JObject;
_jbsx = jbsx;
//foreach (var kv in jbsx)
//{
// Debug.Log(kv.Key + "" + kv.Value);
//}
var jbxxArr = data["jbxx"] as JArray;
var jbxx = jbxxArr[0] as JObject;
_jsxx = jbxx;
//foreach (var kv in jbxx)
//{
// Debug.Log(kv.Key + "" + kv.Value);
//}
_jlzzxx = new List<JObject>();
var jlzzxxArr = data["jlzzxx"] as JArray;
for (int i = 0; i < jlzzxxArr.Count; i++)
{
var jlzzxx = jlzzxxArr[i] as JObject;
_jlzzxx.Add(jlzzxx);
}
//foreach (var kv in jlzzxx)
//{
// Debug.Log(kv.Key + "" + kv.Value);
//}
}
public static async UniTask<T> Post<T>(string endpoint, object payload)
{
var postRequest = CreateRequest(endpoint, RequestType.POST, payload);
await postRequest.SendWebRequest();
#if UNITY_EDITOR
Debug.Log("async req : " + postRequest.downloadHandler.text);
Debug.Log("endpoint : " + endpoint);
#endif
T result = JsonUtility.FromJson<T>(postRequest.downloadHandler.text);
postRequest.Dispose();
return result;
}
private static UnityWebRequest CreateRequest(string path, RequestType type = RequestType.GET, object data = null)
{
var request = new UnityWebRequest(path, type.ToString());
if (data != null)
{
string reqJson = JsonConvert.SerializeObject(data);
#if UNITY_EDITOR
Debug.Log($"async req json : {reqJson}");
#endif
var bodyRaw = Encoding.UTF8.GetBytes(reqJson);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
}
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
return request;
}
private static void AttachHeader(UnityWebRequest request, string key, string value)
{
request.SetRequestHeader(key, value);
}
private static string ObjectToStringTwo(Object obj)
{
string str = JsonConvert.SerializeObject(obj);
return str;
}
private static T StringToObjectTwo<T>(string str)
{
T obj = JsonConvert.DeserializeObject<T>(str);
return obj;
}
/// <summary>
/// 请求类型
/// </summary>
public enum RequestType
{
GET = 0,
POST = 1
}
}