ApiPubController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using Attribute;
  2. using Common;
  3. using Dto;
  4. using Vo;
  5. using Enums;
  6. using Filters;
  7. using Infrastructure;
  8. using Infrastructure.Model;
  9. using Mapster;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Middleware;
  12. using Model;
  13. using Services;
  14. using Model.Base;
  15. using Microsoft.AspNetCore.Authorization;
  16. using Model.Customer;
  17. using Base;
  18. using LitJson;
  19. using System;
  20. namespace Controllers
  21. {
  22. /// <summary>
  23. /// ApiInfo)Controller
  24. /// </summary>
  25. // [Route("ApiInfo")]
  26. // [ApiExplorerSettings(GroupName = "ApiInfo")]
  27. public class ApiPubController : BaseController
  28. {
  29. private readonly IApiInfoService _ApiInfoService;
  30. private readonly IApiGroupService _ApiGroupService;
  31. private readonly ISysMenuService _SysMenuService;
  32. public ApiPubController(IApiInfoService ApiInfoService, IApiGroupService ApiGroupService, ISysMenuService SysMenuService)
  33. {
  34. _ApiInfoService = ApiInfoService;
  35. _ApiGroupService = ApiGroupService;
  36. _SysMenuService = SysMenuService;
  37. }
  38. #region 通用-接收接口数据通知
  39. [HttpPost]
  40. [Route("/noauth/api/notice")]
  41. public string notice(string value)
  42. {
  43. JsonData data = JsonMapper.ToObject(value);
  44. Dictionary<string, object> obj = new Dictionary<string, object>();
  45. for (int i = 0; i < data.Count; i++)
  46. {
  47. string id = data[i]["id"].ToString();
  48. string groupName = data[i]["group_name"].ToString();
  49. string groupRemark = data[i]["group_remark"].ToString();
  50. int groupKind = int.Parse(data[i]["group_kind"].ToString());
  51. int groupId = 0;
  52. ApiGroup group = _ApiGroupService.GetFirst(m => m.groupName == groupName && m.groupKind == groupKind);
  53. if(group == null)
  54. {
  55. groupId = _ApiGroupService.Add(new ApiGroup()
  56. {
  57. createDate = DateTime.Now,
  58. groupName = groupName,
  59. groupRemark = groupRemark,
  60. groupKind = groupKind,
  61. groupVersion = "1",
  62. });
  63. }
  64. else
  65. {
  66. groupId = group.id;
  67. group.updateDate = DateTime.Now;
  68. group.groupRemark = groupRemark;
  69. int groupVersion = int.Parse(group.groupVersion) + 1;
  70. group.groupVersion = groupVersion.ToString();
  71. _ApiGroupService.Update(group, true);
  72. }
  73. JsonData apiList = data[i]["child"];
  74. for (int j = 0; j < apiList.Count; j++)
  75. {
  76. string ApiRouter = apiList[j]["api_router"].ToString();
  77. string ApiPort = apiList[j]["api_port"].ToString();
  78. string ApiHost = apiList[j]["api_host"].ToString();
  79. string ApiKey = apiList[j]["api_key"].ToString();
  80. string ApiName = apiList[j]["api_name"].ToString();
  81. string ApiMethod = apiList[j]["api_method"].ToString();
  82. ApiInfo apiInfo = _ApiInfoService.GetFirst(m => m.groupId == groupId && m.apiKey == ApiKey);
  83. if(apiInfo == null)
  84. {
  85. _ApiInfoService.Add(new ApiInfo()
  86. {
  87. createDate = DateTime.Now,
  88. apiKey = ApiKey,
  89. apiRouter = ApiRouter,
  90. apiPort = int.Parse(Function.CheckInt(ApiPort)),
  91. apiHost = ApiHost,
  92. apiName = ApiName,
  93. apiMethod = ApiMethod,
  94. groupId = groupId,
  95. });
  96. if(groupKind == 1)
  97. {
  98. string ApiPath = apiList[j]["api_path"].ToString();
  99. string ApiEnName = apiList[j]["api_en_name"].ToString();
  100. string rootPath = ApiPath.Split('/')[1];
  101. SysMenu rootMenu = _SysMenuService.GetFirst(m => m.enName == rootPath) ?? new SysMenu();
  102. if(!_SysMenuService.Any(m => m.enName == ApiEnName))
  103. {
  104. int menuId = _SysMenuService.InsertReturnIdentity(new SysMenu()
  105. {
  106. name = ApiName.Split('-')[0],
  107. enName = ApiEnName,
  108. path = ApiPath,
  109. parentId = rootMenu.menuId,
  110. visible = "1",
  111. keepAlive = "1",
  112. menuType = "0",
  113. CreateBy = "admin",
  114. CreateTime = DateTime.Now,
  115. UpdateBy = "admin",
  116. UpdateTime = DateTime.Now,
  117. });
  118. _SysMenuService.InsertReturnIdentity(new SysMenu()
  119. {
  120. name = "新增",
  121. permission = "add",
  122. parentId = menuId,
  123. visible = "1",
  124. keepAlive = "1",
  125. menuType = "1",
  126. CreateBy = "admin",
  127. CreateTime = DateTime.Now,
  128. UpdateBy = "admin",
  129. UpdateTime = DateTime.Now,
  130. });
  131. _SysMenuService.InsertReturnIdentity(new SysMenu()
  132. {
  133. name = "修改",
  134. permission = string.Concat("update", ApiEnName[..1].ToUpdate(), ApiEnName.AsSpan(1)),
  135. parentId = menuId,
  136. visible = "1",
  137. keepAlive = "1",
  138. menuType = "1",
  139. CreateBy = "admin",
  140. CreateTime = DateTime.Now,
  141. UpdateBy = "admin",
  142. UpdateTime = DateTime.Now,
  143. });
  144. _SysMenuService.InsertReturnIdentity(new SysMenu()
  145. {
  146. name = "删除",
  147. permission = "delete",
  148. parentId = menuId,
  149. visible = "1",
  150. keepAlive = "1",
  151. menuType = "1",
  152. CreateBy = "admin",
  153. CreateTime = DateTime.Now,
  154. UpdateBy = "admin",
  155. UpdateTime = DateTime.Now,
  156. });
  157. }
  158. }
  159. }
  160. else
  161. {
  162. apiInfo.apiRouter = ApiRouter;
  163. apiInfo.apiPort = int.Parse(Function.CheckInt(ApiPort));
  164. apiInfo.apiHost = ApiHost;
  165. apiInfo.apiName = ApiName;
  166. apiInfo.apiMethod = ApiMethod;
  167. _ApiInfoService.Update(apiInfo, true);
  168. }
  169. }
  170. }
  171. return "success";
  172. }
  173. #endregion
  174. #region 通用-获取接口列表
  175. [Route("/noauth/api/list")]
  176. public JsonResult list(string value)
  177. {
  178. if(string.IsNullOrEmpty(value))
  179. {
  180. return Json(new AppResultJson() { Status = "-1", Info = "" });
  181. }
  182. value = DesDecrypt(value);
  183. JsonData data = JsonMapper.ToObject(value);
  184. Dictionary<string, int> groupobj = new Dictionary<string, int>();
  185. Dictionary<string, object> obj = new Dictionary<string, object>();
  186. if(value.Contains("\"groups\""))
  187. {
  188. JsonData groupJson = data["groups"];
  189. for(int i = 0; i < groupJson.Count; i ++)
  190. {
  191. string name = groupJson[i]["name"].ToString();
  192. int version = int.Parse(groupJson[i]["version"].ToString());
  193. groupobj.Add(name, version);
  194. }
  195. }
  196. var groups = _ApiGroupService.GetList(m => m.groupKind == 0);
  197. foreach(var group in groups)
  198. {
  199. int curVersion = int.Parse(Function.CheckInt(group.groupVersion));
  200. if(groupobj.Any(m => m.Key == group.groupName && m.Value < curVersion))
  201. {
  202. Dictionary<string, object> groupData = new Dictionary<string, object>();
  203. var apis = _ApiInfoService.GetList(m => m.groupId == group.id).ToList();
  204. foreach(var api in apis)
  205. {
  206. Dictionary<string, object> apiData = new Dictionary<string, object>();
  207. string url = api.apiHost;
  208. if(api.apiPort != 80 && api.apiPort != 443)
  209. {
  210. url += ":" + api.apiPort;
  211. }
  212. url += api.apiRouter;
  213. apiData.Add("name", api.apiName);
  214. apiData.Add("url", url);
  215. apiData.Add("method", api.apiMethod);
  216. groupData.Add(api.apiKey, apiData);
  217. }
  218. groupData.Add("groupVersion", group.groupVersion);
  219. groupData.Add("groupInfo", group.groupRemark);
  220. obj.Add(group.groupName, groupData);
  221. }
  222. }
  223. return Json(new AppResultJson() { Status = "1", Info = "", Data = obj });
  224. }
  225. #endregion
  226. #region 通用-获取接口列表
  227. [Route("/noauth/api/groups")]
  228. public JsonResult groups(string value)
  229. {
  230. if(string.IsNullOrEmpty(value))
  231. {
  232. return Json(new AppResultJson() { Status = "-1", Info = "" });
  233. }
  234. value = DesDecrypt(value);
  235. JsonData data = JsonMapper.ToObject(value);
  236. string key = data["key"].ToString();
  237. if(key != "llb#2024")
  238. {
  239. return Json(new AppResultJson() { Status = "-1", Info = "无权限" });
  240. }
  241. var groups = _ApiGroupService.GetList(m => m.groupKind == 0).Select(m => m.groupName).ToList();
  242. return Json(new AppResultJson() { Status = "1", Info = "", Data = groups });
  243. }
  244. #endregion
  245. #region 通用-获取接口列表(后台)
  246. [Route("/noauth/api/listforadmin")]
  247. public JsonResult listForAdmin(string value)
  248. {
  249. if(string.IsNullOrEmpty(value))
  250. {
  251. return Json(new AppResultJson() { Status = "-1", Info = "" });
  252. }
  253. value = DesDecrypt(value);
  254. JsonData data = JsonMapper.ToObject(value);
  255. Dictionary<string, int> groupobj = new Dictionary<string, int>();
  256. Dictionary<string, object> obj = new Dictionary<string, object>();
  257. if(value.Contains("\"groups\""))
  258. {
  259. JsonData groupJson = data["groups"];
  260. for(int i = 0; i < groupJson.Count; i ++)
  261. {
  262. string name = groupJson[i]["name"].ToString();
  263. int version = int.Parse(groupJson[i]["version"].ToString());
  264. groupobj.Add(name, version);
  265. }
  266. }
  267. var groups = _ApiGroupService.GetList(m => m.groupKind == 1);
  268. foreach(var group in groups)
  269. {
  270. int curVersion = int.Parse(Function.CheckInt(group.groupVersion));
  271. if(groupobj.Any(m => m.Key == group.groupName && m.Value < curVersion))
  272. {
  273. Dictionary<string, object> groupData = new Dictionary<string, object>();
  274. var apis = _ApiInfoService.GetList(m => m.groupId == group.id).ToList();
  275. foreach(var api in apis)
  276. {
  277. Dictionary<string, object> apiData = new Dictionary<string, object>();
  278. string url = api.apiHost;
  279. if(api.apiPort != 80 && api.apiPort != 443)
  280. {
  281. url += ":" + api.apiPort;
  282. }
  283. url += api.apiRouter;
  284. apiData.Add("name", api.apiName);
  285. apiData.Add("url", url);
  286. apiData.Add("method", api.apiMethod);
  287. groupData.Add(api.apiKey, apiData);
  288. }
  289. groupData.Add("groupVersion", group.groupVersion);
  290. groupData.Add("groupInfo", group.groupRemark);
  291. obj.Add(group.groupName, groupData);
  292. }
  293. }
  294. return Json(new AppResultJson() { Status = "1", Info = "", Data = obj });
  295. }
  296. #endregion
  297. #region 通用-获取接口列表(后台)
  298. [Route("/noauth/api/groupsforadmin")]
  299. public JsonResult groupsForAdmin(string value)
  300. {
  301. if(string.IsNullOrEmpty(value))
  302. {
  303. return Json(new AppResultJson() { Status = "-1", Info = "" });
  304. }
  305. value = DesDecrypt(value);
  306. JsonData data = JsonMapper.ToObject(value);
  307. string key = data["key"].ToString();
  308. if(key != "llb#2024")
  309. {
  310. return Json(new AppResultJson() { Status = "-1", Info = "无权限" });
  311. }
  312. var groups = _ApiGroupService.GetList(m => m.groupKind == 1).Select(m => m.groupName).ToList();
  313. return Json(new AppResultJson() { Status = "1", Info = "", Data = groups });
  314. }
  315. #endregion
  316. #region 获取OSS参数
  317. [Route("/noauth/api/ossparam")]
  318. public JsonResult ossParam(string value)
  319. {
  320. OssConfigs ossConfigs = new();
  321. AppSettings.Bind("OssConfigs", ossConfigs);
  322. Dictionary<string, object> Obj = new Dictionary<string, object>();
  323. Obj.Add("AccessId", ossConfigs.Key);
  324. Obj.Add("AccessKey", ossConfigs.Secret);
  325. return Json(new AppResultJson() { Status = "-1", Info = "", Data = Obj });
  326. }
  327. #endregion
  328. }
  329. }