218 lines
8.6 KiB
C#
218 lines
8.6 KiB
C#
using LitJson;
|
|
using System.Collections;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Web;
|
|
using System.Xml;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.Networking;
|
|
using ZXKFramework;
|
|
|
|
public class NetManager : MonoBehaviour
|
|
{
|
|
[DllImport("__Internal")]
|
|
private static extern string StringReturnURLFunction();
|
|
readonly NetInfo netInfo = new();
|
|
[Header("测试")]
|
|
public bool test;
|
|
[Header("发送")]
|
|
public bool send;
|
|
public void Init()
|
|
{
|
|
if (!send) return;
|
|
InvokeRepeating(nameof(GetListen),20,20);
|
|
InvokeRepeating(nameof(ResetToken), 7200, 7200);
|
|
if (test)
|
|
{
|
|
StartCoroutine(GetTest());
|
|
}
|
|
else
|
|
{
|
|
StartCoroutine(XmlReader(() => {
|
|
GetTicket();
|
|
GetUserInfoAndToken();
|
|
}));
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 2.2.1获取Ticket
|
|
/// </summary>
|
|
void GetTicket()
|
|
{
|
|
netInfo.ticket = UnityTools.GetURLParameters(GetPageURL());
|
|
}
|
|
string GetPageURL()
|
|
{
|
|
return HttpUtility.UrlDecode(StringReturnURLFunction(), Encoding.UTF8);
|
|
}
|
|
/// <summary>
|
|
/// 2.2.2获取用户信息和access_token
|
|
/// </summary>
|
|
void GetUserInfoAndToken()
|
|
{
|
|
if (string.IsNullOrEmpty(netInfo.ticket) || string.IsNullOrEmpty(netInfo.appid) || string.IsNullOrEmpty(netInfo.secret)) return;
|
|
StartCoroutine(GetUserAndToken());
|
|
}
|
|
IEnumerator GetUserAndToken()
|
|
{
|
|
string url = netInfo.server_id + "/open/v1/token?" + "ticket=" + netInfo.ticket + "&appid=" + netInfo.appid + "&signature=" + UnityTools.MD5Encrypt32Big(netInfo.appid + netInfo.ticket + netInfo.secret);
|
|
using UnityWebRequest request = UnityWebRequest.Get(url);
|
|
yield return request.SendWebRequest();
|
|
if (request.isDone && string.IsNullOrEmpty(request.error))
|
|
{
|
|
JsonData jsonData = JsonMapper.ToObject(request.downloadHandler.text);
|
|
netInfo.access_token = (string)jsonData["data"]["access_token"];
|
|
netInfo.create_time = (string)jsonData["data"]["create_time"];
|
|
netInfo.expire_time = (string)jsonData["data"]["expire_time"];
|
|
netInfo.user_name = (string)jsonData["data"]["user_name"];
|
|
netInfo.nick_name = (string)jsonData["data"]["nick_name"];
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 2.2.3 重新获取 access_token
|
|
/// </summary>
|
|
void ResetToken()
|
|
{
|
|
if (string.IsNullOrEmpty(netInfo.access_token)) return;
|
|
StartCoroutine(ReGetToken());
|
|
}
|
|
IEnumerator ReGetToken()
|
|
{
|
|
string url = netInfo.server_id + "/open/v1/token/refresh?" + "access_token=" + netInfo.access_token + "&appid=" + netInfo.appid + "&signature=" + UnityTools.MD5Encrypt32Big(netInfo.access_token + netInfo.appid + netInfo.secret);
|
|
using UnityWebRequest request = UnityWebRequest.Get(url);
|
|
yield return request.SendWebRequest();
|
|
if (request.isDone && string.IsNullOrEmpty(request.error))
|
|
{
|
|
JsonData jsonData = JsonMapper.ToObject(request.downloadHandler.text);
|
|
if ((int)jsonData["code"] != 8)
|
|
{
|
|
netInfo.access_token = (string)jsonData["data"]["access_token"];
|
|
netInfo.create_time = (string)jsonData["data"]["create_time"];
|
|
netInfo.expire_time = (string)jsonData["data"]["expire_time"];
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 3.2.1 实训结果数据回传
|
|
/// </summary>
|
|
public void SendData(string json)
|
|
{
|
|
if (!send) return;
|
|
StartCoroutine(PostData(json));
|
|
}
|
|
IEnumerator PostData(string json)
|
|
{
|
|
string url = netInfo.server_id + "/open/v1/data_upload?" + "access_token=" + netInfo.access_token;
|
|
using UnityWebRequest request = UnityWebRequest.Post(url, json);
|
|
request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
|
|
byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
|
|
using var uploadHandler = new UploadHandlerRaw(bodyRaw);
|
|
// 释放使用UnityWebRequest.Post方法时创建的uploadHandler
|
|
request.uploadHandler.Dispose();
|
|
// 替换为自己创建的uploadHandler
|
|
request.uploadHandler = uploadHandler;
|
|
yield return request.SendWebRequest();
|
|
Debug.Log(request.downloadHandler.text);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 4.0.0 应用在线监测接口
|
|
/// </summary>
|
|
void GetListen()
|
|
{
|
|
StartCoroutine(GetStatusListen());
|
|
}
|
|
IEnumerator GetStatusListen()
|
|
{
|
|
int max = 50;
|
|
int online = 20;
|
|
string url = netInfo.server_id + "/open/v1/ping?" + "max=" + max + "&online=" + online + "&appid=" + netInfo.appid + "&signature=" + UnityTools.MD5Encrypt32Big(netInfo.appid + max + online + netInfo.secret);
|
|
using UnityWebRequest request = UnityWebRequest.Get(url);
|
|
yield return request.SendWebRequest();
|
|
}
|
|
IEnumerator XmlReader(UnityAction action)
|
|
{
|
|
string path = Application.streamingAssetsPath + "/webConfig.xml";
|
|
using UnityWebRequest unityWebRequest = UnityWebRequest.Get(path);
|
|
yield return unityWebRequest.SendWebRequest();
|
|
string content = unityWebRequest.downloadHandler.text;
|
|
XmlDocument doc = new();
|
|
doc.LoadXml(content);
|
|
XmlNodeList nodeList = doc.SelectSingleNode("root").ChildNodes;
|
|
foreach (XmlElement element in nodeList)
|
|
{
|
|
if (element.Name == "ServerId")
|
|
{
|
|
netInfo.server_id = element.InnerText;
|
|
}
|
|
if (element.Name == "Appid")
|
|
{
|
|
netInfo.appid = element.InnerText;
|
|
}
|
|
if (element.Name == "Secret")
|
|
{
|
|
netInfo.secret = element.InnerText;
|
|
}
|
|
}
|
|
action?.Invoke();
|
|
}
|
|
/// <summary>
|
|
/// 测试
|
|
/// </summary>
|
|
IEnumerator GetTest()
|
|
{
|
|
using UnityWebRequest request1 = UnityWebRequest.Get(Application.streamingAssetsPath + "/NetConfig.xml");
|
|
yield return request1.SendWebRequest();
|
|
if (request1.isDone && string.IsNullOrEmpty(request1.error))
|
|
{
|
|
XmlDocument doc = new();
|
|
doc.LoadXml(request1.downloadHandler.text);
|
|
XmlNodeList nodeList = doc.SelectSingleNode("root").ChildNodes;
|
|
foreach (XmlElement element in nodeList)
|
|
{
|
|
if (element.Name == "ServerId")
|
|
{
|
|
netInfo.server_id = element.InnerText;
|
|
}
|
|
if (element.Name == "Appid")
|
|
{
|
|
netInfo.appid = element.InnerText;
|
|
}
|
|
if (element.Name == "Secret")
|
|
{
|
|
netInfo.secret = element.InnerText;
|
|
}
|
|
}
|
|
}
|
|
using UnityWebRequest request2 = UnityWebRequest.Get(netInfo.server_id + "/open/v1/ticket?uid=100");
|
|
yield return request2.SendWebRequest();
|
|
if (request2.isDone && string.IsNullOrEmpty(request2.error))
|
|
{
|
|
JsonData jsonData = JsonMapper.ToObject(request2.downloadHandler.text);
|
|
netInfo.ticket = (string)jsonData["data"];
|
|
}
|
|
using UnityWebRequest request3 = UnityWebRequest.Get(netInfo.server_id + "/open/v1/token?ticket=" + netInfo.ticket + "&appid=" + netInfo.appid + "&signature=" + UnityTools.MD5Encrypt32Big(netInfo.appid + netInfo.ticket + netInfo.secret));
|
|
yield return request3.SendWebRequest();
|
|
if (request3.isDone && string.IsNullOrEmpty(request3.error))
|
|
{
|
|
JsonData jsonData = JsonMapper.ToObject(request3.downloadHandler.text);
|
|
netInfo.access_token = (string)jsonData["data"]["access_token"];
|
|
netInfo.create_time = (string)jsonData["data"]["create_time"];
|
|
netInfo.expire_time = (string)jsonData["data"]["expire_time"];
|
|
netInfo.user_name = (string)jsonData["data"]["user_name"];
|
|
netInfo.nick_name = (string)jsonData["data"]["nick_name"];
|
|
}
|
|
}
|
|
public string GetTestData()
|
|
{
|
|
return "<color=green>TICKET=</color>" + netInfo.ticket +
|
|
"\n<color=green>SERVER_ID=</color>" + netInfo.server_id +
|
|
"\n<color=green>APPID=</color>" + netInfo.appid +
|
|
"\n<color=green>SECRET=</color>" + netInfo.secret +
|
|
"\n<color=green>TOKEN=</color>" + netInfo.access_token +
|
|
"\n<color=green>USER_NAME=</color>" + netInfo.nick_name;
|
|
}
|
|
}
|