培训考核三期,新版培训,网页版培训登录器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

301 lines
9.8 KiB

using BestHTTP;
using Newtonsoft.Json;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using System.Security.Cryptography;
#pragma warning disable CS8632 // 只能在 "#nullable" 注释上下文内的代码中使用可为 null 的引用类型的注释。
#pragma warning disable IDE1006 // 命名样式
public class testData
{
public int currentPage;
public int pageSize;
public string sign;
public long timestamp;
}
public class BuildingInfo
{
/// <summary>
/// 对接平台id
/// </summary>
public string accessId { get; set; }
/// <summary>
/// 对接秘钥
/// </summary>
public string accessKey { get; set; }
/// <summary>
/// 地址
/// </summary>
public string address { get; set; }
/// <summary>
/// 建筑面积
/// </summary>
public float? area { get; set; }
/// <summary>
/// 城市编码
/// </summary>
public int? city { get; set; }
/// <summary>
/// 城市名称
/// </summary>
public string cityName { get; set; }
/// <summary>
/// 建筑年份
/// </summary>
public string completedYear { get; set; }
/// <summary>
/// 消控室电话
/// </summary>
public string controlPhone { get; set; }
/// <summary>
/// 区县编码
/// </summary>
public int county { get; set; }
/// <summary>
/// 区县名称
/// </summary>
public string countyName { get; set; }
/// <summary>
/// 占地面积
/// </summary>
public float? coverArea { get; set; }
/// <summary>
/// 街道编码
/// </summary>
public int? district { get; set; }
/// <summary>
/// 街道名称
/// </summary>
public string districtName { get; set; }
/// <summary>
/// 建筑高度
/// </summary>
public float? height { get; set; }
/// <summary>
/// 高度类型
/// </summary>
public int? heightType { get; set; }
/// <summary>
/// 高度类型名称
/// </summary>
public string heightTypeName { get; set; }
/// <summary>
/// 编号
/// </summary>
public string id { get; set; }
/// <summary>
/// 地理坐标y(纬度)
/// </summary>
public double? lat { get; set; }
/// <summary>
/// 地理坐标x(经度)
/// </summary>
public double? lng { get; set; }
/// <summary>
/// 建筑名称
/// </summary>
public string name { get; set; }
/// <summary>
/// 省份编码
/// </summary>
public int? province { get; set; }
/// <summary>
/// 省份名称
/// </summary>
public string provinceName { get; set; }
/// <summary>
/// 建筑结构code
/// </summary>
public int? structure { get; set; }
/// <summary>
/// 建筑结构名称
/// </summary>
public string structureName { get; set; }
/// <summary>
/// 建筑类型
/// </summary>
public int? type { get; set; }
/// <summary>
/// 建筑类型名称
/// </summary>
public string typeName { get; set; }
/// <summary>
/// 地下层数
/// </summary>
public int? underFloor { get; set; }
/// <summary>
/// 地上层数
/// </summary>
public int? upperFloor { get; set; }
/// <summary>
/// 使用性质code
/// </summary>
public int? useNature { get; set; }
/// <summary>
/// 使用性质名称
/// </summary>
public string useNatureName { get; set; }
/// <summary>
/// 详细地址
/// </summary>
public string wholeAddress { get; set; }
}
public class PageInfoList<T>
{
public int? endRow { get; set; }
public bool? hasNextPage { get; set; }
public bool? hasPreviousPage { get; set; }
public bool? isFirstPage { get; set; }
public bool? isLastPage { get; set; }
/// <summary>
/// 数据
/// </summary>
public List<T> list { get; set; }
public int? navigateFirstPage { get; set; }
public int? navigateLastPage { get; set; }
public int? navigatePages { get; set; }
public List<int> navigatepageNums { get; set; }
public int? nextPage { get; set; }
public int? pageNum { get; set; }
public int? pageSize { get; set; }
public int? pages { get; set; }
public int? prePage { get; set; }
public int? size { get; set; }
public int? startRow { get; set; }
public int? total { get; set; }
}
public class TotalInfo
{
/// <summary>
/// 状态码
/// </summary>
public int? code { get; set; }
/// <summary>
/// 数据
/// </summary>
public PageInfoList<BuildingInfo> data { get; set; }
/// <summary>
/// 消息
/// </summary>
public string message { get; set; }
}
#pragma warning restore CS8632 // 只能在 "#nullable" 注释上下文内的代码中使用可为 null 的引用类型的注释。
#pragma warning restore IDE1006 // 命名样式
public class HttpTest : MonoBehaviour
{
private string baseHttpUrl = "http://39.108.36.226:81/api/iot/open/building/list";
private testData testData;
private const string key = "2f88b96ff4ac43b4bfbd2b5f99610a01";
private const string secret = "ccb53d34773a43069d17e2c6d3cfd0d1";
private void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
using (MD5 md5Hash = MD5.Create())
{
testData = new testData();
testData.currentPage = 1;
testData.pageSize = 10000;
testData.timestamp = System.DateTime.Now.Ticks / 10000;
string signstr = $"currentPage=1&pageSize=10000&timestamp={testData.timestamp}&secret={secret}";
byte[] signbyte = Encoding.UTF8.GetBytes(signstr);
byte[] signmdbyte = md5Hash.ComputeHash(signbyte);
string StrResult = BitConverter.ToString(signmdbyte);
StrResult = StrResult.Replace("-", "").ToLower();
testData.sign = StrResult;
//testData.sign = "cc22ec7691a09a7e8a863c333b1bca11";
Debug.Log(testData.sign);
PostJson<testData, TotalInfo>("?appKey=2f88b96ff4ac43b4bfbd2b5f99610a01", testData, (d) =>
{
Debug.Log(d.code);
List<BuildingInfo> tdata = new List<BuildingInfo>();
for (int i = 0; i < d.data.list.Count; i++)
{
var info = d.data.list[i];
if (info.countyName.Contains("虹口区"))
{
Debug.Log($"虹口区:{info.name}---{info.id}");
}
}
}, (a, b) =>
{
Debug.Log(a + b);
});
}
}
}
/// <summary>
/// 提交json数据
/// </summary>
/// <typeparam name="TIn">提交数据类型</typeparam>
/// <typeparam name="TOut">返回数据类型</typeparam>
/// <param name="apiUrl">API URL</param>
/// <param name="data">提交数据</param>
/// <param name="success">请求成功回调</param>
/// <param name="error">请求错误回调</param>
public void PostJson<TIn, TOut>(string apiUrl, TIn data, Action<TOut> success, Action<int, string> error = null)
{
if (string.IsNullOrEmpty(apiUrl))
{
throw new ArgumentNullException($"API URL:{apiUrl}无效!");
}
HTTPRequest request = new HTTPRequest(new Uri(baseHttpUrl + apiUrl), HTTPMethods.Post, (req, resp) =>
{
switch (req.State)
{
case HTTPRequestStates.Finished:
if (resp.IsSuccess)
{
TOut result = JsonConvert.DeserializeObject<TOut>(resp.DataAsText);
success?.Invoke(result);
}
else
{
Debug.LogWarning(string.Format("Request finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}",
resp.StatusCode,
resp.Message,
resp.DataAsText));
error?.Invoke(resp.StatusCode, JsonConvert.DeserializeObject<ProblemDetails>(resp.DataAsText).detail);
}
break;
case HTTPRequestStates.Error:
Debug.LogError("Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"));
error?.Invoke((int)HTTPRequestStates.Error, "Request Finished with Error!");
break;
case HTTPRequestStates.Aborted:
Debug.LogWarning("Request Aborted!");
error?.Invoke((int)HTTPRequestStates.Aborted, "Request Aborted!");
break;
case HTTPRequestStates.ConnectionTimedOut:
Debug.LogError("Connection Timed Out!");
error?.Invoke((int)HTTPRequestStates.ConnectionTimedOut, "Connection Timed Out!");
break;
case HTTPRequestStates.TimedOut:
Debug.LogError("Processing the request Timed Out!");
error?.Invoke((int)HTTPRequestStates.TimedOut, "Processing the request Timed Out!");
break;
}
});
//request.SetHeader("Authorization", "Bearer " + identityInfo.token);
request.SetHeader("Content-Type", "application/json; charset=UTF-8");
request.SetHeader("dev", "open_service");
string json = JsonConvert.SerializeObject(data);
request.RawData = Encoding.UTF8.GetBytes(json);
request.Send();
}
}