SystemConfigController.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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")]
  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}{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}{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")]
  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.site = currentSite;
  249. currentSysConfig.nickName = currentSite;
  250. currentSysConfig.proDeptId = long.Parse(_configuration["CustomParam:proDeptId"]);
  251. currentSysConfig.clientKey.clientID = _configuration["HaBookAuth:CoreService:clientID"];
  252. currentSysConfig.clientKey.clientSecret = _configuration["HaBookAuth:CoreService:clientSecret"];
  253. currentSysConfig.dDAuth.agentId = _configuration["DingDingAuth:Agentld"];
  254. currentSysConfig.dDAuth.appKey = _configuration["DingDingAuth:appKey"];
  255. currentSysConfig.dDAuth.appSecret = _configuration["DingDingAuth:appSecret"];
  256. currentSysConfig.azureClient.storage = _configuration["Azure:Storage:ConnectionString"];
  257. currentSysConfig.azureClient.cosmos = _configuration["Azure:Cosmos:ConnectionString"];
  258. currentSysConfig.azureClient.redis = _configuration["Azure:Redis:ConnectionString"];
  259. currentSysConfig.azureClient.servicBus = _configuration["Azure:ServiceBus:ConnectionString"];
  260. return Ok(new { state = 201, allConfigs, currentSysConfig });
  261. }
  262. }
  263. /// <summary>
  264. /// 修改或者添加配置文件,到多个配置文件中
  265. /// </summary>
  266. /// <param name="sysConfig"></param>
  267. /// <returns></returns>
  268. [ProducesDefaultResponseType]
  269. [AuthToken(Roles = "admin")]
  270. [HttpPost("set-singleconfig")]
  271. public async Task<IActionResult> SetSingleConfig(SysConfig sysConfig)
  272. {
  273. var builder = $"{_hostingEnvironment.ContentRootPath}/JsonFile/SystemConfig.json";
  274. StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  275. StringBuilder stringBuilder = new();
  276. string text;
  277. while ((text = streamReader.ReadLine()) != null)
  278. {
  279. stringBuilder.Append(text.ToString());
  280. }
  281. string input = stringBuilder.ToString();
  282. List<SysConfig> allConfigs = new();
  283. if (!string.IsNullOrEmpty(input))
  284. {
  285. try
  286. {
  287. allConfigs = input.ToObject<List<SysConfig>>();
  288. }
  289. catch
  290. {
  291. allConfigs.Add(sysConfig);
  292. }
  293. var tempSysConfig = allConfigs.Where(x => x.site.Equals(sysConfig.site)).FirstOrDefault();
  294. if (tempSysConfig != null)
  295. {
  296. tempSysConfig.site = sysConfig.site;
  297. tempSysConfig.nickName = sysConfig.nickName;
  298. tempSysConfig.proDeptId = sysConfig.proDeptId;
  299. tempSysConfig.clientKey = sysConfig.clientKey;
  300. tempSysConfig.dDAuth = sysConfig.dDAuth;
  301. tempSysConfig.azureClient = sysConfig.azureClient;
  302. }
  303. else
  304. {
  305. allConfigs.Add(sysConfig);
  306. }
  307. }
  308. else
  309. {
  310. allConfigs.Add(sysConfig);
  311. }
  312. string ListJson = JsonConvert.SerializeObject(allConfigs, Formatting.Indented);
  313. System.IO.File.WriteAllText(builder, ListJson);
  314. return Ok(new { state = 200, allConfigs });
  315. }
  316. /// <summary>
  317. /// 切换站点
  318. /// </summary>
  319. /// <param name="jsonElement"></param>
  320. /// <returns></returns>
  321. [ProducesDefaultResponseType]
  322. [AuthToken(Roles = "admin")]
  323. [HttpPost("cut-site")]
  324. public async Task<IActionResult> CutSite(JsonElement jsonElement)
  325. {
  326. if (!jsonElement.TryGetProperty("site", out JsonElement site)) return BadRequest();
  327. var builder = $"{_hostingEnvironment.ContentRootPath}/JsonFile/SystemConfig.json";
  328. var formalDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.json";
  329. var extendDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.Development.json";
  330. StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
  331. StringBuilder stringBuilder = new();
  332. string text;
  333. while ((text = streamReader.ReadLine()) != null)
  334. {
  335. stringBuilder.Append(text.ToString());
  336. }
  337. string input = stringBuilder.ToString();
  338. List<SysConfig> allConfigs = input.ToObject<List<SysConfig>>();
  339. var tempSysConfig = allConfigs.Where(x => x.site.Equals($"{site}")).FirstOrDefault();
  340. // 有排版 注释全无
  341. StreamReader reader = System.IO.File.OpenText(formalDeploy);
  342. JsonTextReader jsonTextReader = new(reader);
  343. JObject jsonObject = (JObject)JToken.ReadFrom(jsonTextReader);
  344. StreamReader readerDeve = System.IO.File.OpenText(extendDeploy);
  345. JsonTextReader jsonTextReaderDeve = new(readerDeve);
  346. JObject jsonObjectDeve = (JObject)JToken.ReadFrom(jsonTextReaderDeve);
  347. if (tempSysConfig != null)
  348. {
  349. jsonObject["Option"]["Location"] = $"{site}";
  350. jsonObject["CustomParam"]["proDeptId"] = $"{tempSysConfig.proDeptId}";
  351. jsonObjectDeve["Option"]["Location"] = $"{site}";
  352. jsonObjectDeve["CustomParam"]["proDeptId"] = $"{tempSysConfig.proDeptId}";
  353. jsonObject["HaBookAuth"]["CoreService"]["clientID"] = tempSysConfig.clientKey.clientID;
  354. jsonObject["HaBookAuth"]["CoreService"]["clientSecret"] = tempSysConfig.clientKey.clientSecret;
  355. jsonObjectDeve["HaBookAuth"]["CoreService"]["clientID"] = tempSysConfig.clientKey.clientID;
  356. jsonObjectDeve["HaBookAuth"]["CoreService"]["clientSecret"] = tempSysConfig.clientKey.clientSecret;
  357. jsonObject["DingDingAuth"]["Agentld"] = $"{tempSysConfig.dDAuth.agentId}";
  358. jsonObject["DingDingAuth"]["appKey"] = $"{tempSysConfig.dDAuth.appKey}";
  359. jsonObject["DingDingAuth"]["appSecret"] = $"{tempSysConfig.dDAuth.appSecret}";
  360. jsonObjectDeve["DingDingAuth"]["Agentld"] = $"{tempSysConfig.dDAuth.agentId}";
  361. jsonObjectDeve["DingDingAuth"]["appKey"] = $"{tempSysConfig.dDAuth.appKey}";
  362. jsonObjectDeve["DingDingAuth"]["appSecret"] = $"{tempSysConfig.dDAuth.appSecret}";
  363. jsonObject["Azure"]["Storage"]["ConnectionString"] = $"{ tempSysConfig.azureClient.storage}";
  364. jsonObject["Azure"]["Cosmos"]["ConnectionString"] = $"{tempSysConfig.azureClient.cosmos}";
  365. jsonObject["Azure"]["Redis"]["ConnectionString"] = $"{tempSysConfig.azureClient.redis}";
  366. jsonObject["Azure"]["ServiceBus"]["ConnectionString"] = $"{tempSysConfig.azureClient.servicBus}";
  367. jsonObjectDeve["Azure"]["Storage"]["ConnectionString"] = $"{ tempSysConfig.azureClient.storage}";
  368. jsonObjectDeve["Azure"]["Cosmos"]["ConnectionString"] = $"{tempSysConfig.azureClient.cosmos}";
  369. jsonObjectDeve["Azure"]["Redis"]["ConnectionString"] = $"{tempSysConfig.azureClient.redis}";
  370. jsonObjectDeve["Azure"]["ServiceBus"]["ConnectionString"] = $"{tempSysConfig.azureClient.servicBus}";
  371. }
  372. else return Ok(new { state = 404,msg = "未找到你需要的参数" });
  373. reader.Close();
  374. string output = JsonConvert.SerializeObject(jsonObject, Formatting.Indented); //是否json文件有缩进
  375. System.IO.File.WriteAllText(formalDeploy, output);
  376. readerDeve.Close();
  377. string outputDeve = JsonConvert.SerializeObject(jsonObjectDeve, Formatting.Indented); //是否json文件有缩进
  378. System.IO.File.WriteAllText(extendDeploy, outputDeve);
  379. return Ok(new { state = 200, SysConfig = tempSysConfig });
  380. }
  381. }
  382. }