private void InitSystemParams(IDictionary <string, object> config) { string pluginsAssembly = config.GetValue <string>("SystemPramsPluginAssemblyPath"); string pluginsClassName = config.GetValue <string>("SystemPramsPluginClassName");; ParamsPlugin paramsPlugin = ReflectorHelper.GetPluginInstance <ParamsPlugin>(pluginsAssembly, pluginsClassName); if (paramsPlugin != null) { paramsPlugin.InitParams(); } }
public override void InitParams() { base.InitParams(); //将登录信息缓存入系统变量字典 var context = RequestDataHelper.GetHttpContext(); if (context.User != null) { Claim claim = context.User.FindFirst(ClaimTypes.Sid); if (claim != null) { string userId = claim.Value; ParamsPlugin.Set("UserId", long.Parse(userId == "" ? "0" : userId)); } } }
/// <summary> /// 参数验证 /// </summary> /// <param name="code">接口编码</param> private void ParamsCheck(string code, IDictionary <string, object> config, IDictionary <string, object> inputParameters) { using (Lua lua = new Lua()) { lua.State.Encoding = Encoding.UTF8; //参数整体验证 string checkScript = config["CheckScript"].ToString(); IDictionary <string, object> parameters = RequestDataHelper.GetMixParams(); object bodyJson = RequestDataHelper.GetBodyJsonParameters(); IDictionary <string, IList <IFormFile> > fileDic = RequestDataHelper.GetAllFiles(); if (!string.IsNullOrWhiteSpace(checkScript)) { object[] result = LuaScriptRunner.ExecuteLuaScript(lua, checkScript, parameters, bodyJson);//第一个返回值为验证是否通过(true|false),第二个参数为验证错误信息,为true时没有 if (!(bool)result[0]) { if (result.Length > 1) { throw new CustomException(11, result[1].ToString());//通过自定义异常抛出验证失败信息 } else { throw new CustomException(11, "参数验证失败"); } } } IDictionary <string, object> paramData = new Dictionary <string, object>(parameters); //单个参数验证 IList <IDictionary <string, object> > apiParams = _dal.GetApiParams(code);//配置参数信息 if (apiParams.Count > 0) { foreach (IDictionary <string, object> dic in apiParams) { int paramType = (int)dic["ParamType"]; //0 = String,1 = Integer,2 = Long,3 = Double,4 = Float,5 = Decimal,6 = Boolean,7 = Date,8 = DateTime,9=Ulong,10 = Key/Value,11= List,12 = File string paramCode = dic["ParamCode"].ToString(); string paramName = dic["ParamName"].ToString(); short isRequire = (short)dic["IsRequire"]; short paramsKind = (short)dic["ParamsKind"]; //ParamsKind 0 = 普通参数;1 = 系统参数;2=Id值; string checkRule = dic["CheckRule"].ToString(); //验证使用的正则表达式 string ruleError = dic["RuleError"].ToString(); //正则表达式验证不通过时候的错误提示信息 string paramCheckScript = dic["CheckScript"].ToString(); //验证单个参数的lua脚本 if (paramsKind == 1) { if (isRequire == 1 && !ParamsPlugin.ContainsKey(paramCode)) { throw new CustomException(11, "系统参数" + paramName + "不能为空"); } var sysParamValue = ParamsPlugin.Get(paramCode); if (isRequire == 1 && sysParamValue == null) { throw new CustomException(11, "系统参数" + paramName + "不能为空"); } inputParameters[paramCode] = sysParamValue; paramData[paramCode] = sysParamValue; } else if (paramsKind == 2) { var id = DbHelper.NewLongId(); inputParameters[paramCode] = id; paramData[paramCode] = id; } //检查必录项 if (isRequire == 1) { this.CheckRequire(paramType, paramCode, paramName, paramData, fileDic); } //正则检查 if (!string.IsNullOrWhiteSpace(checkRule)) { this.CheckRegexRule(paramType, paramCode, paramName, checkRule, ruleError, paramData); } //脚本验证 if (!string.IsNullOrWhiteSpace(paramCheckScript)) { this.LuaScriptCheck(lua, paramType, paramCode, paramName, paramCheckScript, paramData); } //转换参数类型 this.ConvertParamsType(paramType, paramCode); } } } }