using Attribute; using Common; using Dto; using Vo; using Enums; using Filters; using Infrastructure; using Infrastructure.Model; using Mapster; using Microsoft.AspNetCore.Mvc; using Middleware; using Model; using Services; using Model.Base; using Microsoft.AspNetCore.Authorization; using Model.Customer; using Base; using LitJson; using System; namespace Controllers { /// /// ApiInfo)Controller /// // [Route("ApiInfo")] // [ApiExplorerSettings(GroupName = "ApiInfo")] public class ApiPubController : BaseController { private readonly IApiInfoService _ApiInfoService; private readonly IApiGroupService _ApiGroupService; private readonly ISysMenuService _SysMenuService; public ApiPubController(IApiInfoService ApiInfoService, IApiGroupService ApiGroupService, ISysMenuService SysMenuService) { _ApiInfoService = ApiInfoService; _ApiGroupService = ApiGroupService; _SysMenuService = SysMenuService; } #region 通用-接收接口数据通知 [HttpPost] [Route("/noauth/api/notice")] public string notice(string value) { JsonData data = JsonMapper.ToObject(value); Dictionary obj = new Dictionary(); for (int i = 0; i < data.Count; i++) { string id = data[i]["id"].ToString(); string groupName = data[i]["group_name"].ToString(); string groupRemark = data[i]["group_remark"].ToString(); int groupKind = int.Parse(data[i]["group_kind"].ToString()); int groupId = 0; ApiGroup group = _ApiGroupService.GetFirst(m => m.groupName == groupName && m.groupKind == groupKind); if(group == null) { groupId = _ApiGroupService.Add(new ApiGroup() { createDate = DateTime.Now, groupName = groupName, groupRemark = groupRemark, groupKind = groupKind, groupVersion = "1", }); } else { groupId = group.id; group.updateDate = DateTime.Now; group.groupRemark = groupRemark; int groupVersion = int.Parse(group.groupVersion) + 1; group.groupVersion = groupVersion.ToString(); _ApiGroupService.Update(group, true); } JsonData apiList = data[i]["child"]; for (int j = 0; j < apiList.Count; j++) { string ApiRouter = apiList[j]["api_router"].ToString(); string ApiPort = apiList[j]["api_port"].ToString(); string ApiHost = apiList[j]["api_host"].ToString(); string ApiKey = apiList[j]["api_key"].ToString(); string ApiName = apiList[j]["api_name"].ToString(); string ApiMethod = apiList[j]["api_method"].ToString(); ApiInfo apiInfo = _ApiInfoService.GetFirst(m => m.groupId == groupId && m.apiKey == ApiKey); if(apiInfo == null) { _ApiInfoService.Add(new ApiInfo() { createDate = DateTime.Now, apiKey = ApiKey, apiRouter = ApiRouter, apiPort = int.Parse(Function.CheckInt(ApiPort)), apiHost = ApiHost, apiName = ApiName, apiMethod = ApiMethod, groupId = groupId, }); if(groupKind == 1) { string ApiPath = apiList[j]["api_path"].ToString(); string ApiEnName = apiList[j]["api_en_name"].ToString(); string rootPath = ApiPath.Split('/')[1]; SysMenu rootMenu = _SysMenuService.GetFirst(m => m.enName == rootPath) ?? new SysMenu(); if(!_SysMenuService.Any(m => m.enName == ApiEnName)) { int menuId = _SysMenuService.InsertReturnIdentity(new SysMenu() { name = ApiName.Split('-')[0], enName = ApiEnName, path = ApiPath, parentId = rootMenu.menuId, visible = "1", keepAlive = "1", menuType = "0", CreateBy = "admin", CreateTime = DateTime.Now, UpdateBy = "admin", UpdateTime = DateTime.Now, }); _SysMenuService.InsertReturnIdentity(new SysMenu() { name = "新增", permission = "add", parentId = menuId, visible = "1", keepAlive = "1", menuType = "1", CreateBy = "admin", CreateTime = DateTime.Now, UpdateBy = "admin", UpdateTime = DateTime.Now, }); _SysMenuService.InsertReturnIdentity(new SysMenu() { name = "修改", permission = string.Concat("update", ApiEnName[..1].ToUpdate(), ApiEnName.AsSpan(1)), parentId = menuId, visible = "1", keepAlive = "1", menuType = "1", CreateBy = "admin", CreateTime = DateTime.Now, UpdateBy = "admin", UpdateTime = DateTime.Now, }); _SysMenuService.InsertReturnIdentity(new SysMenu() { name = "删除", permission = "delete", parentId = menuId, visible = "1", keepAlive = "1", menuType = "1", CreateBy = "admin", CreateTime = DateTime.Now, UpdateBy = "admin", UpdateTime = DateTime.Now, }); } } } else { apiInfo.apiRouter = ApiRouter; apiInfo.apiPort = int.Parse(Function.CheckInt(ApiPort)); apiInfo.apiHost = ApiHost; apiInfo.apiName = ApiName; apiInfo.apiMethod = ApiMethod; _ApiInfoService.Update(apiInfo, true); } } } return "success"; } #endregion #region 通用-获取接口列表 [Route("/noauth/api/list")] public JsonResult list(string value) { if(string.IsNullOrEmpty(value)) { return Json(new AppResultJson() { Status = "-1", Info = "" }); } value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); Dictionary groupobj = new Dictionary(); Dictionary obj = new Dictionary(); if(value.Contains("\"groups\"")) { JsonData groupJson = data["groups"]; for(int i = 0; i < groupJson.Count; i ++) { string name = groupJson[i]["name"].ToString(); int version = int.Parse(groupJson[i]["version"].ToString()); groupobj.Add(name, version); } } var groups = _ApiGroupService.GetList(m => m.groupKind == 0); foreach(var group in groups) { int curVersion = int.Parse(Function.CheckInt(group.groupVersion)); if(groupobj.Any(m => m.Key == group.groupName && m.Value < curVersion)) { Dictionary groupData = new Dictionary(); var apis = _ApiInfoService.GetList(m => m.groupId == group.id).ToList(); foreach(var api in apis) { Dictionary apiData = new Dictionary(); string url = api.apiHost; if(api.apiPort != 80 && api.apiPort != 443) { url += ":" + api.apiPort; } url += api.apiRouter; apiData.Add("name", api.apiName); apiData.Add("url", url); apiData.Add("method", api.apiMethod); groupData.Add(api.apiKey, apiData); } groupData.Add("groupVersion", group.groupVersion); groupData.Add("groupInfo", group.groupRemark); obj.Add(group.groupName, groupData); } } return Json(new AppResultJson() { Status = "1", Info = "", Data = obj }); } #endregion #region 通用-获取接口列表 [Route("/noauth/api/groups")] public JsonResult groups(string value) { if(string.IsNullOrEmpty(value)) { return Json(new AppResultJson() { Status = "-1", Info = "" }); } value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); string key = data["key"].ToString(); if(key != "llb#2024") { return Json(new AppResultJson() { Status = "-1", Info = "无权限" }); } var groups = _ApiGroupService.GetList(m => m.groupKind == 0).Select(m => m.groupName).ToList(); return Json(new AppResultJson() { Status = "1", Info = "", Data = groups }); } #endregion #region 通用-获取接口列表(后台) [Route("/noauth/api/listforadmin")] public JsonResult listForAdmin(string value) { if(string.IsNullOrEmpty(value)) { return Json(new AppResultJson() { Status = "-1", Info = "" }); } value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); Dictionary groupobj = new Dictionary(); Dictionary obj = new Dictionary(); if(value.Contains("\"groups\"")) { JsonData groupJson = data["groups"]; for(int i = 0; i < groupJson.Count; i ++) { string name = groupJson[i]["name"].ToString(); int version = int.Parse(groupJson[i]["version"].ToString()); groupobj.Add(name, version); } } var groups = _ApiGroupService.GetList(m => m.groupKind == 1); foreach(var group in groups) { int curVersion = int.Parse(Function.CheckInt(group.groupVersion)); if(groupobj.Any(m => m.Key == group.groupName && m.Value < curVersion)) { Dictionary groupData = new Dictionary(); var apis = _ApiInfoService.GetList(m => m.groupId == group.id).ToList(); foreach(var api in apis) { Dictionary apiData = new Dictionary(); string url = api.apiHost; if(api.apiPort != 80 && api.apiPort != 443) { url += ":" + api.apiPort; } url += api.apiRouter; apiData.Add("name", api.apiName); apiData.Add("url", url); apiData.Add("method", api.apiMethod); groupData.Add(api.apiKey, apiData); } groupData.Add("groupVersion", group.groupVersion); groupData.Add("groupInfo", group.groupRemark); obj.Add(group.groupName, groupData); } } return Json(new AppResultJson() { Status = "1", Info = "", Data = obj }); } #endregion #region 通用-获取接口列表(后台) [Route("/noauth/api/groupsforadmin")] public JsonResult groupsForAdmin(string value) { if(string.IsNullOrEmpty(value)) { return Json(new AppResultJson() { Status = "-1", Info = "" }); } value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); string key = data["key"].ToString(); if(key != "llb#2024") { return Json(new AppResultJson() { Status = "-1", Info = "无权限" }); } var groups = _ApiGroupService.GetList(m => m.groupKind == 1).Select(m => m.groupName).ToList(); return Json(new AppResultJson() { Status = "1", Info = "", Data = groups }); } #endregion #region 获取OSS参数 [Route("/noauth/api/ossparam")] public JsonResult ossParam(string value) { OssConfigs ossConfigs = new(); AppSettings.Bind("OssConfigs", ossConfigs); Dictionary Obj = new Dictionary(); Obj.Add("AccessId", ossConfigs.Key); Obj.Add("AccessKey", ossConfigs.Secret); return Json(new AppResultJson() { Status = "-1", Info = "", Data = Obj }); } #endregion } }