http 请求封装

This commit is contained in:
yanghongwei 2024-06-19 00:23:45 +08:00
parent c0036c4b98
commit 81f68f3a1a
5 changed files with 85 additions and 7 deletions

View File

@ -19,6 +19,21 @@
<param name="seconds">缓存时间秒0=int.max </param>
<returns></returns>
</member>
<member name="T:langguanApi.Common.Proxy.HttpProxy">
<summary>
http
</summary>
</member>
<member name="M:langguanApi.Common.Proxy.HttpProxy.Get``1(System.Collections.Generic.Dictionary{System.String,System.String},System.String)">
<summary>
获取数据
</summary>
<typeparam name="T"></typeparam>
<param name="parameters"></param>
<param name="requestUri"></param>
<returns></returns>
<exception cref="T:System.Exception"></exception>
</member>
<member name="F:langguanApi.Common.Redis.RedisHelper._subscriber">
<summary>
sub
@ -2009,7 +2024,7 @@
HomeService
</summary>
</member>
<member name="M:langguanApi.Service.HomeService.#ctor(langguanApi.Service.DeviceService,langguanApi.Service.Hj212Service,Microsoft.Extensions.Configuration.IConfiguration,langguanApi.Common.CacheManager,langguanApi.Service.WeatherService,langguanApi.Service.AlertService,langguanApi.Service.DetectionService)">
<member name="M:langguanApi.Service.HomeService.#ctor(langguanApi.Service.DeviceService,langguanApi.Service.Hj212Service,Microsoft.Extensions.Configuration.IConfiguration,langguanApi.Common.CacheManager,langguanApi.Service.WeatherService,langguanApi.Service.AlertService,langguanApi.Service.DetectionService,langguanApi.Common.Proxy.HttpProxy)">
<summary>
HomeService
</summary>

View File

@ -0,0 +1,48 @@
using Amazon.Runtime;
using langguanApi.Extensions.AutoDI;
namespace langguanApi.Common.Proxy
{
/// <summary>
/// http
/// </summary>
[ServiceInjection(InjectionType.Singleton)]
public class HttpProxy
{
private readonly IHttpClientFactory _httpClientFactory;
public HttpProxy(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
/// <summary>
/// 获取数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="parameters"></param>
/// <param name="requestUri"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<T> Get<T>(Dictionary<string, string> parameters, string requestUri)
{
//从工厂获取请求对象
var client = _httpClientFactory.CreateClient();
if (parameters != null)
{
var strParam = string.Join("&", parameters.Select(o => o.Key + "=" + o.Value));
requestUri = string.Concat(requestUri, '?', strParam);
}
client.BaseAddress = new Uri(requestUri);
var resp = await client.GetAsync(requestUri);
if (resp.IsSuccessStatusCode)
{
var content = await resp.Content.ReadAsStringAsync();
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(content);
}
else
{
Console.WriteLine($"请求失败,状态码:{resp.StatusCode},请求地址 {requestUri}");
return default(T);
}
}
}
}

View File

@ -3,6 +3,7 @@ using langguanApi.Common;
using langguanApi.Extensions.AutoDI;
using langguanApi.Model;
using System.Linq.Expressions;
using langguanApi.Common.Proxy;
namespace langguanApi.Service
{
@ -19,6 +20,7 @@ namespace langguanApi.Service
private readonly WeatherService _weatherService;
private readonly AlertService _alertService;
private readonly DetectionService _detectionService;
private HttpProxy _httpProxy;
/// <summary>
/// HomeService
/// </summary>
@ -27,7 +29,7 @@ namespace langguanApi.Service
public HomeService(DeviceService device, Hj212Service hj212Service,
IConfiguration configuration, CacheManager cacheManager,
WeatherService weatherService, AlertService alertService,
DetectionService detectionService)
DetectionService detectionService, HttpProxy httpProxy)
{
_deviceService = device;
_hj212Service = hj212Service;
@ -36,6 +38,7 @@ namespace langguanApi.Service
_weatherService = weatherService;
_alertService = alertService;
_detectionService = detectionService;
_httpProxy = httpProxy;
}
/// <summary>
/// view
@ -49,7 +52,8 @@ namespace langguanApi.Service
Yesterday = 0.8,
LastWeek = 0.6,
};
Expression<Func<Model.HJ212, bool>> filter = exp => true;
// 首页热力图
var hotmap = new { };
// 首页设备报警数据
var alerts = await _alertService.IndexData();
// 首页设备检测数据
@ -62,6 +66,9 @@ namespace langguanApi.Service
var ariQuality = await _cacheManager.GetConvertVale(RedisKeylist.AriQuality, getAriQualityFunc, 60 * 120);
Func<Task<object>> getTrendFunc = async () => await _hj212Service.GetIndexData();
var trend = await _cacheManager.GetConvertVale(RedisKeylist.Trend, getTrendFunc, 60 * new Random().Next(70));
// alerts += await _httpProxy.Get<string>(null, _configuration.GetValue<string>("Apis:AlertUrl"));
// alerts += await _httpProxy.Get<string>(null, _configuration.GetValue<string>("Apis:RateUrl"));
//首页清洁运输比例
var cleaData = new
{
@ -87,6 +94,7 @@ namespace langguanApi.Service
},
devices,
hotmap,
weather,
ariQuality,
rate = new[] {

View File

@ -82,7 +82,7 @@ namespace langguanApi.Service
}
if (!string.IsNullOrEmpty(OrgId))
{
deviceExp= deviceExp.And(filter => filter.OrgId == OrgId);
deviceExp = deviceExp.And(filter => filter.OrgId == OrgId);
}
var devices = (await _deviceService.GetListWithExp(deviceExp)).ToList();
foreach (var item in result)
@ -106,8 +106,8 @@ namespace langguanApi.Service
public async Task<ApiResult> List(int OrganizedType = 1)
{
List<OrganizedByDeviceDto> dto = new List<OrganizedByDeviceDto>();
Expression<Func<Organized, bool>> exp = filter => filter.OrganizedType == OrganizedType && filter.IsDelete == false;
Expression<Func<Organized, bool>> exp = filter => filter.OrganizedType == OrganizedType
&& filter.IsDelete == false;
var result = (await base.GetListWithExp(exp)).OrderByDescending(x => x.Order).ToList();
return new ApiResult() { code = 0, data = result };
}

View File

@ -16,7 +16,14 @@
"Index": 5
},
"Weather": "https://weather.cma.cn/api/now/54511", //,
"AirQuality": "https://air.cnemc.cn:18007/CityData/GetAQIDataPublishLiveInfo?cityCode=110000",//API
"AirQuality": "https://air.cnemc.cn:18007/CityData/GetAQIDataPublishLiveInfo?cityCode=110000", //API
"Apis": {
"RateUrl": "https://air.cnemc.cn:18007/CityData/GetAQIDataPublishLiveInfo?cityCode=110000", //线API
"AlertUrl": "https://air.cnemc.cn:18007/CityData/GetAQIDataPublishLiveInfo?cityCode=110000", //API
"DeviceList": "https://air.cnemc.cn:18007/CityData/GetAQIDataPublishLiveInfo?cityCode=110000", // API
"DeviceInfo": "https://air.cnemc.cn:18007/CityData/GetAQIDataPublishLiveInfo?cityCode=110000" // API
},
"Home": {
"Title": "鄂托克旗新航焦化有限公司",
"Center": {