| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- using Attribute;
- using Model;
- using Model.Base;
- using Repository;
- using Service;
- using Microsoft.AspNetCore.Mvc;
- using Vo.Admin;
- using Dto.Admin;
- using Infrastructure;
- using Common;
- using System.Linq;
- using Util;
- using Custom;
- using LitJson;
- using Feign;
- using Util.Logic;
- using Mapster;
- using Newtonsoft.Json.Linq;
- namespace Services
- {
- /// <summary>
- /// 接口信息Service业务层处理
- /// </summary>
- [AppService(ServiceType = typeof(IApiInfoService), ServiceLifetime = LifeTime.Transient)]
- public class ApiInfoService : BaseService<ApiInfo>, IApiInfoService
- {
- /// <summary>
- /// 接口信息-列表
- /// </summary>
- /// <param name="param">参数请求体</param>
- /// <param name="page">分页参数</param>
- /// <returns>列表</returns>
- public PagedInfo<GetApiInfoListVo> getApiInfoList([FromQuery] PagerInfo page, [FromQuery] ApiInfo param)
- {
- //拼装查询条件
- var predicate = Expressionable.Create<ApiInfo>();
- predicate = predicate.AndIF(param.serviceId > 0, m => m.serviceId == param.serviceId);
- predicate = predicate.AndIF(param.groupId > 0, m => m.groupId == param.groupId);
- predicate = predicate.AndIF(!string.IsNullOrEmpty(param.tabName), m => m.tabName.Contains(param.tabName));
- predicate = predicate.AndIF(!string.IsNullOrEmpty(param.apiName), m => (m.apiName.Contains(param.apiName) || m.tabName.Contains(param.apiName)));
- predicate = predicate.AndIF(param.apiKind > 0, m => m.apiKind == param.apiKind);
- var response = Queryable()
- .Where(predicate.ToExpression())
- .OrderByDescending(m => m.tabName)
- .OrderByDescending(m => m.apiName)
- .OrderByDescending(m => m.id)
- .ToPage<ApiInfo, GetApiInfoListVo>(page);
- return response;
- }
- /// <summary>
- /// 接口信息-字段关联接口列表
- /// </summary>
- /// <param name="param">参数请求体</param>
- /// <param name="page">分页参数</param>
- /// <returns>字段关联接口列表</returns>
- public PagedInfo<GetApiInfoListByProIdVo> getApiInfoListByProId([FromQuery] PagerInfo page, [FromQuery] ApiInfo param)
- {
- //拼装查询条件
- var predicate = Expressionable.Create<ApiInfo>();
- predicate = predicate.AndIF(param.serviceId > 0, m => m.serviceId == param.serviceId);
- var response = Queryable()
- .Where(predicate.ToExpression())
- .OrderByDescending(m => m.id)
- .ToPage<ApiInfo, GetApiInfoListByProIdVo>(page);
- return response;
- }
- /// <summary>
- /// 克隆接口
- /// </summary>
- /// <param name="param">参数请求体</param>
- public void copyApiInfo([FromBody] CopyApiInfoDto param, ApiGroup group)
- {
- // var group = _ApiGroupService.GetFirst(m => m.id == param.targetGroupId) ?? new ApiGroup();
- var user = JwtUtil.GetLoginUser(App.HttpContext);
- List<int> apiIdList = Tools.SpitIntArrary(param.sourceApiId).ToList();
- var sourceApis = GetList(m => apiIdList.Contains(m.id)).ToList();
- foreach(ApiInfo sourceApi in sourceApis)
- {
- ApiInfo newApi = new ApiInfo();
- newApi.serviceId = group.serviceId;
- newApi.groupId = param.targetApiGroupId;
- newApi.tabName = sourceApi.tabName;
- newApi.apiName = sourceApi.apiName;
- newApi.controllerFilePath = sourceApi.controllerFilePath;
- newApi.usageScenarios = sourceApi.usageScenarios;
- newApi.apiKind = sourceApi.apiKind;
- newApi.methodName = sourceApi.methodName;
- newApi.apiDetail = sourceApi.apiDetail;
- newApi.apiParam = sourceApi.apiParam;
- newApi.createTime = DateTime.Now;
- newApi.createBy = user.username ?? "";
- newApi.linkTableId = sourceApi.linkTableId;
- newApi.pageFlag = sourceApi.pageFlag;
- Add(newApi);
- }
- }
- /// <summary>
- /// 初始化接口
- /// </summary>
- public void initApiInfo([FromBody] InitApiInfoDto param)
- {
- var user = JwtUtil.GetLoginUser(App.HttpContext);
- var _DatabaseInfoService = App.GetService<IDatabaseInfoService>();
- var _DatabaseTableService = App.GetService<IDatabaseTableService>();
- var _DatabaseFieldService = App.GetService<IDatabaseFieldService>();
- var _ApiGroupService = App.GetService<IApiGroupService>();
- var apiGroup = _ApiGroupService.GetFirst(m => m.id == param.groupId) ?? new ApiGroup();
- foreach(int tableId in param.sourceTableId)
- {
- var table = _DatabaseTableService.GetById(tableId) ?? new DatabaseTable();
- var database = _DatabaseInfoService.GetFirst(m => m.id == table.databaseId) ?? new DatabaseInfo();
- var fields = _DatabaseFieldService.GetList(m => m.tableId == table.id);
- for (int apiKind = 1; apiKind <= 5; apiKind++)
- {
- var apiParam = new List<ApiParamObject>();
- foreach(var field in fields)
- {
- bool reqParam = false;
- bool resParam = false;
- if((field.showQuery && apiKind == 1) || (field.showEdit && (apiKind == 3 || apiKind == 4)))
- {
- reqParam = true;
- }
- if((apiKind == 2 || apiKind == 4 || apiKind == 5) && field.primaryKey)
- {
- reqParam = true;
- }
- if(field.showList && (apiKind == 1 || apiKind == 2))
- {
- resParam = true;
- }
- apiParam.Add(new ApiParamObject()
- {
- id = field.id,
- fieldTitle = field.fieldTitle ?? "",
- fieldName = field.fieldName ?? "",
- fieldType = field.fieldType ?? "",
- reqParam = reqParam,
- resParam = resParam,
- resType = "字段值",
- checkAttribute = !string.IsNullOrEmpty(field.validateAttribute) ? field.validateAttribute.Split(',') : Array.Empty<string>(),
- fieldDetail = field.fieldDetail ?? "",
- });
- }
- Add(new ApiInfo()
- {
- serviceId = apiGroup.serviceId,
- groupId = param.groupId,
- tabName = "基本信息",
- apiName = table.tableTitle + (apiKind == 1 ? "列表" : apiKind == 2 ? "详情" : apiKind == 3 ? "添加" : apiKind == 4 ? "修改" : "删除"),
- controllerFilePath = table.tableNameUpper,
- apiKind = apiKind,
- methodName = apiKind == 1 ? "get" + table.tableNameFirstUpper + "List" :
- apiKind == 2 ? "get" + table.tableNameFirstUpper + "Query" :
- apiKind == 3 ? "add" + table.tableNameFirstUpper :
- apiKind == 4 ? "update" + table.tableNameFirstUpper :
- "delete" + table.tableNameFirstUpper,
- apiDetail = apiKind == 1 ? "根据条件查询" + table.tableTitle + "列表数据" :
- apiKind == 2 ? "根据Id查询" + table.tableTitle + "某一条详情数据" :
- apiKind == 3 ? "添加" + table.tableTitle + "数据" :
- apiKind == 4 ? "修改" + table.tableTitle + "数据" :
- "根据Id删除" + table.tableTitle + "数据",
- apiParam = apiParam,
- createTime = DateTime.Now,
- linkTableId = tableId,
- pageFlag = apiKind == 1,
- createBy = user.username ?? "",
- });
- }
- }
- }
- /// <summary>
- /// 获取编辑组件类型
- /// </summary>
- /// <param name="fieldTitle">字段标题</param>
- /// <param name="fieldType">字段类型</param>
- /// <returns>编辑组件类型</returns>
- public EditTypeFrontend getEditType(string fieldTitle, string fieldType)
- {
- List<EditTypeFrontend> editType = getEditTypes();
- try
- {
- if (fieldTitle.Contains("密码"))
- {
- return editType.FirstOrDefault(m => m.name == "password") ?? new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- else if (fieldTitle.Contains("验证码"))
- {
- return editType.FirstOrDefault(m => m.name == "phonecode") ?? new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- else if (fieldTitle.Contains("图片") || fieldTitle.Contains("头像") || fieldTitle.Contains("照片"))
- {
- return editType.FirstOrDefault(m => m.name == "image") ?? new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- else if (fieldTitle.Contains("文件") || fieldTitle.Contains("附件"))
- {
- return editType.FirstOrDefault(m => m.name == "file") ?? new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- else if (fieldTitle.Contains("内容") || fieldTitle.Contains("详情") || fieldTitle.Contains("描述"))
- {
- return editType.FirstOrDefault(m => m.name == "editor") ?? new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- else if (fieldTitle.Contains("类型") || fieldTitle.Contains("分类") || fieldTitle.Contains("状态"))
- {
- return editType.FirstOrDefault(m => m.name == "select") ?? new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- else if (fieldTitle.Contains("日期") || fieldTitle.Contains("时间"))
- {
- return editType.FirstOrDefault(m => m.name == "datePicker") ?? new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- else if (fieldTitle.Contains("开关") || fieldTitle.Contains("是否") || fieldTitle.Contains("启用") || fieldTitle.Contains("禁用"))
- {
- return editType.FirstOrDefault(m => m.name == "switch") ?? new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- if (fieldType == "varchar" || fieldType == "nvarchar" || fieldType == "text")
- {
- return editType.FirstOrDefault(m => m.name == "input") ?? new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- else if (fieldType == "int" || fieldType == "bigint" || fieldType == "decimal" || fieldType == "float")
- {
- return editType.FirstOrDefault(m => m.name == "number") ?? new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- else if (fieldType == "datetime" || fieldType == "date")
- {
- return editType.FirstOrDefault(m => m.name == "datePicker") ?? new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- else if (fieldType == "bit")
- {
- return editType.FirstOrDefault(m => m.name == "switch") ?? new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- else if (fieldType == "json")
- {
- return editType.FirstOrDefault(m => m.name == "json") ?? new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- }
- catch (Exception ex)
- {
- Utils.WriteLog(ex.ToString());
- return new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
- }
- return new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" };
-
- }
- public List<EditTypeFrontend> getEditTypes()
- {
- List<EditTypeFrontend> editType = new List<EditTypeFrontend>();
- editType.Add(new EditTypeFrontend() { name = "input", tag = "ElInput", title = "输入框" });
- editType.Add(new EditTypeFrontend() { name = "password", tag = "ElInput", title = "密码输入框" });
- editType.Add(new EditTypeFrontend() { name = "phonecode", tag = "PhoneCode", title = "手机验证码组件" });
- editType.Add(new EditTypeFrontend() { name = "select", tag = "ElSelect", title = "选择器" });
- editType.Add(new EditTypeFrontend() { name = "picker", tag = "ElDatePicker", title = "日期选择器" });
- editType.Add(new EditTypeFrontend() { name = "datePicker", tag = "ElDatePicker", title = "日期选择器" });
- editType.Add(new EditTypeFrontend() { name = "timePicker", tag = "ElTimePicker", title = "时间选择器" });
- editType.Add(new EditTypeFrontend() { name = "number", tag = "ElInputNumber", title = "数字输入框" });
- editType.Add(new EditTypeFrontend() { name = "cascader", tag = "ElCascader", title = "级联选择器" });
- editType.Add(new EditTypeFrontend() { name = "Json", tag = "JsonEditor", title = "JSON" });
- editType.Add(new EditTypeFrontend() { name = "editor", tag = "TextEditor", title = "富文本编辑器" });
- editType.Add(new EditTypeFrontend() { name = "image", tag = "OmegaUpload", title = "图片上传" });
- editType.Add(new EditTypeFrontend() { name = "file", tag = "OmegaUpload", title = "文件上传" });
- editType.Add(new EditTypeFrontend() { name = "switch", tag = "ElSwitch", title = "开关" });
- editType.Add(new EditTypeFrontend() { name = "transfer", tag = "ElTransfer", title = "穿梭框" });
- editType.Add(new EditTypeFrontend() { name = "autocomplete", tag = "ElAutocomplete", title = "自动完成" });
- editType.Add(new EditTypeFrontend() { name = "treeSelect", tag = "ElTreeSelect", title = "树形选择器" });
- editType.Add(new EditTypeFrontend() { name = "tree", tag = "ElTree", title = "树形控件" });
- editType.Add(new EditTypeFrontend() { name = "radio", tag = "ElRadio", title = "单选框" });
- editType.Add(new EditTypeFrontend() { name = "radioGroup", tag = "ElRadioGroup", title = "单选框组" });
- editType.Add(new EditTypeFrontend() { name = "checkbox", tag = "ElCheckbox", title = "复选框" });
- editType.Add(new EditTypeFrontend() { name = "checkboxGroup", tag = "ElCheckboxGroup", title = "复选框组" });
- editType.Add(new EditTypeFrontend() { name = "slider", tag = "ElSlider", title = "滑块" });
- editType.Add(new EditTypeFrontend() { name = "rate", tag = "ElRate", title = "评分" });
- editType.Add(new EditTypeFrontend() { name = "colorPicker", tag = "ElColorPicker", title = "颜色选择器" });
- return editType;
- }
- /// <summary>
- /// 测试接口
- /// </summary>
- public TestApiInfoVo testApiInfo([FromBody] TestApiInfoDto param, List<ApiGroup> groups)
- {
- if (string.IsNullOrEmpty(param.idList))
- {
- return new TestApiInfoVo()
- {
- testResult = "没有传入接口ID"
- };
- }
- Utils.WriteLog("1", "测试接口");
- List<Dictionary<string, object>> testResult = new List<Dictionary<string, object>>();
- List<Dictionary<string, object>> testResultSuccess = new List<Dictionary<string, object>>();
- List<int> ids = Tools.SpitIntArrary(param.idList).ToList();
- var apiList = GetList(m => ids.Contains(m.id));
- var service = IProject.getProjectService(apiList.FirstOrDefault().serviceId);
- Utils.WriteLog(JsonMapper.ToJson(service), "测试接口");
- var project = IProject.getProject(service.projectId);
- Utils.WriteLog(JsonMapper.ToJson(project), "测试接口");
- string encryptKind = project.encryptKind ?? "";
- string encryptParam = project.encryptParam ?? "{}";
- JsonData jsonEncryptParam = JsonMapper.ToObject(encryptParam);
- string gatewayHost = project.gatewayHostDev ?? "";
- if(gatewayHost.Contains(":"))
- {
- var gatewayHostArray = gatewayHost.Split(":");
- gatewayHost = gatewayHostArray[0];
- string port = gatewayHostArray[1];
- if (port == "443")
- {
- gatewayHost = "https://" + gatewayHost;
- }
- else if (port == "80")
- {
- gatewayHost = "http://" + gatewayHost;
- }
- else
- {
- gatewayHost = "http://" + gatewayHost + ":" + port;
- }
- }
- else
- {
- gatewayHost = "http://" + gatewayHost;
- }
- Utils.WriteLog(gatewayHost, "测试接口");
- Dictionary<string, string> headers = new Dictionary<string, string>(); //请求头
- if(!string.IsNullOrEmpty(param.headers))
- {
- headers = JsonMapper.ToObject<Dictionary<string, string>>(param.headers);
- }
- Utils.WriteLog(JsonMapper.ToJson(headers), "测试接口");
- foreach (var apiInfo in apiList)
- {
- Utils.WriteLog(JsonMapper.ToJson(apiInfo), "测试接口");
- var group = groups.FirstOrDefault(m => m.id == apiInfo.groupId) ?? new ApiGroup();
- var apiInfoItem = apiInfo.Adapt<ApiTreeInfo>();
- apiInfoItem.apiPath = gatewayHost + "/v1/" + group.groupName + "/" + apiInfo.controllerNameUpper + "/" + apiInfoItem.methodName;
- Dictionary<string, object> apiItem = new Dictionary<string, object>();
- Dictionary<string, string> reqParams = new Dictionary<string, string>();
- if(string.IsNullOrEmpty(param.reqData))
- {
- if(apiInfo.apiParam != null)
- {
- reqParams = apiInfo.apiParam.Where(m => m.reqParam).ToDictionary(m => m.fieldName, m => TestDataGenerator.GenerateValue(m.fieldType));
- }
- }
- else
- {
- reqParams = JsonMapper.ToObject<Dictionary<string, string>>(param.reqData);
- }
- string response = ApiTestHelper.apiTestRequest(apiInfoItem, encryptKind, encryptParam, reqParams, headers);
- string reqParamEncrypt = "";
- string statusCode = "未知";
- if(response.Contains("|"))
- {
- reqParamEncrypt = response.Split("|")[0];
- response = response.Split("|")[1];
- if(response.Split("|").Length > 2)
- {
- statusCode = response.Split("|")[2];
- }
- }
- apiItem.Add("接口名称", apiInfoItem.tabName + "-" + apiInfoItem.apiName);
- apiItem.Add("请求头", headers);
- apiItem.Add("请求参数", reqParams);
- apiItem.Add("请求参数加密", reqParamEncrypt);
- apiItem.Add("返回状态码", statusCode);
- apiItem.Add("返回报文", response);
- apiItem.Add("请求地址", apiInfoItem.apiPath);
- if(statusCode == "200")
- {
- testResultSuccess.Add(apiItem);
- }
- else
- {
- testResult.Add(apiItem);
- }
- }
- if(testResultSuccess.Count > 0)
- {
- testResult = testResultSuccess.Concat(testResult).ToList();
- }
- return new TestApiInfoVo()
- {
- testResult = JsonMapper.ToJson(testResult)
- };
- }
- }
- }
|