コード例 #1
0
 public IActionResult Verify(FormType type, string[] fields)
 {
     var rules = DB.VerificationRules
         .Where(x => x.Type == type)
         .ToList();
     var result = new VerifyResult { IsSuccess = true, Information = "", FailedRules = new List<Rule>() };
     foreach(var x in rules)
     {
         var res = DataVerificationRuleManager.Verify(x.RuleId, fields);
         if (!res.IsSuccess)
         {
             result.IsSuccess = false;
             result.Information += res.Information;
             result.FailedRules.AddRange(res.FailedRules);
         }
     }
     return Json(result.FailedRules.Select(x => x.ArgumentIndex));
 }
コード例 #2
0
        public IActionResult Edit(long id, string[] fields, [FromHeader] string Referer)
        {
            // 准备工作
            var gpsPosition = true;
            var form = DB.Forms.Single(x => x.Id == id);
            var modifyLog = new Log
            {
                Time = DateTime.Now,
                UserId = User.Current.Id,
                FormId = form.Id,
                OriginJson = form.FormJson
            };
            var type = form.Type;
            var result = new VerifyResult { IsSuccess = true, Information = "", FailedRules = new List<Rule>() };

            // 赋值
            try
            {
                form.Lon = Convert.ToDouble(fields[Hash.Lon[type].Value]);
                form.Lat = Convert.ToDouble(fields[Hash.Lat[type].Value]);
            }
            catch
            {
                gpsPosition = false;
            }
            if (Hash.City[type].HasValue)
                form.City = fields[Hash.City[type].Value];
            if (Hash.District[type].HasValue)
                form.District = fields[Hash.District[type].Value];
            if (type == FormType.站址)
            {
                form.Name = fields[0];
            }
            else
            {
                form.StationKey = Convert.ToInt64(fields[Hash.StationId[type].Value]);
            }
            form.UniqueKey = fields[Hash.UniqueKey[type]];
            form.FormJson = JsonConvert.SerializeObject(fields);

            // 根据校验规则进行校验
            var rules = DB.VerificationRules
                .Where(x => x.Type == form.Type)
                .ToList();
            foreach (var x in rules)
            {
                var res = DataVerificationRuleManager.Verify(x.RuleId, fields);
                if (!res.IsSuccess)
                {
                    result.IsSuccess = false;
                    result.Information += res.Information;
                    result.FailedRules.AddRange(res.FailedRules);
                }
            }
            var logs = new List<VerificationLog>();
            foreach(var x in result.FailedRules)
            {
                logs.Add(new VerificationLog { Field = Hash.Headers[type][x.ArgumentIndex], FieldIndex = x.ArgumentIndex, Reason = $"{ Hash.Headers[type][x.ArgumentIndex] }字段没有通过校验" });
            }
            form.Status = result.IsSuccess ? VerificationStatus.Accepted : VerificationStatus.Wrong;
            form.VerificationTime = DateTime.Now;
            // 如果表单是站址,则需要额外校验
            if (form.Type == FormType.站址)
            {
                var city = DB.Cities.SingleOrDefault(x => x.Id == form.City);
                // 1. 判断城市是否合法
                if (city == null)
                {
                    var l = form.VerificationLogs;
                    l.Add(new VerificationLog { Time = DateTime.Now, Field = Hash.Headers[type][3], FieldIndex = 3, Reason = $"不存在城市{form.City}" });
                    form.VerificationJson = JsonConvert.SerializeObject(l);
                    form.Status = VerificationStatus.Wrong;
                }
                // 2. 判断区县是否合法
                else if (!city.Districts.Contains(form.District))
                {
                    var l = form.VerificationLogs;
                    l.Add(new VerificationLog { Time = DateTime.Now, Field = Hash.Headers[type][4], FieldIndex = 4, Reason = $"{city.Id}中不存在区县{form.District}" });
                    form.VerificationJson = JsonConvert.SerializeObject(l);
                    form.Status = VerificationStatus.Wrong;
                }
                // 3a. 判断经纬度是否合法
                else if (!gpsPosition)
                {
                    var l = form.VerificationLogs;
                    l.Add(new VerificationLog { Time = DateTime.Now, Field = Hash.Headers[type][Hash.Lon[type].Value], FieldIndex = Hash.Lon[type].Value, Reason = $"({form.Lon.Value}, {form.Lat.Value})不属于{form.City}" });
                    l.Add(new VerificationLog { Time = DateTime.Now, Field = Hash.Headers[type][Hash.Lat[type].Value], FieldIndex = Hash.Lat[type].Value, Reason = $"({form.Lon.Value}, {form.Lat.Value})不属于{form.City}" });
                    form.VerificationJson = JsonConvert.SerializeObject(l);
                    form.Status = VerificationStatus.Wrong;
                }
                // 3b. 判断经纬度是否合法
                else if (!city.Edge.IsInPolygon(new CodeComb.Algorithm.Geography.Point { X = form.Lon.Value, Y = form.Lat.Value, Type = CodeComb.Algorithm.Geography.PointType.WGS }))
                {
                    var l = form.VerificationLogs;
                    l.Add(new VerificationLog { Time = DateTime.Now, Field = Hash.Headers[type][Hash.Lon[type].Value], FieldIndex = Hash.Lon[type].Value, Reason = $"({form.Lon.Value}, {form.Lat.Value})不属于{form.City}" });
                    l.Add(new VerificationLog { Time = DateTime.Now, Field = Hash.Headers[type][Hash.Lat[type].Value], FieldIndex = Hash.Lat[type].Value, Reason = $"({form.Lon.Value}, {form.Lat.Value})不属于{form.City}" });
                    form.VerificationJson = JsonConvert.SerializeObject(l);
                    form.Status = VerificationStatus.Wrong;
                }
            }
            modifyLog.ModifiedJson = form.FormJson;
            DB.Logs.Add(modifyLog);
            DB.SaveChanges();
            return Prompt(x =>
            {
                x.Title = "修改成功";
                x.Details = "表单信息已经成功保存!";
                x.HideBack = true;
                x.RedirectText = "返回上一页";
                x.RedirectUrl = Referer;
            });
        }