Forráskód Böngészése

添加星历推算输入验证

wyq 1 éve
szülő
commit
712726243f

+ 1 - 0
XdCxRhDW.WebApi/Controllers/DetectCgController.cs

@@ -108,6 +108,7 @@ namespace XdCxRhDW.WebApi.Controllers
         /// <param name="dto">信号检测参数</param>
         /// <returns></returns>
         [HttpPost]
+       [CustomValidation(typeof(DetectDto), "Validate")]
         public async Task<AjaxResult<IEnumerable<DetectResDto>>> DetectCalc(DetectDto dto)
         {
             dto.file1 = GetLocalFile(dto.file1);

+ 41 - 24
XdCxRhDW.WebApi/Controllers/XlController.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
 using System.Data.Entity;
 using System.Data.SqlClient;
 using System.Linq;
@@ -25,21 +26,29 @@ namespace XdCxRhDW.WebApi.Controllers
         /// <param name="dto">推算参数</param>
         /// <returns></returns>
         [HttpPost]
+        [CustomValidation(typeof(XlCalcDto), "Validate")]
         public AjaxResult<SatEphDto> Calc(XlCalcDto dto)
         {
-            var p = EphHelper.Calc(dto.tleStr, dto.dt);
-            return Success(new SatEphDto()
+            try
             {
-                SatId = p.SatId,
-                SatTime = p.SatTime,
-                TleTime = p.TleTime,
-                X = p.X,
-                Y = p.Y,
-                Z = p.Z,
-                VX = p.VX,
-                VY = p.VY,
-                VZ = p.VZ,
-            });
+                var p = EphHelper.Calc(dto.tleStr, dto.dt);
+                return Success(new SatEphDto()
+                {
+                    SatId = p.SatId,
+                    SatTime = p.SatTime,
+                    TleTime = p.TleTime,
+                    X = p.X,
+                    Y = p.Y,
+                    Z = p.Z,
+                    VX = p.VX,
+                    VY = p.VY,
+                    VZ = p.VZ,
+                });
+            }
+            catch (Exception ex)
+            {
+                return Error<SatEphDto>(ex.Message);
+            }
         }
 
         /// <summary>
@@ -48,21 +57,29 @@ namespace XdCxRhDW.WebApi.Controllers
         /// <param name="dto">推算参数</param>
         /// <returns></returns>
         [HttpPost]
+        [CustomValidation(typeof(XlCalcMultDto), "Validate")]
         public AjaxResult<List<EphResDto>> CalcMult(XlCalcMultDto dto)
         {
-            var eph = EphHelper.CalcMult(dto.tleStr, dto.start, dto.end, dto.spanSeconds);
-            return Success(eph.Select(p => new EphResDto()
+            try
+            {
+                var eph = EphHelper.CalcMult(dto.tleStr, dto.start, dto.end, dto.spanSeconds);
+                return Success(eph.Select(p => new EphResDto()
+                {
+                    SatId = p.SatId,
+                    SatTime = p.SatTime,
+                    TleTime = p.TleTime,
+                    X = p.X,
+                    Y = p.Y,
+                    Z = p.Z,
+                    VX = p.VX,
+                    VY = p.VY,
+                    VZ = p.VZ,
+                }).ToList());
+            }
+            catch (Exception ex)
             {
-                SatId = p.SatId,
-                SatTime = p.SatTime,
-                TleTime = p.TleTime,
-                X = p.X,
-                Y = p.Y,
-                Z = p.Z,
-                VX = p.VX,
-                VY = p.VY,
-                VZ = p.VZ,
-            }).ToList());
+                return Error<List<EphResDto>>(ex.Message);
+            }
         }
     }
 }

+ 38 - 0
XdCxRhDw.Dto/Attribute/FileMustExistAttribute.cs

@@ -47,4 +47,42 @@ namespace XdCxRhDw.Dto.Attribute
             return File.Exists(localFile);
         }
     }
+
+    /// <summary>
+    /// 双行根验证,会验证格式
+    /// </summary>
+    public class TleStrAttribute : ValidationAttribute
+    {
+        /// <summary>
+        /// 
+        /// </summary>
+        public TleStrAttribute()
+        {
+        }
+        /// <summary>
+        /// 文件验证
+        /// </summary>
+        /// <param name="value"></param>
+        /// <returns></returns>
+        /// <exception cref="Exception"></exception>
+        public override bool IsValid(object value)
+        {
+            if (value.GetType() != typeof(string))
+            {
+                throw new Exception($"{nameof(TleStrAttribute)}只能用于string类型!");
+            }
+            string tleStr = value.ToString();
+            if (string.IsNullOrWhiteSpace(tleStr))
+            {
+                ErrorMessage = "字段 {0} 值不能为空";
+                return false;
+            }
+            if (!tleStr.Contains(";"))
+            {
+                ErrorMessage = $"双行根数[{value}]格式错误,(line1和line2用分号拼到一起)";
+                return false;
+            }
+            return true;
+        }
+    }
 }

+ 1 - 0
XdCxRhDw.Dto/PosDto/X1D1PosDto.cs

@@ -1,5 +1,6 @@
 using System;
 using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

+ 2 - 0
XdCxRhDw.Dto/XlCalcDto/XlCalcDto.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using XdCxRhDw.Dto.Attribute;
 
 namespace XdCxRhDw.Dto
 {
@@ -14,6 +15,7 @@ namespace XdCxRhDw.Dto
         /// <summary>
         /// 双行根数(line1和line2用分号拼到一起)
         /// </summary>
+        [TleStrAttribute]
         public string tleStr { get; set; }
 
         /// <summary>

+ 3 - 0
XdCxRhDw.Dto/XlCalcDto/XlCalcMultDto.cs

@@ -3,6 +3,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using XdCxRhDw.Dto.Attribute;
 
 namespace XdCxRhDw.Dto
 {
@@ -14,6 +15,7 @@ namespace XdCxRhDw.Dto
         /// <summary>
         /// 双行根数(line1和line2用分号拼到一起)
         /// </summary>
+        [TleStrAttribute]
         public string tleStr { get; set; }
 
         /// <summary>
@@ -29,6 +31,7 @@ namespace XdCxRhDw.Dto
         /// <summary>
         /// 推算间隔(秒)
         /// </summary>
+        [RangeInt(0,IncludeMin = true)]
         public int spanSeconds { get; set; }
     }
 }