/** * 参数校验 * * @param checkFiled 需要校验的字段 * @param value 传入参数值 * @param path 路径 (获取校验文件路径) * @param method 需要校验方法 * * @return String **/ public static String CheckParam(String checkFiled, Object value, String path, String method) { try { String code = "200"; //bool flag = false; int max = 64; String type = ""; String apiPath = path; if (path.Contains("/")) { path = path.Substring(0, path.IndexOf("/")); } //String[] fileds = {}; String checkObject = ""; //获取需要校验的key Dictionary <String, String[]> checkInfo = GetCheckInfo(apiPath, method); foreach (var item in checkInfo) { //fileds = entry.getValue(); checkObject = item.Key; } JToken verify = FromPath(path + VERIFY_JSON_NAME); List <String> keys = GetKeys(((JObject)verify).GetValue(checkObject)); JObject entity = (JObject)((JObject)verify).GetValue(checkObject); foreach (String key in keys) { if (checkFiled.Equals(key)) { JObject obj = (JObject)entity.GetValue(checkFiled); if (obj.GetValue("require") != null) { Boolean must = (Boolean)((JObject)obj.GetValue("require")).GetValue("must"); if (value.GetType() == typeof(String)) { if (String.IsNullOrEmpty(value.ToString())) { code = (String)((JObject)obj.GetValue("require")).GetValue("invalid"); } } else { if (null == value) { code = (String)((JObject)obj.GetValue("require")).GetValue("invalid"); } } } if (obj.GetValue("length") != null) { max = (int)((JObject)obj.GetValue("length")).GetValue("max"); if (value.GetType() == typeof(String)) { if ("200".Equals(code) && String.IsNullOrEmpty(value.ToString())) { code = (String)((JObject)obj.GetValue("length")).GetValue("invalid"); } if ("200".Equals(code) && value.ToString().Length > max) { code = (String)((JObject)(JObject)obj.GetValue("length")).GetValue("invalid"); } } else if (value.GetType() == typeof(String[])) { String[] valueTemp = { }; try { valueTemp = (String[])value; } catch (Exception e) { code = (String)((JObject)obj.GetValue("typeof")).GetValue("invalid"); } if ("200".Equals(code) && valueTemp.Length > max) { code = (String)((JObject)obj.GetValue("length")).GetValue("invalid"); } } } if (obj.GetValue("size") != null) { max = (int)((JObject)obj.GetValue("size")).GetValue("max"); type = (String)((JObject)obj.GetValue("typeof")).GetValue("type"); if (type.Contains("array")) { String[] valueTemp = null; if ("200".Equals(code) && null == value) { code = (String)((JObject)obj.GetValue("size")).GetValue("invalid"); } try { valueTemp = (String[])value; } catch (Exception e) { code = (String)((JObject)obj.GetValue("typeof")).GetValue("invalid"); } if ("200".Equals(code) && valueTemp.Length > max) { code = (String)((JObject)obj.GetValue("size")).GetValue("invalid"); } } else if (type.Contains("int")) { int valueTemp = 64; try { valueTemp = (int)value; } catch (Exception e) { code = (String)((JObject)obj.GetValue("typeof")).GetValue("invalid"); } if ("200".Equals(code) && null == value) { code = (String)((JObject)obj.GetValue("size")).GetValue("invalid"); } if ("200".Equals(code) && valueTemp > max) { code = (String)((JObject)obj.GetValue("size")).GetValue("invalid"); } } } String message = (String)CommonUtil.GetErrorMessage(apiPath, method, code, checkFiled, max.ToString(), "1", type); if (null != message) { message = message.Replace("errorMessage", "msg"); return(message); } } } } catch (Exception e) { Console.WriteLine(e.Message); throw e; } return(null); }
/** * 参数校验方法 * * @param model 校验对象 * @param path 路径 * @param method 需要校验方法 * * @return String **/ public static String CheckFiled(Object model, String path, String method) { try { String code = "200"; int max = 64; //api.json 的路径 String apiPath = path; String type = ""; if (path.Contains("/")) { path = path.Substring(0, path.IndexOf("/")); } String[] fileds = { }; String checkObjectKey = ""; //获取需要校验的参数 Dictionary <String, String[]> checkInfo = GetCheckInfo(apiPath, method); foreach (var item in checkInfo) { checkObjectKey = item.Key; fileds = item.Value; } //获取校验文件 JObject verify = FromPath(path + VERIFY_JSON_NAME); //获取校验key List <String> keys = GetKeys(verify.GetValue(checkObjectKey)); //获取具体校验规则 JObject entity = (JObject)verify.GetValue(checkObjectKey); //Dictionary<String, Object> entity = verify.GetValue(checkObjectKey).ToObject<Dictionary<String, Object>>(); foreach (String name in fileds) { foreach (String key in keys) { if (name.Equals(key)) { //将属性的首字符大写,方便构造get,set方法 String nameTemp = name.Substring(0, 1).ToUpper() + name.Substring(1); //获取属性的类型 //String type = field.getGenericType().ToString(); MethodInfo m = model.GetType().GetMethod("Get" + nameTemp); PropertyInfo propertyInfo = model.GetType().GetProperty(nameTemp); //.GetValue(model, null) //获取字段的具体校验规则 JObject obj = (JObject)entity.GetValue(name); if (obj.GetValue("require") != null) { Boolean must = (Boolean)((JObject)obj.GetValue("require")).GetValue("must"); Object result = null; if (null != m) { result = m.Invoke(model, new Object[] { }); } else { result = propertyInfo.GetValue(model, null); } if (result != null && result.GetType() == typeof(String)) { String value = (String)result; if (String.IsNullOrEmpty(value)) { code = (String)((JObject)obj.GetValue("require")).GetValue("invalid"); } } else { Object value = result; if (null == value) { code = (String)((JObject)obj.GetValue("require")).GetValue("invalid"); } } } if (obj.GetValue("length") != null) { max = (int)((JObject)obj.GetValue("length")).GetValue("max"); Object result = null; if (null != m) { result = m.Invoke(model, new Object[] { }); } else { result = propertyInfo.GetValue(model, null); } if (null != result && result.GetType() == typeof(String)) { String value = (String)result; if ("200".Equals(code) && String.IsNullOrEmpty(value)) { code = (String)((JObject)obj.GetValue("length")).GetValue("invalid"); } if ("200".Equals(code) && value.Length > max) { code = (String)((JObject)obj.GetValue("length")).GetValue("invalid"); } } else if (null != result && result.GetType() == typeof(String[])) { String[] value = (String[])result; if ("200".Equals(code) && value.Length > max) { code = (String)((JObject)obj.GetValue("length")).GetValue("invalid"); } } } if (obj.GetValue("size") != null) { max = (int)((JObject)obj.GetValue("size")).GetValue("max"); type = (String)((JObject)obj.GetValue("typeof")).GetValue("type"); if (type.Contains("array")) { Object[] value = null; if (null != m) { value = (Object[])m.Invoke(model, new Object[] { }); } else { value = (Object[])propertyInfo.GetValue(model, null); } if ("200".Equals(code) && null == value) { code = (String)((JObject)obj.GetValue("size")).GetValue("invalid"); } if ("200".Equals(code) && value.Length > max) { code = (String)((JObject)obj.GetValue("size")).GetValue("invalid"); } } else if (type.Contains("int")) { int value = 0; try { if (null != m) { value = (int)m.Invoke(model, new Object[] { }); } else { value = (int)propertyInfo.GetValue(model, null); } } catch (Exception e) { code = (String)((JObject)obj.GetValue("typeof")).GetValue("invalid"); } if ("200".Equals(code) && 0 == value) { code = (String)((JObject)obj.GetValue("size")).GetValue("invalid"); } if ("200".Equals(code) && value > max) { code = (String)((JObject)obj.GetValue("size")).GetValue("invalid"); } } } if (!"200".Equals(code)) { //根据错误码获取错误信息 String message = (String)CommonUtil.GetErrorMessage(apiPath, method, code, name, max.ToString(), "1", type); //对 errorMessage 替换 message = message.Replace("errorMessage", "msg"); return(message); } } } } } catch (Exception e) { Console.WriteLine(e.Message); throw e; } return(null); }