using LitJson;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
namespace SC.XR.Unity.Module_PlatformAccount
{
///
/// 请求数据
///
public struct RequestData
{
public IEnumerator enumerator;
public Task task;
///
/// 开始请求时触发的事件
///
public Action requestStartEvent;
///
/// 请求中触发的事件(Update)
///
public Action requestingEvent;
///
/// 结束请求时触发的事件
///
public Action requestEndEvent;
}
public abstract class WebRequestMgr : SingletonMono
{
///
/// 当前正在请求的数据
///
private RequestData m_CurrentRequestData;
protected virtual async void Update()
{
// 开始队列请求
if (RequsetQueue.Count > 0)
{
// 队列请求中。。。
m_IsRequestingQueue = true;
if (!m_IsRequesting)
{
// 当前请求中。。。
m_IsRequesting = true;
// 所有请求开始触发的事件
RequestStartEvent?.Invoke();
// 当前请求
m_CurrentRequestData = RequsetQueue.Dequeue();
// 当前请求开始触发的事件
m_CurrentRequestData.requestStartEvent?.Invoke();
// 当前请求协程开始
if (m_CurrentRequestData.enumerator!=null)
{
StartCoroutine(m_CurrentRequestData.enumerator);
}
else if (m_CurrentRequestData.task != null)
{
await m_CurrentRequestData.task;
}
}
}
// 队列请求中。。。
else if (m_IsRequesting)
{
// 所有请求中触发的事件
RequestingEvent?.Invoke();
// 当前请求中触发的事件
m_CurrentRequestData.requestingEvent?.Invoke();
}
// 结束队列请求
else if (m_IsRequestingQueue && !m_IsRequesting && m_RequestQueue.Count <= 0)
{
// 队列请求结束
m_IsRequestingQueue = false;
// 所有请求结束触发的事件
RequestEndEvent?.Invoke();
// 当前请求结束触发的事件
m_CurrentRequestData.requestEndEvent?.Invoke();
}
}
///
/// 是否正在请求
///
private bool m_IsRequesting;
public bool IsRequesting { get { return m_IsRequesting; } set { m_IsRequesting = value; } }
///
/// 是否正在队列请求
///
private bool m_IsRequestingQueue;
public bool IsRequestingQueue { get { return m_IsRequestingQueue; } set { m_IsRequestingQueue = value; } }
///
/// 请求队列
///
private Queue m_RequestQueue;
public Queue RequsetQueue
{
get
{
if (m_RequestQueue == null)
{
m_RequestQueue = new Queue();
}
return m_RequestQueue;
}
}
///
/// 开始队列请求时触发的事件
///
public Action RequestStartEvent;
///
/// 队列请求中触发的事件(Update)
///
public Action RequestingEvent;
///
/// 结束队列请求时触发的事件
///
public Action RequestEndEvent;
///
/// 准备发送UnityWebRequest之前触发的事件
///
public Action PreSendWebRequestEvent;
///
/// 发送完成UnityWebRequset之后触发的回调事件
///
public Action WebRequestCallBackEvent;
///
/// 通用数据Post请求接口
///
public abstract void PostData(string url, string headerJson, string formJson, Action callback = null,Action error = null);
public abstract void PostData(string url, JsonData header, JsonData form, Action callback = null,Action error = null);
///
/// 图像数据Post请求接口
///
public abstract void PostImageData(string url, Texture2D userIcon, HttpClient client, string channel, string filename);
///
/// 通用数据Get请求接口
///
public abstract void GetData(string url, string headerJson, string formJson, Action callback = null,Action error = null);
public abstract void GetData(string url, JsonData header, JsonData form, Action callback = null,Action error = null);
///
/// Get请求队列
///
public abstract IEnumerator GetsDataQueue(string url, JsonData header, JsonData form, Action callback = null,Action error = null);
///
/// Post请求队列
///
public abstract IEnumerator PostsDataQueue(string url, JsonData header, JsonData form, Action callback = null,Action error = null);
}
}