public object Test([FromForm] dynamic form) { IDictionary <string, object> parameters = RequestDataHelper.GetMixParams(); var files = RequestDataHelper.GetAllFiles(); return(null); }
/// <summary> /// 外部调用接口 /// </summary> /// <param name="code">接口编码</param> /// <param name="inputParameters">可传入参数</param> /// <returns></returns> public object DynamicFetch(IDictionary <string, object> config, string code, IDictionary <string, object> inputParameters = null) { string dataBaseKey = config.GetValue <string>("DataBaseKey"); string pluginAssemblyPath = config.GetValue <string>("PluginAssemblyPath"); string pluginClassName = config.GetValue <string>("PluginClassName"); threadLocalDataBaseKey.Value = dataBaseKey; ConfigApiPlugin dynamicApiPlugin = ReflectorHelper.GetPluginInstance <ConfigApiPlugin>(pluginAssemblyPath, pluginClassName); IDictionary <string, object> parameters = RequestDataHelper.GetMixParams(); IDictionary <string, IList <IFormFile> > files = RequestDataHelper.GetAllFiles(); IDictionary <string, object> headers = RequestDataHelper.GetHeaders(); IDictionary <string, object> cookies = RequestDataHelper.GetCookies(); object bodyJson = RequestDataHelper.GetBodyJsonParameters(); if (inputParameters != null && inputParameters.Count > 0) { parameters = RequestDataHelper.MergeDictionary(parameters, inputParameters); } return(this.AopDynamicApi <object>((apiConfig, paramsData, formFiles, reqHeaders, reqCookies, json) => { if (dynamicApiPlugin != null) { IDbHelper dbHelper = this._dbContext.Use(dataBaseKey); //调用接口前扩展处理 dynamicApiPlugin.Before(dbHelper, apiConfig, paramsData, formFiles, json); } }, (apiConfig, paramsData, json) => { string scriptCode = apiConfig.GetValue <string>("ScriptCode"); /* * 1=单一结果(单个值,或者一条sql语句执行结果) * 2=分页 * 3=列表结果集(多个值以List<object>返回 * 4=字典结果集(多个值以Dictionary<string,object>返回 * 5=主从结果集 (会查询嵌套子查询,多个值以Dictionary<string,object>返回 * 6=返回脚本执行结果(直接返回脚本执行结果) */ int codeKind = apiConfig.GetValue <int>("CodeKind"); int apiKind = apiConfig.GetValue <int>("ApiKind"); //0=公共接口;1=对内接口 int status = apiConfig.GetValue <int>("Status"); //0=禁用;1=启用 return this.ExecuteScript(scriptCode, codeKind, paramsData, bodyJson); }, (apiConfig, paramsData, json, result) => { if (dynamicApiPlugin != null) { IDbHelper dbHelper = this._dbContext.Use(dataBaseKey); //调用接口后扩展处理 return dynamicApiPlugin.After(dbHelper, apiConfig, paramsData, json, result); } return result; }, config, parameters, files, headers, cookies, bodyJson)); }
/// <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); } } } }