123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- 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
- {
- /// <summary>
- /// ApiInfo)Controller
- /// </summary>
- // [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<string, object> obj = new Dictionary<string, object>();
- 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<string, int> groupobj = new Dictionary<string, int>();
- Dictionary<string, object> obj = new Dictionary<string, object>();
- 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<string, object> groupData = new Dictionary<string, object>();
- var apis = _ApiInfoService.GetList(m => m.groupId == group.id).ToList();
- foreach(var api in apis)
- {
- Dictionary<string, object> apiData = new Dictionary<string, object>();
- 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<string, int> groupobj = new Dictionary<string, int>();
- Dictionary<string, object> obj = new Dictionary<string, object>();
- 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<string, object> groupData = new Dictionary<string, object>();
- var apis = _ApiInfoService.GetList(m => m.groupId == group.id).ToList();
- foreach(var api in apis)
- {
- Dictionary<string, object> apiData = new Dictionary<string, object>();
- 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<string, object> Obj = new Dictionary<string, object>();
- Obj.Add("AccessId", ossConfigs.Key);
- Obj.Add("AccessKey", ossConfigs.Secret);
- return Json(new AppResultJson() { Status = "-1", Info = "", Data = Obj });
- }
- #endregion
- }
- }
|