From ae0310a51452ef715a5475dbd892752a4bb82182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=B9=8F=E9=B9=8F?= <304594656@qq.com> Date: Tue, 11 Feb 2025 13:58:37 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=A2=9E=E5=8A=A0=20WasteAuthorizeAttribut?= =?UTF-8?q?e=20=E6=8E=88=E6=9D=83=E9=AA=8C=E8=AF=81Filter=20-=20=E6=8A=95?= =?UTF-8?q?=E6=94=BE=E8=AE=B0=E5=BD=95=E5=88=97=E8=A1=A8=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=8A=A0=E4=B8=8A=20WasteAuthorizeAttribute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Nirvana.Common/Common.cs | 10 +- Waste.Application/ApiAsyncActionFilter.cs | 34 ++++- .../ResultInfos/ResultService.cs | 1 + Waste.Application/ThirdApiInfo/OpenService.cs | 141 ++++++++++-------- 4 files changed, 123 insertions(+), 63 deletions(-) diff --git a/Nirvana.Common/Common.cs b/Nirvana.Common/Common.cs index 51ee377..531c9c4 100644 --- a/Nirvana.Common/Common.cs +++ b/Nirvana.Common/Common.cs @@ -10,7 +10,7 @@ namespace Nirvana.Common /// /// 常用公共类 /// - public class Common + public static class Common { #region Stopwatch计时器 /// @@ -293,5 +293,13 @@ namespace Nirvana.Common return BitConverter.ToInt64(buffer, 0); } + public static T GetListValue(this IList list, int index, T def = default) + { + if (list.Count > index) + { + return list[index]; + } + return def; + } } } diff --git a/Waste.Application/ApiAsyncActionFilter.cs b/Waste.Application/ApiAsyncActionFilter.cs index 340ff11..5fdcfee 100644 --- a/Waste.Application/ApiAsyncActionFilter.cs +++ b/Waste.Application/ApiAsyncActionFilter.cs @@ -3,9 +3,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Nirvana.Common; using System; -using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading.Tasks; namespace Waste.Application @@ -41,7 +39,7 @@ namespace Waste.Application } string ip = request.HttpContext.Connection.RemoteIpAddress.ToString(); var res = await _businessApiService.IsWhiteIP(appid, ip); - if(res.code != ApiResultState.SUCCESS) + if (res.code != ApiResultState.SUCCESS) { var jsonresult = new JsonResult(res); context.Result = jsonresult; @@ -59,4 +57,34 @@ namespace Waste.Application } } } + + public class WasteAuthorizeAttribute : Attribute, IAsyncActionFilter, IAsyncPageFilter + { + public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) + { + if (OperatorProvider.Provider.GetCurrent() != null && (OperatorProvider.Provider.GetCurrent().UserId != Guid.Empty || OperatorProvider.Provider.GetCurrent().IsSuper)) + { + _ = await next.Invoke(); + return; + } + + context.Result = new StatusCodeResult(401); + } + + public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) + { + return Task.CompletedTask; + } + + public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) + { + if (OperatorProvider.Provider.GetCurrent() != null && (OperatorProvider.Provider.GetCurrent().UserId != Guid.Empty || OperatorProvider.Provider.GetCurrent().IsSuper)) + { + _ = await next.Invoke(); + return; + } + + context.Result = new StatusCodeResult(401); + } + } } diff --git a/Waste.Application/ResultInfos/ResultService.cs b/Waste.Application/ResultInfos/ResultService.cs index 28d2fac..ab280b9 100644 --- a/Waste.Application/ResultInfos/ResultService.cs +++ b/Waste.Application/ResultInfos/ResultService.cs @@ -38,6 +38,7 @@ namespace Waste.Application /// /// /// + [WasteAuthorize] public async Task> GetListAsync(QueryParams param) { RefAsync totalnum = 0; diff --git a/Waste.Application/ThirdApiInfo/OpenService.cs b/Waste.Application/ThirdApiInfo/OpenService.cs index 6b5f07f..61601f3 100644 --- a/Waste.Application/ThirdApiInfo/OpenService.cs +++ b/Waste.Application/ThirdApiInfo/OpenService.cs @@ -158,10 +158,24 @@ namespace Waste.Application.ThirdApiInfo { return new ResultInfo(ResultState.FAIL, "设备还未获取验证信息"); } + + + // trash@1@62942200205496@4.964 trash@垃圾种类@垃圾桶编号@垃圾重量 + if (!string.IsNullOrEmpty(data.data) && data.data.StartsWith("trash@")) + { + var arr = data.data.Split('@'); + var type = arr.GetListValue(1); + returndata.trash = arr.GetListValue(2); + var weight = arr.GetListValue(3); + + var result = await UpdateDevInfo(data, returndata, device, type, weight); + if (result != null) return result; + + } //解析协议,IC卡数据@垃圾桶编号@厨余垃圾@7.91 // 00000000003031 40 0F00010009 40 C6E4CBFBC0ACBBF8 40 31352E39 // 00000000003031 40 000F000002 40 C6E4CBFBC0ACBBF8 40 35312E30 0D0A - if (!string.IsNullOrEmpty(data.data) && data.data.Length > 52) + else if (!string.IsNullOrEmpty(data.data) && data.data.Length > 52) { data.data = data.data.Replace(" ", ""); //收到的为16进制,对数据进行解析,0-4预留,5-垃圾种类,6-垃圾桶大小,7-@,8-12垃圾桶编号,13@,14-21垃圾种类汉字,22@,23-结束重量, OD OA 回车换行 @@ -173,65 +187,9 @@ namespace Waste.Application.ThirdApiInfo returndata.trash = HextToDec(trashhex).ToString(); //垃圾桶编号使用10进制 var type = GetChsFromHex(typehex); var weight = GetChsFromHex(weighthex); - returndata.type = TrashType(type); - returndata.Weight = weight.ToDouble(); - //计算净重,毛重-皮重=净重,如果净重小于等于0则不上报保存 - returndata.Weight = (returndata.Weight - device.Tare.ToDouble()).ToDouble(2); - if (returndata.Weight <= 0) - { - _loggerService.AddLogger($"{data.ECode},{device.Name},重量小于等于0:{returndata.ToJson()}", 1); - return new ResultInfo(ResultState.FAIL, "无效的重量"); - } - //检查是否为15分钟内第一次上报 - //如果是巴城的设备则不使用这个限制 - if (device.Businessid != Guid.Parse("39FCB9DE-404E-68F5-384B-EE2462EAB87C")) - { - var time15 = DateTime.Now.AddMinutes(-15); - if (await dbClient.Queryable().AnyAsync(x => x.DeviceId == device.Id && x.LastTrash == returndata.trash && x.LastHeartTime > time15)) - { - _loggerService.AddLogger($"{data.ECode},{device.Name},重复垃圾桶编号的数据:{returndata.ToJson()}", 1); - return new ResultInfo(ResultState.FAIL, "15分钟内同一垃圾桶编号上报"); - } - } - returndata.IsSuccessed = true; - //记录数据 - data.IMEI = data.IMEI.ToStr(); - data.ICCID = data.ICCID.ToStr(); - data.IMSI = data.IMSI.ToStr(); - await _capBus.PublishAsync("result.service.insert", new ResultS2SDto - { - BusinessId = device.Businessid, - DeviceId = device.Id, - gslq = data.GSLQ, - iccid = data.ICCID, - imei = data.IMEI, - imsi = data.IMSI, - LastHeartTime = device.LastHeartTime, - latitude = data.Latitude, - longtitude = data.Longitude, - ResultId = returndata.ResultId, - Tare = device.Tare, - trash = returndata.trash, - wastetype = type, - weight = weight - }); - //推送数据给第三方 - await _resultService.SendMessageToThird(new SendThirdMessageSubscribeS2SDto - { - DeviceId = device.Id, - WasteSType = "", - Time = DateTime.Now, - TrashCode = returndata.trash, - WasteType = type, - Weight = weight.ToDecimal(), - faccode = device.FacEcode, - ecode = device.Ecode, - province = device.Province, - city = device.City, - area = device.Area, - address = device.Address - }); + var result = await UpdateDevInfo(data, returndata, device, type, weight); + if (result != null) return result; } else { @@ -584,5 +542,70 @@ namespace Waste.Application.ThirdApiInfo { return ((int)bt).ToString("X2"); } + + private async Task UpdateDevInfo(GetDevInfoRequestDto data, GetDevInfoResponseDto returndata, W_Device device, + string type, string weight) + { + returndata.type = TrashType(type); + returndata.Weight = weight.ToDouble(); + //计算净重,毛重-皮重=净重,如果净重小于等于0则不上报保存 + returndata.Weight = (returndata.Weight - device.Tare.ToDouble()).ToDouble(2); + if (returndata.Weight <= 0) + { + _loggerService.AddLogger($"{data.ECode},{device.Name},重量小于等于0:{returndata.ToJson()}", 1); + return new ResultInfo(ResultState.FAIL, "无效的重量"); + } + //检查是否为15分钟内第一次上报 + //如果是巴城的设备则不使用这个限制 + if (device.Businessid != Guid.Parse("39FCB9DE-404E-68F5-384B-EE2462EAB87C")) + { + var time15 = DateTime.Now.AddMinutes(-15); + if (await dbClient.Queryable().AnyAsync(x => x.DeviceId == device.Id && x.LastTrash == returndata.trash && x.LastHeartTime > time15)) + { + _loggerService.AddLogger($"{data.ECode},{device.Name},重复垃圾桶编号的数据:{returndata.ToJson()}", 1); + return new ResultInfo(ResultState.FAIL, "15分钟内同一垃圾桶编号上报"); + } + } + returndata.IsSuccessed = true; + //记录数据 + data.IMEI = data.IMEI.ToStr(); + data.ICCID = data.ICCID.ToStr(); + data.IMSI = data.IMSI.ToStr(); + await _capBus.PublishAsync("result.service.insert", new ResultS2SDto + { + BusinessId = device.Businessid, + DeviceId = device.Id, + gslq = data.GSLQ, + iccid = data.ICCID, + imei = data.IMEI, + imsi = data.IMSI, + LastHeartTime = device.LastHeartTime, + latitude = data.Latitude, + longtitude = data.Longitude, + ResultId = returndata.ResultId, + Tare = device.Tare, + trash = returndata.trash, + wastetype = type, + weight = weight + }); + + //推送数据给第三方 + await _resultService.SendMessageToThird(new SendThirdMessageSubscribeS2SDto + { + DeviceId = device.Id, + WasteSType = "", + Time = DateTime.Now, + TrashCode = returndata.trash, + WasteType = type, + Weight = weight.ToDecimal(), + faccode = device.FacEcode, + ecode = device.Ecode, + province = device.Province, + city = device.City, + area = device.Area, + address = device.Address + }); + return null; + } } } \ No newline at end of file