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 { /// /// 接口信息Service业务层处理 /// [AppService(ServiceType = typeof(IApiInfoService), ServiceLifetime = LifeTime.Transient)] public class ApiInfoService : BaseService, IApiInfoService { /// /// 接口信息-列表 /// /// 参数请求体 /// 分页参数 /// 列表 public PagedInfo getApiInfoList([FromQuery] PagerInfo page, [FromQuery] ApiInfo param) { //拼装查询条件 var predicate = Expressionable.Create(); 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(page); return response; } /// /// 接口信息-字段关联接口列表 /// /// 参数请求体 /// 分页参数 /// 字段关联接口列表 public PagedInfo getApiInfoListByProId([FromQuery] PagerInfo page, [FromQuery] ApiInfo param) { //拼装查询条件 var predicate = Expressionable.Create(); predicate = predicate.AndIF(param.serviceId > 0, m => m.serviceId == param.serviceId); var response = Queryable() .Where(predicate.ToExpression()) .OrderByDescending(m => m.id) .ToPage(page); return response; } /// /// 克隆接口 /// /// 参数请求体 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 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); } } /// /// 初始化接口 /// public void initApiInfo([FromBody] InitApiInfoDto param) { var user = JwtUtil.GetLoginUser(App.HttpContext); var _DatabaseInfoService = App.GetService(); var _DatabaseTableService = App.GetService(); var _DatabaseFieldService = App.GetService(); var _ApiGroupService = App.GetService(); 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(); 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(), 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 ?? "", }); } } } /// /// 获取编辑组件类型 /// /// 字段标题 /// 字段类型 /// 编辑组件类型 public EditTypeFrontend getEditType(string fieldTitle, string fieldType) { List 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 getEditTypes() { List editType = new List(); 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; } /// /// 测试接口 /// public TestApiInfoVo testApiInfo([FromBody] TestApiInfoDto param, List groups) { if (string.IsNullOrEmpty(param.idList)) { return new TestApiInfoVo() { testResult = "没有传入接口ID" }; } Utils.WriteLog("1", "测试接口"); List> testResult = new List>(); List> testResultSuccess = new List>(); List 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 headers = new Dictionary(); //请求头 if(!string.IsNullOrEmpty(param.headers)) { headers = JsonMapper.ToObject>(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(); apiInfoItem.apiPath = gatewayHost + "/v1/" + group.groupName + "/" + apiInfo.controllerNameUpper + "/" + apiInfoItem.methodName; Dictionary apiItem = new Dictionary(); Dictionary reqParams = new Dictionary(); 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>(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) }; } } }