500 lines
18 KiB
C#
500 lines
18 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using LitJson;
|
|
using System.IO;
|
|
|
|
/// <summary>
|
|
/// 接口及接口访问
|
|
/// </summary>
|
|
public static class InterfaceManager
|
|
{
|
|
private static string _IP = "172.16.1.144:8080";
|
|
public static string IP
|
|
{
|
|
get { return _IP; }
|
|
set
|
|
{
|
|
if (_IP != value)
|
|
_IP = value;
|
|
}
|
|
}
|
|
public static string IpAddress { get => string.Format("http://{0}", IP); }
|
|
#region 接口
|
|
public static string Url_ { get => IpAddress + "/"; }
|
|
#endregion
|
|
|
|
|
|
/// <summary>
|
|
/// 读取数据
|
|
/// </summary>
|
|
/// <typeparam name="T">类型</typeparam>
|
|
/// <param name="path"></param>
|
|
/// <returns></returns>
|
|
public static T LoadData<T>(string fileName, JsonType type = JsonType.LitJson) where T : new()
|
|
{
|
|
//路径
|
|
string path = Application.streamingAssetsPath + "/" + fileName + ".json";
|
|
Debug.Log(path);
|
|
if (!File.Exists(path))
|
|
{
|
|
path = Application.persistentDataPath + "/" + fileName + ".json";
|
|
}
|
|
if (!File.Exists(path))
|
|
{
|
|
return new T();
|
|
}
|
|
string jsonStr = File.ReadAllText(path);
|
|
T data = new T();
|
|
switch (type)
|
|
{
|
|
case JsonType.LitJson:
|
|
data = JsonMapper.ToObject<T>(jsonStr);
|
|
break;
|
|
case JsonType.JsonUtility:
|
|
data = JsonUtility.FromJson<T>(jsonStr);
|
|
break;
|
|
}
|
|
return data;
|
|
}
|
|
|
|
public static string GetLocalTxt(string path)
|
|
{
|
|
using (System.IO.StreamReader reader = new System.IO.StreamReader(path)) { return reader.ReadToEnd(); }
|
|
}
|
|
|
|
public static IEnumerator GetBytes(string url, Action<byte[]> callback)
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
yield return www.SendWebRequest();
|
|
switch (www.result)
|
|
{
|
|
case UnityWebRequest.Result.InProgress:
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
callback?.Invoke(www.downloadHandler.data);
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerator GetString(string url, Action<string> callback)
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
yield return www.SendWebRequest();
|
|
switch (www.result)
|
|
{
|
|
case UnityWebRequest.Result.InProgress:
|
|
Debug.LogError("UnityWebRequest.Result.InProgress");
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
callback?.Invoke(www.downloadHandler.text);
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
Debug.LogError("UnityWebRequest.Result.ConnectionError");
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
Debug.LogError("UnityWebRequest.Result.ProtocolError");
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
Debug.LogError("UnityWebRequest.Result.DataProcessingError");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
public static IEnumerator GetString(string url, int time, Action<string> callback)
|
|
{
|
|
while (true)
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
yield return new WaitForSeconds(time); // 每隔 time 秒调用一次接口
|
|
|
|
yield return www.SendWebRequest();
|
|
switch (www.result)
|
|
{
|
|
case UnityWebRequest.Result.InProgress:
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
callback?.Invoke(www.downloadHandler.text);
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerator GetString(string url, int time, Dictionary<string, string> header, Action<string> callback)
|
|
{
|
|
while (true)
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
yield return new WaitForSeconds(time); // 每隔 time 秒调用一次接口
|
|
foreach (var item in header)
|
|
{
|
|
www.SetRequestHeader(item.Key, item.Value);
|
|
}
|
|
yield return www.SendWebRequest();
|
|
switch (www.result)
|
|
{
|
|
case UnityWebRequest.Result.InProgress:
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
callback?.Invoke(www.downloadHandler.text);
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerator GetJsonString<T>(string url, Action<T> callback = null, string success_code = "200")
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
yield return www.SendWebRequest();
|
|
switch (www.result)
|
|
{
|
|
case UnityWebRequest.Result.InProgress:
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
var Jobject = JObject.Parse(www.downloadHandler.text);
|
|
if (Jobject["code"].ToString() == success_code)
|
|
{
|
|
callback?.Invoke(JsonConvert.DeserializeObject<T>(Jobject["data"].ToString()));
|
|
}
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerator GetJsonString<T>(string url, Dictionary<string, string> header, Action<T> callback = null, string success_code = "200")
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
foreach (var item in header)
|
|
{
|
|
www.SetRequestHeader(item.Key, item.Value);
|
|
}
|
|
yield return www.SendWebRequest();
|
|
switch (www.result)
|
|
{
|
|
case UnityWebRequest.Result.InProgress:
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
//Debug.Log("根据标识返回人员信息...:" + www.downloadHandler.text);
|
|
var Jobject = JObject.Parse(www.downloadHandler.text);
|
|
if (Jobject["code"].ToString() == success_code)
|
|
{
|
|
callback?.Invoke(JsonConvert.DeserializeObject<T>(Jobject["data"].ToString()));
|
|
}
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerator PostJsonString<T>(string url, Action<T> callback = null, string success_code = "200")
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
yield return www.SendWebRequest();
|
|
switch (www.result)
|
|
{
|
|
case UnityWebRequest.Result.InProgress:
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
var Jobject = JObject.Parse(www.downloadHandler.text);
|
|
if (Jobject["code"].ToString() == success_code)
|
|
{
|
|
callback?.Invoke(JsonConvert.DeserializeObject<T>(Jobject["data"].ToString()));
|
|
}
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerator PostJsonString<T>(string url, WWWForm formdata, Dictionary<string, string> headers = null, Action<T> callback = null, string success_code = "200")
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Post(url, formdata))
|
|
{
|
|
foreach (var item in headers)
|
|
{
|
|
www.SetRequestHeader(item.Key, item.Value);
|
|
}
|
|
yield return www.SendWebRequest();
|
|
switch (www.result)
|
|
{
|
|
case UnityWebRequest.Result.InProgress:
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
var Jobject = JObject.Parse(www.downloadHandler.text);
|
|
if (Jobject["code"].ToString() == success_code)
|
|
{
|
|
callback?.Invoke(JsonConvert.DeserializeObject<T>(Jobject["data"].ToString()));
|
|
}
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static IEnumerator PostString(string url, WWWForm formdata, Dictionary<string, string> headers = null, Action<string> callback = null)
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Post(url, formdata))
|
|
{
|
|
foreach (var item in headers)
|
|
{
|
|
www.SetRequestHeader(item.Key, item.Value);
|
|
}
|
|
yield return www.SendWebRequest();
|
|
switch (www.result)
|
|
{
|
|
case UnityWebRequest.Result.InProgress:
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
callback?.Invoke(www.downloadHandler.text);
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
Debug.Log(www.result);
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
Debug.Log(www.result);
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
Debug.Log(www.result);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerator PostString(string url, WWWForm formdata, Dictionary<string, string> headers = null, string jsonBody = null, Action<string> callback = null)
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Post(url, formdata))
|
|
{
|
|
foreach (var item in headers)
|
|
{
|
|
www.SetRequestHeader(item.Key, item.Value);
|
|
}
|
|
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(jsonBody);
|
|
www.uploadHandler = new UploadHandlerRaw(jsonToSend);
|
|
// 设置 Content-Type 为 application/json
|
|
www.SetRequestHeader("Content-Type", "application/json");
|
|
yield return www.SendWebRequest();
|
|
switch (www.result)
|
|
{
|
|
case UnityWebRequest.Result.InProgress:
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
callback?.Invoke(www.downloadHandler.text);
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
Debug.Log(www.result);
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
Debug.Log(www.result);
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
Debug.Log(www.result);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerator GetString(string url, Dictionary<string, string> headers = null, int time = 50, Action<string> callback = null)
|
|
{
|
|
while (true)
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
yield return new WaitForSeconds(time); // 每隔 time 秒调用一次接口
|
|
foreach (var item in headers)
|
|
{
|
|
www.SetRequestHeader(item.Key, item.Value);
|
|
}
|
|
yield return www.SendWebRequest();
|
|
switch (www.result)
|
|
{
|
|
case UnityWebRequest.Result.InProgress:
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
callback?.Invoke(www.downloadHandler.text);
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerator GetString(string url, Dictionary<string, string> headers = null, Action<string> callback = null)
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
foreach (var item in headers)
|
|
{
|
|
www.SetRequestHeader(item.Key, item.Value);
|
|
}
|
|
yield return www.SendWebRequest();
|
|
switch (www.result)
|
|
{
|
|
case UnityWebRequest.Result.InProgress:
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
callback?.Invoke(www.downloadHandler.text);
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static IEnumerator GetString(string url, Dictionary<string, string> headers = null, string jsonBody = null, Action<string> callback = null)
|
|
{
|
|
using (UnityWebRequest www = UnityWebRequest.Get(url))
|
|
{
|
|
foreach (var item in headers)
|
|
{
|
|
www.SetRequestHeader(item.Key, item.Value);
|
|
} // 将请求体设置为原始JSON数据
|
|
byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(jsonBody);
|
|
www.uploadHandler = new UploadHandlerRaw(jsonToSend);
|
|
yield return www.SendWebRequest();
|
|
switch (www.result)
|
|
{
|
|
case UnityWebRequest.Result.InProgress:
|
|
break;
|
|
case UnityWebRequest.Result.Success:
|
|
callback?.Invoke(www.downloadHandler.text);
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// UnityWebRequest Post 表单请求
|
|
/// </summary>
|
|
/// <param name="url">接口地址</param>
|
|
/// <param name="fromDic">form字典</param>
|
|
/// <param name="textCallBack">text回调</param>
|
|
/// <returns></returns>
|
|
public static IEnumerator UnityWebRequestPost(string url, Dictionary<string, string> fromDic, Action<string> textCallBack)
|
|
{
|
|
WWWForm form = new WWWForm();
|
|
foreach (var item in fromDic)
|
|
{
|
|
form.AddField(item.Key, item.Value);
|
|
}
|
|
UnityWebRequest request = UnityWebRequest.Post(url, form);
|
|
request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
|
|
yield return request.SendWebRequest();
|
|
switch (request.result)
|
|
{
|
|
case UnityWebRequest.Result.Success:
|
|
textCallBack?.Invoke(request.downloadHandler.text);
|
|
break;
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
Debug.LogError("ConnectionError");
|
|
break;
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
Debug.LogError("DataProcessingError");
|
|
break;
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
Debug.LogError("ProtocolError");
|
|
break;
|
|
case UnityWebRequest.Result.InProgress:
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|