240 lines
7.6 KiB
C#
240 lines
7.6 KiB
C#
using Crosstales.Common.Util;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using Unity.VisualScripting;
|
||
using UnityEngine;
|
||
using UnityEngine.Networking;
|
||
using WpfApp1.Util;
|
||
|
||
/// <summary>
|
||
/// 接口管理
|
||
/// </summary>
|
||
public static class InterfaceManager
|
||
{
|
||
private static string _token;
|
||
public static string Token
|
||
{
|
||
get => "Bearer " + _token;
|
||
set { _token = value; }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 版本号
|
||
/// </summary>
|
||
public static string Version => Application.version;
|
||
|
||
private static int success_code = 200;
|
||
|
||
#region 接口地址
|
||
/// <summary>
|
||
/// 当前选中的任务Id
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
public static CourseTaskItem.CourseTaskItemData _currentchoosetaskid=new CourseTaskItem.CourseTaskItemData();
|
||
|
||
/// <summary>
|
||
/// IP地址
|
||
/// </summary>
|
||
public static string IdAddress;
|
||
|
||
#endregion
|
||
|
||
#region Get
|
||
|
||
/// <summary>
|
||
/// 加载数据(无头)
|
||
/// </summary>
|
||
/// <param name="_url"></param>
|
||
/// <param name="_callback"></param>
|
||
/// <returns></returns>
|
||
public static IEnumerator Get(string _url,bool isAES,Action<string> _callback = null)
|
||
{
|
||
using (UnityWebRequest __request = new UnityWebRequest(_url, UnityWebRequest.kHttpVerbGET))
|
||
{
|
||
DownloadHandler downloadHandler = new DownloadHandlerBuffer();
|
||
__request.downloadHandler = downloadHandler;
|
||
yield return __request.SendWebRequest();
|
||
if (__request.result == UnityWebRequest.Result.Success)
|
||
{
|
||
if (CallForTest.instance.is加密)
|
||
{
|
||
//系统开启加密
|
||
if (isAES)
|
||
{
|
||
//解密
|
||
string back = AESHelper.AesDecrypt(__request.downloadHandler.text.Trim('"'), CallForTest.instance.AesKey);
|
||
_callback?.Invoke(back);
|
||
}
|
||
else
|
||
{
|
||
//不解密
|
||
_callback?.Invoke(__request.downloadHandler.text);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//系统没开加密
|
||
_callback?.Invoke(__request.downloadHandler.text);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Debug.Log(__request.error);
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 有头
|
||
/// </summary>
|
||
/// <param name="_url"></param>
|
||
/// <param name="_callback"></param>
|
||
/// <returns></returns>
|
||
public static IEnumerator Get(string _url, Action<bool, string> _callback = null)
|
||
{
|
||
using (UnityWebRequest __request = new UnityWebRequest(_url, UnityWebRequest.kHttpVerbGET))
|
||
{
|
||
DownloadHandler downloadHandler = new DownloadHandlerBuffer();
|
||
__request.downloadHandler = downloadHandler;
|
||
__request.SetRequestHeader("Authorization", Token);
|
||
//Debug.Log(Token);
|
||
yield return __request.SendWebRequest();
|
||
if (__request.result == UnityWebRequest.Result.Success)
|
||
{
|
||
string back;
|
||
if (CallForTest.instance.is加密)
|
||
{
|
||
//系统开启加密
|
||
back = AESHelper.AesDecrypt(__request.downloadHandler.text.Trim('"'), CallForTest.instance.AesKey);
|
||
|
||
}
|
||
else
|
||
{
|
||
//没开加密
|
||
back = __request.downloadHandler.text;
|
||
}
|
||
|
||
JObject _jo = JObject.Parse(back);
|
||
if (_jo["code"].ToObject<int>() == success_code)
|
||
_callback?.Invoke(true, back);
|
||
else
|
||
_callback?.Invoke(false, "错误码:" + _jo["code"].ToObject<int>());
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError(__request.error+" "+_url);
|
||
_callback?.Invoke(false, __request.error);
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 下载文件
|
||
/// </summary>
|
||
/// <param name="_url"></param>
|
||
/// <param name="_callback"></param>
|
||
/// <returns></returns>
|
||
public static IEnumerator GetData(string _url, ConsoleItem consoleItem=null, Action<bool, byte[]> _callback = null)
|
||
{
|
||
using (UnityWebRequest __request = new UnityWebRequest(_url, UnityWebRequest.kHttpVerbGET))
|
||
{
|
||
DownloadHandler downloadHandler = new DownloadHandlerBuffer();
|
||
__request.downloadHandler = downloadHandler;
|
||
__request.SetRequestHeader("Authorization", Token);
|
||
|
||
if (consoleItem != null)
|
||
MainCanvasManager.Instance.Set_DownLoad_Process(consoleItem, __request);
|
||
|
||
yield return __request.SendWebRequest();
|
||
|
||
if (consoleItem != null)
|
||
MainCanvasManager.Instance.Remove_DownLoad_Process();
|
||
|
||
if (__request.result == UnityWebRequest.Result.Success)
|
||
{
|
||
_callback(true,__request.downloadHandler.data);
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError(__request.error);
|
||
_callback?.Invoke(false,null);
|
||
}
|
||
}
|
||
}
|
||
|
||
#region Post
|
||
|
||
/// <summary>
|
||
/// 登录不需要token,不加密
|
||
/// </summary>
|
||
/// <param name="_url"></param>
|
||
/// <param name="_jsondata"></param>
|
||
/// <param name="_callback"></param>
|
||
/// <returns></returns>
|
||
public static IEnumerator Post(string _url, string _jsondata, Action<bool, string> _callback = null)
|
||
{
|
||
using (UnityWebRequest __request = new UnityWebRequest(_url, UnityWebRequest.kHttpVerbPOST))
|
||
{
|
||
string sendJson;
|
||
|
||
sendJson = _jsondata;
|
||
|
||
|
||
byte[] __databyte = Encoding.UTF8.GetBytes(sendJson);
|
||
__request.uploadHandler = new UploadHandlerRaw(__databyte);
|
||
__request.downloadHandler = new DownloadHandlerBuffer();
|
||
__request.SetRequestHeader("Content-Type", "application/json");
|
||
yield return __request.SendWebRequest();
|
||
if (__request.result == UnityWebRequest.Result.Success)
|
||
{
|
||
string back;
|
||
|
||
//系统没开加密
|
||
back = __request.downloadHandler.text;
|
||
|
||
JObject _jo = JObject.Parse(back);
|
||
|
||
if (_jo["code"].ToObject<int>() == success_code)
|
||
_callback?.Invoke(true, back);
|
||
else
|
||
_callback?.Invoke(false, "错误码:" + _jo["code"].ToObject<int>());
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError(__request.error);
|
||
_callback?.Invoke(false, __request.error);
|
||
}
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 加载图片精灵
|
||
/// </summary>
|
||
/// <param name="_url"></param>
|
||
/// <param name="_callback"></param>
|
||
/// <returns></returns>
|
||
public static IEnumerator GetSprite(string _url, Action<Sprite> _callback = null)
|
||
{
|
||
using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(_url))
|
||
{
|
||
yield return request.SendWebRequest();
|
||
if (request.result != UnityWebRequest.Result.Success)
|
||
{
|
||
Debug.LogError("加载失败: " + request.error+" "+ _url);
|
||
yield break;
|
||
}
|
||
Texture2D texture = DownloadHandlerTexture.GetContent(request);
|
||
Rect rect = new Rect(0, 0, texture.width, texture.height);
|
||
Sprite sprite = Sprite.Create(texture, rect, Vector2.one * 0.5f);
|
||
_callback?.Invoke(sprite);
|
||
}
|
||
}
|
||
}
|