Files
ZHGD_Web/Assets/Scripts/Managers/HttpManager.cs

218 lines
7.0 KiB
C#
Raw Permalink Normal View History

2025-07-13 23:16:20 +08:00
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using LitJson;
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
public class HttpManager : MonoBehaviour
{
private static HttpManager instance;
public static HttpManager Instance
{
get
{
if (instance == null)
{
instance = GameObject.Find("Managers").GetComponent<HttpManager>();
}
return instance;
}
}
private string m_BaseUrl = "https://yqkfq.yueqing.gov.cn/yq-api/xt-sps/parkTwin/select";
private Dictionary<string, Texture2D> m_textureCache = new Dictionary<string, Texture2D>();
private void Start()
{
}
public Texture2D GetTexture(string path)
{
Texture2D tex = null;
if (m_textureCache.TryGetValue(path, out tex))
{
return tex;
}
return null;
}
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
/// </summary>
/// <returns></returns>
public IEnumerator GetWeather(Action<WeatherInfo> complete)
{
UnityWebRequest request = new UnityWebRequest("https://yqkfq.yueqing.gov.cn/yq-api/xt-system/common/getWeather", UnityWebRequest.kHttpVerbPOST);
string json = "{\"location_type\": 1, \"lat\": 28.066292, \"lng\": 120.990018 }";
DownloadHandler downloadHandler = new DownloadHandlerBuffer();
request.downloadHandler = downloadHandler;
request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
UnityWebRequestAsyncOperation op = request.SendWebRequest();
while (!op.isDone)
{
yield return null;
}
if (string.IsNullOrEmpty(request.error))
{
//Debug.Log(request.downloadHandler.text);
JsonData jsonData = JsonMapper.ToObject(request.downloadHandler.text);
WeatherInfo weatherInfo = new WeatherInfo();
weatherInfo.weatherType = jsonData["data"]["data"]["weather"]["weather"].ToString();
weatherInfo.temp = weatherInfo.weatherType + " " + jsonData["data"]["data"]["weather"]["temp"].ToString() + "<22><>";
//Debug.Log(request.downloadHandler.text);
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
complete?.Invoke(weatherInfo);
}
}
public IEnumerator LoadTextureAsync(string path, Action<Texture2D> ac)
{
Texture2D tex = GetTexture(path);
if (tex == null)
{
UnityWebRequest req = UnityWebRequest.Get(path);
UnityWebRequestAsyncOperation op = req.SendWebRequest();
while (!op.isDone)
{
yield return null;
}
if (op.webRequest.isHttpError || op.webRequest.isNetworkError)
{
Debug.Log("Get web resource failed: " + path);
Debug.Log("ErrorCode: " + op.webRequest.responseCode);
yield break;
}
byte[] bytes = op.webRequest.downloadHandler.data;
tex = new Texture2D(1, 1);
if (!tex.LoadImage(bytes))
{
Debug.Log("LoadImage error: " + path);
yield break;
}
if (!m_textureCache.ContainsKey(path))
{
m_textureCache.Add(path, tex);
}
}
ac?.Invoke(tex);
}
/// <summary>
/// <20><>ȡ<EFBFBD><C8A1>Ϣ
/// </summary>
/// <param name="path"></param>
/// <param name="callback"></param>
/// <returns></returns>
public IEnumerator LoadTextAsync(string path, System.Action<string> callback)
{
string result = null;
UnityWebRequest req = UnityWebRequest.Get(path);
UnityWebRequestAsyncOperation op = req.SendWebRequest();
while (!op.isDone)
{
yield return null;
}
if (op.webRequest.isHttpError || op.webRequest.isNetworkError)
{
Debug.Log("Get web resource failed: " + path);
Debug.Log("ErrorCode: " + op.webRequest.responseCode);
}
else
{
result = op.webRequest.downloadHandler.text;
}
//Debug.Log(path + ":" + result);
if (callback != null)
{
//Debug.Log(path + ":" + result);
callback(result);
}
}
public async void Down(System.Action<string> callback)
{
string url = @"https://www.tianqi.com/pudong/";
string s = await GetPage(url, null);
if(callback != null)
{
callback?.Invoke(s);
}
}
public async Task<string> GetPage(string url, string cookies)
{
return await Task.Run(() =>
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
//request.Referer = $"https://{AppValue.Host}/";
request.Headers.Add("Upgrade-Insecure-Requests", "1");
request.KeepAlive = true;
//request.Headers.Add("Cookie", cookies);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.StackTrace);
}
return null;
});
}
/// <summary>
/// ͨ<><CDA8><EFBFBD><EFBFBD>γ<EFBFBD>Ȼ<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="lng"></param>
/// <param name="lat"></param>
/// 3576.8 -1553.5 120.976111 121.021983 lng 3576.8f - (now - 120.976111f) / 0.045872f * 5130.3f
/// -3207.6 3428.3 28.091056 28.038424 lat -3207.6f + (28.091056f - now) / 0.052632f * 6635.9f
/// <returns></returns>
private Vector3 GetVectByLnat(double lng, double lat)
{
double x = 3576.8f - (lng - 120.976111f) / 0.045872f * 5130.3f;
double z = -3207.6f + (28.091056f - lat) / 0.052632f * 6635.9f;
return new Vector3((float)x, 45, (float)z);
}
}
public class WeatherInfo
{
public string temp;
public string weatherType;
}