SystemConfigController.cs 20 KB

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