SystemConfigController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. namespace TEAMModelBI.Controllers.DingDingStruc
  19. {
  20. [Route("syscfg")]
  21. [ApiController]
  22. public class SystemConfigController : ControllerBase
  23. {
  24. private readonly IConfiguration _configuration;
  25. private readonly IWebHostEnvironment _hostingEnvironment; //读取文件
  26. private readonly Option _option;
  27. //钉钉提示信息
  28. private readonly DingDing _dingDing;
  29. public SystemConfigController(IConfiguration configuration,IWebHostEnvironment hostingEnvironment, IOptionsSnapshot<Option> option, DingDing dingDing)
  30. {
  31. _configuration = configuration;
  32. _hostingEnvironment = hostingEnvironment;
  33. _option = option?.Value;
  34. _dingDing = dingDing;
  35. }
  36. /// <summary>
  37. ///
  38. /// </summary>
  39. /// <returns></returns>
  40. [HttpPost("get-config")]
  41. public async Task<IActionResult> GetConfigJson()
  42. {
  43. //string SiteScope1 = _configuration["CustomParam:SiteScope"];
  44. string SiteScope = _option.Location;
  45. string proDeptId = _configuration["CustomParam:proDeptId"];
  46. string ddAgentld = _configuration["DingDingAuth:Agentld"];
  47. string ddAppKey = _configuration["DingDingAuth:appKey"];
  48. string ddAppSecret = _configuration["DingDingAuth:appSecret"];
  49. string storage = _configuration["Azure:Storage:ConnectionString"];
  50. string cosmos = _configuration["Azure:Cosmos:ConnectionString"];
  51. string redis = _configuration["Azure:Redis:ConnectionString"];
  52. string serviceBus = _configuration["Azure:ServiceBus:ConnectionString"];
  53. return Ok(new { state = 200, SiteScope, proDeptId, ddAgentld, ddAppKey, ddAppSecret, storage, cosmos, redis, serviceBus });
  54. }
  55. /// <summary>
  56. /// 配置文件修改
  57. /// </summary>
  58. /// <param name="jsonElement"></param>
  59. /// <returns></returns>
  60. [ProducesDefaultResponseType]
  61. [AuthToken(Roles = "admin")]
  62. [HttpPost("set-config")]
  63. public async Task<IActionResult> SetConfigJson(JsonElement jsonElement)
  64. {
  65. try
  66. {
  67. if (!jsonElement.TryGetProperty("busy", out JsonElement busy)) return BadRequest();
  68. jsonElement.TryGetProperty("site", out JsonElement site);
  69. jsonElement.TryGetProperty("proDeptId", out JsonElement proDeptId);
  70. jsonElement.TryGetProperty("appId", out JsonElement appId);
  71. jsonElement.TryGetProperty("appKey", out JsonElement appKey);
  72. jsonElement.TryGetProperty("appSecret", out JsonElement appSecret);
  73. jsonElement.TryGetProperty("storage", out JsonElement storage);
  74. jsonElement.TryGetProperty("cosmosDb", out JsonElement cosmosDb);
  75. jsonElement.TryGetProperty("redis", out JsonElement redis);
  76. jsonElement.TryGetProperty("serviceBus", out JsonElement serviceBus);
  77. var formalDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.json";
  78. var extendDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.Development.json";
  79. //方式二 有排版 注释全无
  80. StreamReader reader = System.IO.File.OpenText(formalDeploy);
  81. JsonTextReader jsonTextReader = new(reader);
  82. JObject jsonObject = (JObject)JToken.ReadFrom(jsonTextReader);
  83. StreamReader readerDeve = System.IO.File.OpenText(extendDeploy);
  84. JsonTextReader jsonTextReaderDeve = new(readerDeve);
  85. JObject jsonObjectDeve = (JObject)JToken.ReadFrom(jsonTextReaderDeve);
  86. switch ($"{busy}")
  87. {
  88. case "website":
  89. if (!string.IsNullOrEmpty($"{site}"))
  90. {
  91. jsonObject["Option"]["Location"] = $"{site}";
  92. jsonObjectDeve["Option"]["Location"] = $"{site}";
  93. }
  94. if (!string.IsNullOrEmpty($"{proDeptId}"))
  95. {
  96. jsonObject["CustomParam"]["proDeptId"] = $"{proDeptId}";
  97. jsonObjectDeve["CustomParam"]["proDeptId"] = $"{proDeptId}";
  98. }
  99. break;
  100. case "dd":
  101. if (!string.IsNullOrEmpty($"{appId}"))
  102. {
  103. jsonObject["DingDingAuth"]["Agentld"] = $"{appId}";
  104. jsonObjectDeve["DingDingAuth"]["Agentld"] = $"{appId}";
  105. }
  106. if (!string.IsNullOrEmpty($"{appKey}"))
  107. {
  108. jsonObject["DingDingAuth"]["appKey"] = $"{appKey}";
  109. jsonObjectDeve["DingDingAuth"]["appKey"] = $"{appKey}";
  110. }
  111. if (!string.IsNullOrEmpty($"{appSecret}"))
  112. {
  113. jsonObject["DingDingAuth"]["appSecret"] = $"{appSecret}";
  114. jsonObjectDeve["DingDingAuth"]["appSecret"] = $"{appSecret}";
  115. }
  116. break;
  117. case "dbs":
  118. if (!string.IsNullOrEmpty($"{storage}"))
  119. {
  120. jsonObject["Azure"]["Storage"]["ConnectionString"] = $"{storage}";
  121. jsonObjectDeve["Azure"]["Storage"]["ConnectionString"] = $"{storage}";
  122. }
  123. if (!string.IsNullOrEmpty($"{cosmosDb}"))
  124. {
  125. jsonObject["Azure"]["Cosmos"]["ConnectionString"] = $"{cosmosDb}";
  126. jsonObjectDeve["Azure"]["Cosmos"]["ConnectionString"] = $"{cosmosDb}";
  127. }
  128. if (!string.IsNullOrEmpty($"{redis}"))
  129. {
  130. jsonObject["Azure"]["Redis"]["ConnectionString"] = $"{redis}";
  131. jsonObjectDeve["Azure"]["Redis"]["ConnectionString"] = $"{redis}";
  132. }
  133. if (!string.IsNullOrEmpty($"{serviceBus}"))
  134. {
  135. jsonObject["Azure"]["ServiceBus"]["ConnectionString"] = $"{serviceBus}";
  136. jsonObjectDeve["Azure"]["ServiceBus"]["ConnectionString"] = $"{serviceBus}";
  137. }
  138. break;
  139. //default:
  140. // if (!string.IsNullOrEmpty($"{site}"))
  141. // {
  142. // jsonObject["Option"]["Location"] = $"{site}";
  143. // }
  144. // if (!string.IsNullOrEmpty($"{proDeptId}"))
  145. // {
  146. // jsonObject["CustomParam"]["proDeptId"] = $"{proDeptId}";
  147. // }
  148. // if (!string.IsNullOrEmpty($"{appId}"))
  149. // {
  150. // jsonObject["DingDingAuth"]["Agentld"] = $"{appId}";
  151. // }
  152. // if (!string.IsNullOrEmpty($"{appKey}"))
  153. // {
  154. // jsonObject["DingDingAuth"]["appKey"] = $"{appKey}";
  155. // }
  156. // if (!string.IsNullOrEmpty($"{appSecret}"))
  157. // {
  158. // jsonObject["DingDingAuth"]["appSecret"] = $"{appSecret}";
  159. // }
  160. // if (!string.IsNullOrEmpty($"{storage}"))
  161. // {
  162. // jsonObject["Azure"]["Storage"]["ConnectionString"] = $"{storage}";
  163. // }
  164. // if (!string.IsNullOrEmpty($"{cosmosDb}"))
  165. // {
  166. // jsonObject["Azure"]["Cosmos"]["ConnectionString"] = $"{cosmosDb}";
  167. // }
  168. // if (!string.IsNullOrEmpty($"{redis}"))
  169. // {
  170. // jsonObject["Azure"]["Redis"]["ConnectionString"] = $"{redis}";
  171. // }
  172. // if (!string.IsNullOrEmpty($"{serviceBus}"))
  173. // {
  174. // jsonObject["Azure"]["ServiceBus"]["ConnectionString"] = $"{serviceBus}";
  175. // }
  176. // break;
  177. }
  178. try
  179. {
  180. reader.Close();
  181. string output = JsonConvert.SerializeObject(jsonObject, Formatting.Indented); //是否json文件有缩进
  182. System.IO.File.WriteAllText(formalDeploy, output);
  183. readerDeve.Close();
  184. string outputDeve = JsonConvert.SerializeObject(jsonObjectDeve, Formatting.Indented); //是否json文件有缩进
  185. System.IO.File.WriteAllText(extendDeploy, outputDeve);
  186. }
  187. catch (Exception ex)
  188. {
  189. await _dingDing.SendBotMsg($"BI,{_option.Location} /syscfg/set-config \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  190. return Ok(new { state = 403, msg = $"保存失败!" });
  191. }
  192. ////方式一 无排版 注释全无
  193. //JObject jsonObject;
  194. //using (StreamReader file = new StreamReader(formalDeploy))
  195. //using (JsonTextReader reader = new JsonTextReader(file))
  196. //{
  197. // jsonObject = (JObject)JToken.ReadFrom(reader);
  198. // jsonObject["DingDingAuth"]["getuserinfo_bycode"] = "asdasdasdasd";
  199. //}
  200. //using (var writer = new StreamWriter(formalDeploy))
  201. //using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
  202. //{
  203. // jsonObject.WriteTo(jsonwriter);
  204. //}
  205. return Ok(new { state = 200 });
  206. }
  207. catch (Exception ex)
  208. {
  209. await _dingDing.SendBotMsg($"BI,{_option.Location} /syscfg/set-config \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  210. return BadRequest();
  211. }
  212. }
  213. }
  214. }