123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.IO;
- using System.Collections.Generic;
- using Microsoft.Extensions.Configuration;
- using TEAMModelOS.Models;
- using Microsoft.Extensions.Options;
- using TEAMModelBI.Tool.Extension;
- using TEAMModelOS.SDK.DI;
- using TEAMModelBI.Filter;
- namespace TEAMModelBI.Controllers.DingDingStruc
- {
- [Route("syscfg")]
- [ApiController]
- public class SystemConfigController : ControllerBase
- {
- private readonly IConfiguration _configuration;
- private readonly IWebHostEnvironment _hostingEnvironment; //读取文件
- private readonly Option _option;
- //钉钉提示信息
- private readonly DingDing _dingDing;
- public SystemConfigController(IConfiguration configuration,IWebHostEnvironment hostingEnvironment, IOptionsSnapshot<Option> option, DingDing dingDing)
- {
- _configuration = configuration;
- _hostingEnvironment = hostingEnvironment;
- _option = option?.Value;
- _dingDing = dingDing;
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- [HttpPost("get-config")]
- public async Task<IActionResult> GetConfigJson()
- {
- //string SiteScope1 = _configuration["CustomParam:SiteScope"];
- string SiteScope = _option.Location;
- string proDeptId = _configuration["CustomParam:proDeptId"];
- string ddAgentld = _configuration["DingDingAuth:Agentld"];
- string ddAppKey = _configuration["DingDingAuth:appKey"];
- string ddAppSecret = _configuration["DingDingAuth:appSecret"];
- string storage = _configuration["Azure:Storage:ConnectionString"];
- string cosmos = _configuration["Azure:Cosmos:ConnectionString"];
- string redis = _configuration["Azure:Redis:ConnectionString"];
- string serviceBus = _configuration["Azure:ServiceBus:ConnectionString"];
- return Ok(new { state = 200, SiteScope, proDeptId, ddAgentld, ddAppKey, ddAppSecret, storage, cosmos, redis, serviceBus });
- }
- /// <summary>
- /// 配置文件修改
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin")]
- [HttpPost("set-config")]
- public async Task<IActionResult> SetConfigJson(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("busy", out JsonElement busy)) return BadRequest();
- jsonElement.TryGetProperty("site", out JsonElement site);
- jsonElement.TryGetProperty("proDeptId", out JsonElement proDeptId);
- jsonElement.TryGetProperty("appId", out JsonElement appId);
- jsonElement.TryGetProperty("appKey", out JsonElement appKey);
- jsonElement.TryGetProperty("appSecret", out JsonElement appSecret);
- jsonElement.TryGetProperty("storage", out JsonElement storage);
- jsonElement.TryGetProperty("cosmosDb", out JsonElement cosmosDb);
- jsonElement.TryGetProperty("redis", out JsonElement redis);
- jsonElement.TryGetProperty("serviceBus", out JsonElement serviceBus);
- var formalDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.json";
- var extendDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.Development.json";
- //方式二 有排版 注释全无
- StreamReader reader = System.IO.File.OpenText(formalDeploy);
- JsonTextReader jsonTextReader = new(reader);
- JObject jsonObject = (JObject)JToken.ReadFrom(jsonTextReader);
- StreamReader readerDeve = System.IO.File.OpenText(extendDeploy);
- JsonTextReader jsonTextReaderDeve = new(readerDeve);
- JObject jsonObjectDeve = (JObject)JToken.ReadFrom(jsonTextReaderDeve);
- switch ($"{busy}")
- {
- case "website":
- if (!string.IsNullOrEmpty($"{site}"))
- {
- jsonObject["Option"]["Location"] = $"{site}";
- jsonObjectDeve["Option"]["Location"] = $"{site}";
- }
- if (!string.IsNullOrEmpty($"{proDeptId}"))
- {
- jsonObject["CustomParam"]["proDeptId"] = $"{proDeptId}";
- jsonObjectDeve["CustomParam"]["proDeptId"] = $"{proDeptId}";
- }
- break;
- case "dd":
- if (!string.IsNullOrEmpty($"{appId}"))
- {
- jsonObject["DingDingAuth"]["Agentld"] = $"{appId}";
- jsonObjectDeve["DingDingAuth"]["Agentld"] = $"{appId}";
- }
- if (!string.IsNullOrEmpty($"{appKey}"))
- {
- jsonObject["DingDingAuth"]["appKey"] = $"{appKey}";
- jsonObjectDeve["DingDingAuth"]["appKey"] = $"{appKey}";
- }
- if (!string.IsNullOrEmpty($"{appSecret}"))
- {
- jsonObject["DingDingAuth"]["appSecret"] = $"{appSecret}";
- jsonObjectDeve["DingDingAuth"]["appSecret"] = $"{appSecret}";
- }
- break;
- case "dbs":
- if (!string.IsNullOrEmpty($"{storage}"))
- {
- jsonObject["Azure"]["Storage"]["ConnectionString"] = $"{storage}";
- jsonObjectDeve["Azure"]["Storage"]["ConnectionString"] = $"{storage}";
- }
- if (!string.IsNullOrEmpty($"{cosmosDb}"))
- {
- jsonObject["Azure"]["Cosmos"]["ConnectionString"] = $"{cosmosDb}";
- jsonObjectDeve["Azure"]["Cosmos"]["ConnectionString"] = $"{cosmosDb}";
- }
- if (!string.IsNullOrEmpty($"{redis}"))
- {
- jsonObject["Azure"]["Redis"]["ConnectionString"] = $"{redis}";
- jsonObjectDeve["Azure"]["Redis"]["ConnectionString"] = $"{redis}";
- }
- if (!string.IsNullOrEmpty($"{serviceBus}"))
- {
- jsonObject["Azure"]["ServiceBus"]["ConnectionString"] = $"{serviceBus}";
- jsonObjectDeve["Azure"]["ServiceBus"]["ConnectionString"] = $"{serviceBus}";
- }
- break;
- //default:
- // if (!string.IsNullOrEmpty($"{site}"))
- // {
- // jsonObject["Option"]["Location"] = $"{site}";
- // }
- // if (!string.IsNullOrEmpty($"{proDeptId}"))
- // {
- // jsonObject["CustomParam"]["proDeptId"] = $"{proDeptId}";
- // }
- // if (!string.IsNullOrEmpty($"{appId}"))
- // {
- // jsonObject["DingDingAuth"]["Agentld"] = $"{appId}";
- // }
- // if (!string.IsNullOrEmpty($"{appKey}"))
- // {
- // jsonObject["DingDingAuth"]["appKey"] = $"{appKey}";
- // }
- // if (!string.IsNullOrEmpty($"{appSecret}"))
- // {
- // jsonObject["DingDingAuth"]["appSecret"] = $"{appSecret}";
- // }
- // if (!string.IsNullOrEmpty($"{storage}"))
- // {
- // jsonObject["Azure"]["Storage"]["ConnectionString"] = $"{storage}";
- // }
- // if (!string.IsNullOrEmpty($"{cosmosDb}"))
- // {
- // jsonObject["Azure"]["Cosmos"]["ConnectionString"] = $"{cosmosDb}";
- // }
- // if (!string.IsNullOrEmpty($"{redis}"))
- // {
- // jsonObject["Azure"]["Redis"]["ConnectionString"] = $"{redis}";
- // }
- // if (!string.IsNullOrEmpty($"{serviceBus}"))
- // {
- // jsonObject["Azure"]["ServiceBus"]["ConnectionString"] = $"{serviceBus}";
- // }
- // break;
- }
- try
- {
- reader.Close();
- string output = JsonConvert.SerializeObject(jsonObject, Formatting.Indented); //是否json文件有缩进
- System.IO.File.WriteAllText(formalDeploy, output);
- readerDeve.Close();
- string outputDeve = JsonConvert.SerializeObject(jsonObjectDeve, Formatting.Indented); //是否json文件有缩进
- System.IO.File.WriteAllText(extendDeploy, outputDeve);
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /syscfg/set-config \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
- return Ok(new { state = 403, msg = $"保存失败!" });
- }
- ////方式一 无排版 注释全无
- //JObject jsonObject;
- //using (StreamReader file = new StreamReader(formalDeploy))
- //using (JsonTextReader reader = new JsonTextReader(file))
- //{
- // jsonObject = (JObject)JToken.ReadFrom(reader);
- // jsonObject["DingDingAuth"]["getuserinfo_bycode"] = "asdasdasdasd";
- //}
- //using (var writer = new StreamWriter(formalDeploy))
- //using (JsonTextWriter jsonwriter = new JsonTextWriter(writer))
- //{
- // jsonObject.WriteTo(jsonwriter);
- //}
- return Ok(new { state = 200 });
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} /syscfg/set-config \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- }
- }
|