ApiPubController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. namespace Controllers
  20. {
  21. /// <summary>
  22. /// ApiInfo)Controller
  23. /// </summary>
  24. // [Route("ApiInfo")]
  25. // [ApiExplorerSettings(GroupName = "ApiInfo")]
  26. public class ApiPubController : BaseController
  27. {
  28. private readonly IApiInfoService _ApiInfoService;
  29. private readonly IApiGroupService _ApiGroupService;
  30. public ApiPubController(IApiInfoService ApiInfoService, IApiGroupService ApiGroupService)
  31. {
  32. _ApiInfoService = ApiInfoService;
  33. _ApiGroupService = ApiGroupService;
  34. }
  35. #region 通用-接收接口数据通知
  36. [HttpPost]
  37. [Route("/noauth/api/notice")]
  38. public string notice(string value)
  39. {
  40. JsonData data = JsonMapper.ToObject(value);
  41. Dictionary<string, object> obj = new Dictionary<string, object>();
  42. for (int i = 0; i < data.Count; i++)
  43. {
  44. string id = data[i]["id"].ToString();
  45. string groupName = data[i]["group_name"].ToString();
  46. string groupRemark = data[i]["group_remark"].ToString();
  47. int groupKind = int.Parse(data[i]["group_kind"].ToString());
  48. int groupId = 0;
  49. ApiGroup group = _ApiGroupService.GetFirst(m => m.groupName == groupName && m.groupKind == groupKind);
  50. if(group == null)
  51. {
  52. groupId = _ApiGroupService.Add(new ApiGroup()
  53. {
  54. createDate = DateTime.Now,
  55. groupName = groupName,
  56. groupRemark = groupRemark,
  57. groupKind = groupKind,
  58. groupVersion = "1",
  59. });
  60. }
  61. else
  62. {
  63. groupId = group.id;
  64. group.updateDate = DateTime.Now;
  65. group.groupRemark = groupRemark;
  66. int groupVersion = int.Parse(group.groupVersion) + 1;
  67. group.groupVersion = groupVersion.ToString();
  68. _ApiGroupService.Update(group, true);
  69. }
  70. JsonData apiList = data[i]["child"];
  71. for (int j = 0; j < apiList.Count; j++)
  72. {
  73. string ApiRouter = apiList[j]["api_router"].ToString();
  74. string ApiPort = apiList[j]["api_port"].ToString();
  75. string ApiHost = apiList[j]["api_host"].ToString();
  76. string ApiKey = apiList[j]["api_key"].ToString();
  77. string ApiName = apiList[j]["api_name"].ToString();
  78. string ApiMethod = apiList[j]["api_method"].ToString();
  79. ApiInfo apiInfo = _ApiInfoService.GetFirst(m => m.groupId == groupId && m.apiKey == ApiKey);
  80. if(apiInfo == null)
  81. {
  82. _ApiInfoService.Add(new ApiInfo()
  83. {
  84. createDate = DateTime.Now,
  85. apiKey = ApiKey,
  86. apiRouter = ApiRouter,
  87. apiPort = int.Parse(Function.CheckInt(ApiPort)),
  88. apiHost = ApiHost,
  89. apiName = ApiName,
  90. apiMethod = ApiMethod,
  91. groupId = groupId,
  92. });
  93. }
  94. else
  95. {
  96. apiInfo.apiRouter = ApiRouter;
  97. apiInfo.apiPort = int.Parse(Function.CheckInt(ApiPort));
  98. apiInfo.apiHost = ApiHost;
  99. apiInfo.apiName = ApiName;
  100. apiInfo.apiMethod = ApiMethod;
  101. _ApiInfoService.Update(apiInfo, true);
  102. }
  103. }
  104. }
  105. return "success";
  106. }
  107. #endregion
  108. #region 通用-获取接口列表
  109. [Route("/noauth/api/list")]
  110. public JsonResult list(string value)
  111. {
  112. if(string.IsNullOrEmpty(value))
  113. {
  114. return Json(new AppResultJson() { Status = "-1", Info = "" });
  115. }
  116. value = DesDecrypt(value);
  117. JsonData data = JsonMapper.ToObject(value);
  118. Dictionary<string, int> groupobj = new Dictionary<string, int>();
  119. Dictionary<string, object> obj = new Dictionary<string, object>();
  120. if(value.Contains("\"groups\""))
  121. {
  122. JsonData groupJson = data["groups"];
  123. for(int i = 0; i < groupJson.Count; i ++)
  124. {
  125. string name = groupJson[i]["name"].ToString();
  126. int version = int.Parse(groupJson[i]["version"].ToString());
  127. groupobj.Add(name, version);
  128. }
  129. }
  130. var groups = _ApiGroupService.GetList(m => m.groupKind == 0);
  131. foreach(var group in groups)
  132. {
  133. int curVersion = int.Parse(Function.CheckInt(group.groupVersion));
  134. if(groupobj.Any(m => m.Key == group.groupName && m.Value < curVersion))
  135. {
  136. Dictionary<string, object> groupData = new Dictionary<string, object>();
  137. var apis = _ApiInfoService.GetList(m => m.groupId == group.id).ToList();
  138. foreach(var api in apis)
  139. {
  140. Dictionary<string, object> apiData = new Dictionary<string, object>();
  141. string url = api.apiHost;
  142. if(api.apiPort != 80 && api.apiPort != 443)
  143. {
  144. url += ":" + api.apiPort;
  145. }
  146. url += api.apiRouter;
  147. apiData.Add("name", api.apiName);
  148. apiData.Add("url", url);
  149. apiData.Add("method", api.apiMethod);
  150. groupData.Add(api.apiKey, apiData);
  151. }
  152. groupData.Add("groupVersion", group.groupVersion);
  153. groupData.Add("groupInfo", group.groupRemark);
  154. obj.Add(group.groupName, groupData);
  155. }
  156. }
  157. return Json(new AppResultJson() { Status = "1", Info = "", Data = obj });
  158. }
  159. #endregion
  160. #region 通用-获取接口列表
  161. [Route("/noauth/api/groups")]
  162. public JsonResult groups(string value)
  163. {
  164. if(string.IsNullOrEmpty(value))
  165. {
  166. return Json(new AppResultJson() { Status = "-1", Info = "" });
  167. }
  168. value = DesDecrypt(value);
  169. JsonData data = JsonMapper.ToObject(value);
  170. string key = data["key"].ToString();
  171. if(key != "tel#2024")
  172. {
  173. return Json(new AppResultJson() { Status = "-1", Info = "无权限" });
  174. }
  175. var groups = _ApiGroupService.GetList(m => m.groupKind == 0).Select(m => m.groupName).ToList();
  176. return Json(new AppResultJson() { Status = "1", Info = "", Data = groups });
  177. }
  178. #endregion
  179. #region 通用-获取接口列表(后台)
  180. [Route("/noauth/api/listforadmin")]
  181. public JsonResult listForAdmin(string value)
  182. {
  183. if(string.IsNullOrEmpty(value))
  184. {
  185. return Json(new AppResultJson() { Status = "-1", Info = "" });
  186. }
  187. value = DesDecrypt(value);
  188. JsonData data = JsonMapper.ToObject(value);
  189. Dictionary<string, int> groupobj = new Dictionary<string, int>();
  190. Dictionary<string, object> obj = new Dictionary<string, object>();
  191. if(value.Contains("\"groups\""))
  192. {
  193. JsonData groupJson = data["groups"];
  194. for(int i = 0; i < groupJson.Count; i ++)
  195. {
  196. string name = groupJson[i]["name"].ToString();
  197. int version = int.Parse(groupJson[i]["version"].ToString());
  198. groupobj.Add(name, version);
  199. }
  200. }
  201. var groups = _ApiGroupService.GetList(m => m.groupKind == 1);
  202. foreach(var group in groups)
  203. {
  204. int curVersion = int.Parse(Function.CheckInt(group.groupVersion));
  205. if(groupobj.Any(m => m.Key == group.groupName && m.Value < curVersion))
  206. {
  207. Dictionary<string, object> groupData = new Dictionary<string, object>();
  208. var apis = _ApiInfoService.GetList(m => m.groupId == group.id).ToList();
  209. foreach(var api in apis)
  210. {
  211. Dictionary<string, object> apiData = new Dictionary<string, object>();
  212. string url = api.apiHost;
  213. if(api.apiPort != 80 && api.apiPort != 443)
  214. {
  215. url += ":" + api.apiPort;
  216. }
  217. url += api.apiRouter;
  218. apiData.Add("name", api.apiName);
  219. apiData.Add("url", url);
  220. apiData.Add("method", api.apiMethod);
  221. groupData.Add(api.apiKey, apiData);
  222. }
  223. groupData.Add("groupVersion", group.groupVersion);
  224. groupData.Add("groupInfo", group.groupRemark);
  225. obj.Add(group.groupName, groupData);
  226. }
  227. }
  228. return Json(new AppResultJson() { Status = "1", Info = "", Data = obj });
  229. }
  230. #endregion
  231. #region 通用-获取接口列表(后台)
  232. [Route("/noauth/api/groupsforadmin")]
  233. public JsonResult groupsForAdmin(string value)
  234. {
  235. if(string.IsNullOrEmpty(value))
  236. {
  237. return Json(new AppResultJson() { Status = "-1", Info = "" });
  238. }
  239. value = DesDecrypt(value);
  240. JsonData data = JsonMapper.ToObject(value);
  241. string key = data["key"].ToString();
  242. if(key != "tel#2024")
  243. {
  244. return Json(new AppResultJson() { Status = "-1", Info = "无权限" });
  245. }
  246. var groups = _ApiGroupService.GetList(m => m.groupKind == 1).Select(m => m.groupName).ToList();
  247. return Json(new AppResultJson() { Status = "1", Info = "", Data = groups });
  248. }
  249. #endregion
  250. #region 获取OSS参数
  251. [Route("/noauth/api/ossparam")]
  252. public JsonResult ossParam(string value)
  253. {
  254. OssConfigs ossConfigs = new();
  255. AppSettings.Bind("OssConfigs", ossConfigs);
  256. Dictionary<string, object> Obj = new Dictionary<string, object>();
  257. Obj.Add("AccessId", ossConfigs.Key);
  258. Obj.Add("AccessKey", ossConfigs.Secret);
  259. return Json(new AppResultJson() { Status = "-1", Info = "", Data = Obj });
  260. }
  261. #endregion
  262. }
  263. }