SystemConfigController.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. using Microsoft.AspNetCore.Hosting;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Text;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. using System;
  10. using System.IO;
  11. using System.Collections.Generic;
  12. using Microsoft.Extensions.Configuration;
  13. using TEAMModelOS.Models;
  14. using Microsoft.Extensions.Options;
  15. using TEAMModelBI.Tool.Extension;
  16. using TEAMModelOS.SDK.DI;
  17. using TEAMModelBI.Filter;
  18. using TEAMModelOS.SDK.Extension;
  19. using System.Linq;
  20. using TEAMModelBI.Models;
  21. namespace TEAMModelBI.Controllers.ManySiteCut
  22. {
  23. [Route("syscfg")]
  24. [ApiController]
  25. public class SystemConfigController : ControllerBase
  26. {
  27. private readonly IConfiguration _configuration;
  28. private readonly IWebHostEnvironment _hostingEnvironment; //读取文件
  29. private readonly Option _option;
  30. //钉钉提示信息
  31. private readonly DingDing _dingDing;
  32. public SystemConfigController(IConfiguration configuration,IWebHostEnvironment hostingEnvironment, IOptionsSnapshot<Option> option, DingDing dingDing)
  33. {
  34. _configuration = configuration;
  35. _hostingEnvironment = hostingEnvironment;
  36. _option = option?.Value;
  37. _dingDing = dingDing;
  38. }
  39. /// <summary>
  40. /// 获取应用配置文件
  41. /// </summary>
  42. /// <returns></returns>
  43. [ProducesDefaultResponseType]
  44. [HttpPost("get-config")]
  45. public async Task<IActionResult> GetConfigJson()
  46. {
  47. //string SiteScope1 = _configuration["CustomParam:SiteScope"];
  48. string SiteScope = _option.Location;
  49. string proDeptId = _configuration["CustomParam:proDeptId"];
  50. string ddAgentld = _configuration["DingDingAuth:Agentld"];
  51. string ddAppKey = _configuration["DingDingAuth:appKey"];
  52. string ddAppSecret = _configuration["DingDingAuth:appSecret"];
  53. string storage = _configuration["Azure:Storage:ConnectionString"];
  54. string cosmos = _configuration["Azure:Cosmos:ConnectionString"];
  55. string redis = _configuration["Azure:Redis:ConnectionString"];
  56. string serviceBus = _configuration["Azure:ServiceBus:ConnectionString"];
  57. return Ok(new { state = 200, SiteScope, proDeptId, ddAgentld, ddAppKey, ddAppSecret, storage, cosmos, redis, serviceBus });
  58. }
  59. /// <summary>
  60. /// 应用配置文件修改
  61. /// </summary>
  62. /// <param name="jsonElement"></param>
  63. /// <returns></returns>
  64. [ProducesDefaultResponseType]
  65. [AuthToken(Roles = "admin,rdc")]
  66. [HttpPost("set-config")]
  67. public async Task<IActionResult> SetConfigJson(JsonElement jsonElement)
  68. {
  69. try
  70. {
  71. if (!jsonElement.TryGetProperty("busy", out JsonElement busy)) return BadRequest();
  72. jsonElement.TryGetProperty("site", out JsonElement site);
  73. jsonElement.TryGetProperty("proDeptId", out JsonElement proDeptId);
  74. jsonElement.TryGetProperty("appId", out JsonElement appId);
  75. jsonElement.TryGetProperty("appKey", out JsonElement appKey);
  76. jsonElement.TryGetProperty("appSecret", out JsonElement appSecret);
  77. jsonElement.TryGetProperty("storage", out JsonElement storage);
  78. jsonElement.TryGetProperty("cosmosDb", out JsonElement cosmosDb);
  79. jsonElement.TryGetProperty("redis", out JsonElement redis);
  80. jsonElement.TryGetProperty("serviceBus", out JsonElement serviceBus);
  81. var formalDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.json";
  82. var extendDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.Development.json";
  83. //方式二 有排版 注释全无
  84. StreamReader reader = System.IO.File.OpenText(formalDeploy);
  85. JsonTextReader jsonTextReader = new(reader);
  86. JObject jsonObject = (JObject)JToken.ReadFrom(jsonTextReader);
  87. StreamReader readerDeve = System.IO.File.OpenText(extendDeploy);
  88. JsonTextReader jsonTextReaderDeve = new(readerDeve);
  89. JObject jsonObjectDeve = (JObject)JToken.ReadFrom(jsonTextReaderDeve);
  90. switch ($"{busy}")
  91. {
  92. case "website":
  93. if (!string.IsNullOrEmpty($"{site}"))
  94. {
  95. jsonObject["Option"]["Location"] = $"{site}";
  96. jsonObjectDeve["Option"]["Location"] = $"{site}";
  97. }
  98. if (!string.IsNullOrEmpty($"{proDeptId}"))
  99. {
  100. jsonObject["CustomParam"]["proDeptId"] = $"{proDeptId}";
  101. jsonObjectDeve["CustomParam"]["proDeptId"] = $"{proDeptId}";
  102. }
  103. break;
  104. case "dd":
  105. if (!string.IsNullOrEmpty($"{appId}"))
  106. {
  107. jsonObject["DingDingAuth"]["Agentld"] = $"{appId}";
  108. jsonObjectDeve["DingDingAuth"]["Agentld"] = $"{appId}";
  109. }
  110. if (!string.IsNullOrEmpty($"{appKey}"))
  111. {
  112. jsonObject["DingDingAuth"]["appKey"] = $"{appKey}";
  113. jsonObjectDeve["DingDingAuth"]["appKey"] = $"{appKey}";
  114. }
  115. if (!string.IsNullOrEmpty($"{appSecret}"))
  116. {
  117. jsonObject["DingDingAuth"]["appSecret"] = $"{appSecret}";
  118. jsonObjectDeve["DingDingAuth"]["appSecret"] = $"{appSecret}";
  119. }
  120. break;
  121. case "dbs":
  122. if (!string.IsNullOrEmpty($"{storage}"))
  123. {
  124. jsonObject["Azure"]["Storage"]["ConnectionString"] = $"{storage}";
  125. jsonObjectDeve["Azure"]["Storage"]["ConnectionString"] = $"{storage}";
  126. }
  127. if (!string.IsNullOrEmpty($"{cosmosDb}"))
  128. {
  129. jsonObject["Azure"]["Cosmos"]["ConnectionString"] = $"{cosmosDb}";
  130. jsonObjectDeve["Azure"]["Cosmos"]["ConnectionString"] = $"{cosmosDb}";
  131. }
  132. if (!string.IsNullOrEmpty($"{redis}"))
  133. {
  134. jsonObject["Azure"]["Redis"]["ConnectionString"] = $"{redis}";
  135. jsonObjectDeve["Azure"]["Redis"]["ConnectionString"] = $"{redis}";
  136. }
  137. if (!string.IsNullOrEmpty($"{serviceBus}"))
  138. {
  139. jsonObject["Azure"]["ServiceBus"]["ConnectionString"] = $"{serviceBus}";
  140. jsonObjectDeve["Azure"]["ServiceBus"]["ConnectionString"] = $"{serviceBus}";
  141. }
  142. break;
  143. //default:
  144. // if (!string.IsNullOrEmpty($"{site}"))
  145. // {
  146. // jsonObject["Option"]["Location"] = $"{site}";
  147. // }
  148. // if (!string.IsNullOrEmpty($"{proDeptId}"))
  149. // {
  150. // jsonObject["CustomParam"]["proDeptId"] = $"{proDeptId}";
  151. // }
  152. // if (!string.IsNullOrEmpty($"{appId}"))
  153. // {
  154. // jsonObject["DingDingAuth"]["Agentld"] = $"{appId}";
  155. // }
  156. // if (!string.IsNullOrEmpty($"{appKey}"))
  157. // {
  158. // jsonObject["DingDingAuth"]["appKey"] = $"{appKey}";
  159. // }
  160. // if (!string.IsNullOrEmpty($"{appSecret}"))
  161. // {
  162. // jsonObject["DingDingAuth"]["appSecret"] = $"{appSecret}";
  163. // }
  164. // if (!string.IsNullOrEmpty($"{storage}"))
  165. // {
  166. // jsonObject["Azure"]["Storage"]["ConnectionString"] = $"{storage}";
  167. // }
  168. // if (!string.IsNullOrEmpty($"{cosmosDb}"))
  169. // {
  170. // jsonObject["Azure"]["Cosmos"]["ConnectionString"] = $"{cosmosDb}";
  171. // }
  172. // if (!string.IsNullOrEmpty($"{redis}"))
  173. // {
  174. // jsonObject["Azure"]["Redis"]["ConnectionString"] = $"{redis}";
  175. // }
  176. // if (!string.IsNullOrEmpty($"{serviceBus}"))
  177. // {
  178. // jsonObject["Azure"]["ServiceBus"]["ConnectionString"] = $"{serviceBus}";
  179. // }
  180. // break;
  181. }
  182. try
  183. {
  184. reader.Close();
  185. string output = JsonConvert.SerializeObject(jsonObject, Formatting.Indented); //是否json文件有缩进
  186. System.IO.File.WriteAllText(formalDeploy, output);
  187. readerDeve.Close();
  188. string outputDeve = JsonConvert.SerializeObject(jsonObjectDeve, Formatting.Indented); //是否json文件有缩进
  189. System.IO.File.WriteAllText(extendDeploy, outputDeve);
  190. }
  191. catch (Exception ex)
  192. {
  193. await _dingDing.SendBotMsg($"BI,{_option.Location} /syscfg/set-config \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  194. return Ok(new { state = 403, msg = $"保存失败!" });
  195. }
  196. ////方式一 无排版 注释全无
  197. //JObject jsonObject;
  198. //using (StreamReader file = new StreamReader(formalDeploy))
  199. //using (JsonTextReader reader = new JsonTextReader(file))
  200. //{
  201. // jsonObject = (JObject)JToken.ReadFrom(reader);
  202. // jsonObject["DingDingAuth"]["getuserinfo_bycode"] = "asdasdasdasd";
  203. //}
  204. //using (var writer = new StreamWriter(formalDeploy))
  205. //using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
  206. //{
  207. // jsonObject.WriteTo(jsonwriter);
  208. //}
  209. return Ok(new { state = 200 });
  210. }
  211. catch (Exception ex)
  212. {
  213. await _dingDing.SendBotMsg($"BI,{_option.Location} /syscfg/set-config \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  214. return BadRequest();
  215. }
  216. }
  217. /// <summary>
  218. /// 获取所有的应用链接秘钥,以及当前应用链接秘钥 //已对接
  219. /// </summary>
  220. /// <param name="jsonElement"></param>
  221. /// <returns></returns>
  222. [ProducesDefaultResponseType]
  223. [AuthToken(Roles = "admin,rdc")]
  224. [HttpPost("get-allconfig")]
  225. public async Task<IActionResult> GetAllConfig()
  226. {
  227. var builder = $"{_hostingEnvironment.ContentRootPath}/JsonFile/SystemConfig.json";
  228. StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  229. StringBuilder stringBuilder = new();
  230. string text;
  231. while ((text = streamReader.ReadLine()) != null)
  232. {
  233. stringBuilder.Append(text.ToString());
  234. }
  235. string input = stringBuilder.ToString();
  236. List<SysConfig> allConfigs = input.ToObject<List<SysConfig>>();
  237. var currentSite = _option.Location;
  238. SysConfig currentSysConfig = new();
  239. try
  240. {
  241. currentSysConfig = allConfigs.Where(x => x.site.Equals(currentSite)).FirstOrDefault();
  242. }
  243. catch{}
  244. if (currentSysConfig != null)
  245. {
  246. return Ok(new { state = 200, allConfigs, currentSysConfig });
  247. }
  248. else {
  249. currentSysConfig = new SysConfig();
  250. currentSysConfig.site = $"{currentSite}";
  251. currentSysConfig.nickName = $"{currentSite}";
  252. currentSysConfig.proDeptId = long.Parse(_configuration["CustomParam:proDeptId"]);
  253. currentSysConfig.clientKey = new()
  254. {
  255. clientID = _configuration["HaBookAuth:CoreService:clientID"],
  256. clientSecret = _configuration["HaBookAuth:CoreService:clientSecret"]
  257. };
  258. currentSysConfig.dDAuth = new()
  259. {
  260. agentId = _configuration["DingDingAuth:Agentld"],
  261. appKey = _configuration["DingDingAuth:appKey"],
  262. appSecret = _configuration["DingDingAuth:appSecret"]
  263. };
  264. currentSysConfig.azureClient = new()
  265. {
  266. storage = _configuration["Azure:Storage:ConnectionString"],
  267. cosmos = _configuration["Azure:Cosmos:ConnectionString"],
  268. redis = _configuration["Azure:Redis:ConnectionString"],
  269. servicBus = _configuration["Azure:ServiceBus:ConnectionString"]
  270. };
  271. return Ok(new { state = 201, allConfigs, currentSysConfig });
  272. }
  273. }
  274. /// <summary>
  275. /// 修改或者添加配置文件,到多个配置文件中 //已对接
  276. /// </summary>
  277. /// <param name="sysConfig"></param>
  278. /// <returns></returns>
  279. [ProducesDefaultResponseType]
  280. [AuthToken(Roles = "admin,rdc")]
  281. [HttpPost("set-singleconfig")]
  282. public async Task<IActionResult> SetSingleConfig(SysConfig sysConfig)
  283. {
  284. var builder = $"{_hostingEnvironment.ContentRootPath}/JsonFile/SystemConfig.json";
  285. StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  286. StringBuilder stringBuilder = new();
  287. string text;
  288. while ((text = streamReader.ReadLine()) != null)
  289. {
  290. stringBuilder.Append(text.ToString());
  291. }
  292. string input = stringBuilder.ToString();
  293. List<SysConfig> allConfigs = new();
  294. if (!string.IsNullOrEmpty(input))
  295. {
  296. try
  297. {
  298. allConfigs = input.ToObject<List<SysConfig>>();
  299. }
  300. catch
  301. {
  302. allConfigs.Add(sysConfig);
  303. }
  304. var tempSysConfig = allConfigs.Where(x => x.site.Equals(sysConfig.site)).FirstOrDefault();
  305. if (tempSysConfig != null)
  306. {
  307. tempSysConfig.site = sysConfig.site;
  308. tempSysConfig.nickName = sysConfig.nickName;
  309. tempSysConfig.proDeptId = sysConfig.proDeptId;
  310. tempSysConfig.clientKey = sysConfig.clientKey;
  311. tempSysConfig.dDAuth = sysConfig.dDAuth;
  312. tempSysConfig.azureClient = sysConfig.azureClient;
  313. }
  314. else
  315. {
  316. allConfigs.Add(sysConfig);
  317. }
  318. }
  319. else
  320. {
  321. allConfigs.Add(sysConfig);
  322. }
  323. string ListJson = JsonConvert.SerializeObject(allConfigs, Formatting.Indented);
  324. System.IO.File.WriteAllText(builder, ListJson);
  325. return Ok(new { state = 200, allConfigs });
  326. }
  327. /// <summary>
  328. /// 切换站点 //已对接
  329. /// </summary>
  330. /// <param name="jsonElement"></param>
  331. /// <returns></returns>
  332. [ProducesDefaultResponseType]
  333. [AuthToken(Roles = "admin,rdc")]
  334. [HttpPost("cut-site")]
  335. public async Task<IActionResult> CutSite(JsonElement jsonElement)
  336. {
  337. if (!jsonElement.TryGetProperty("site", out JsonElement site)) return BadRequest();
  338. var builder = $"{_hostingEnvironment.ContentRootPath}/JsonFile/SystemConfig.json";
  339. var formalDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.json";
  340. var extendDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.Development.json";
  341. StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  342. StringBuilder stringBuilder = new();
  343. string text;
  344. while ((text = streamReader.ReadLine()) != null)
  345. {
  346. stringBuilder.Append(text.ToString());
  347. }
  348. string input = stringBuilder.ToString();
  349. List<SysConfig> allConfigs = input.ToObject<List<SysConfig>>();
  350. var tempSysConfig = allConfigs.Where(x => x.site.Equals($"{site}")).FirstOrDefault();
  351. // 有排版 注释全无
  352. StreamReader reader = System.IO.File.OpenText(formalDeploy);
  353. JsonTextReader jsonTextReader = new(reader);
  354. JObject jsonObject = (JObject)JToken.ReadFrom(jsonTextReader);
  355. StreamReader readerDeve = System.IO.File.OpenText(extendDeploy);
  356. JsonTextReader jsonTextReaderDeve = new(readerDeve);
  357. JObject jsonObjectDeve = (JObject)JToken.ReadFrom(jsonTextReaderDeve);
  358. if (tempSysConfig != null)
  359. {
  360. jsonObject["Option"]["Location"] = $"{site}";
  361. jsonObject["CustomParam"]["proDeptId"] = $"{tempSysConfig.proDeptId}";
  362. jsonObjectDeve["Option"]["Location"] = $"{site}";
  363. jsonObjectDeve["CustomParam"]["proDeptId"] = $"{tempSysConfig.proDeptId}";
  364. jsonObject["HaBookAuth"]["CoreService"]["clientID"] = tempSysConfig.clientKey.clientID;
  365. jsonObject["HaBookAuth"]["CoreService"]["clientSecret"] = tempSysConfig.clientKey.clientSecret;
  366. jsonObjectDeve["HaBookAuth"]["CoreService"]["clientID"] = tempSysConfig.clientKey.clientID;
  367. jsonObjectDeve["HaBookAuth"]["CoreService"]["clientSecret"] = tempSysConfig.clientKey.clientSecret;
  368. jsonObject["DingDingAuth"]["Agentld"] = $"{tempSysConfig.dDAuth.agentId}";
  369. jsonObject["DingDingAuth"]["appKey"] = $"{tempSysConfig.dDAuth.appKey}";
  370. jsonObject["DingDingAuth"]["appSecret"] = $"{tempSysConfig.dDAuth.appSecret}";
  371. jsonObjectDeve["DingDingAuth"]["Agentld"] = $"{tempSysConfig.dDAuth.agentId}";
  372. jsonObjectDeve["DingDingAuth"]["appKey"] = $"{tempSysConfig.dDAuth.appKey}";
  373. jsonObjectDeve["DingDingAuth"]["appSecret"] = $"{tempSysConfig.dDAuth.appSecret}";
  374. jsonObject["Azure"]["Storage"]["ConnectionString"] = $"{ tempSysConfig.azureClient.storage}";
  375. jsonObject["Azure"]["Cosmos"]["ConnectionString"] = $"{tempSysConfig.azureClient.cosmos}";
  376. jsonObject["Azure"]["Redis"]["ConnectionString"] = $"{tempSysConfig.azureClient.redis}";
  377. jsonObject["Azure"]["ServiceBus"]["ConnectionString"] = $"{tempSysConfig.azureClient.servicBus}";
  378. jsonObjectDeve["Azure"]["Storage"]["ConnectionString"] = $"{ tempSysConfig.azureClient.storage}";
  379. jsonObjectDeve["Azure"]["Cosmos"]["ConnectionString"] = $"{tempSysConfig.azureClient.cosmos}";
  380. jsonObjectDeve["Azure"]["Redis"]["ConnectionString"] = $"{tempSysConfig.azureClient.redis}";
  381. jsonObjectDeve["Azure"]["ServiceBus"]["ConnectionString"] = $"{tempSysConfig.azureClient.servicBus}";
  382. }
  383. else return Ok(new { state = 404,msg = "未找到你需要的参数" });
  384. reader.Close();
  385. string output = JsonConvert.SerializeObject(jsonObject, Formatting.Indented); //是否json文件有缩进
  386. System.IO.File.WriteAllText(formalDeploy, output);
  387. readerDeve.Close();
  388. string outputDeve = JsonConvert.SerializeObject(jsonObjectDeve, Formatting.Indented); //是否json文件有缩进
  389. System.IO.File.WriteAllText(extendDeploy, outputDeve);
  390. return Ok(new { state = 200, SysConfig = tempSysConfig });
  391. }
  392. }
  393. }