123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- 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;
- using TEAMModelOS.SDK.Extension;
- using System.Linq;
- using TEAMModelBI.Models;
- 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,rdc")]
- [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}\n{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}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- /// <summary>
- /// 获取所有的应用链接秘钥,以及当前应用链接秘钥 //已对接
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc")]
- [HttpPost("get-allconfig")]
- public async Task<IActionResult> GetAllConfig()
- {
- var builder = $"{_hostingEnvironment.ContentRootPath}/JsonFile/SystemConfig.json";
- StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
- StringBuilder stringBuilder = new();
- string text;
- while ((text = streamReader.ReadLine()) != null)
- {
- stringBuilder.Append(text.ToString());
- }
- string input = stringBuilder.ToString();
- List<SysConfig> allConfigs = input.ToObject<List<SysConfig>>();
- var currentSite = _option.Location;
- SysConfig currentSysConfig = new();
- try
- {
- currentSysConfig = allConfigs.Where(x => x.site.Equals(currentSite)).FirstOrDefault();
- }
- catch{}
- if (currentSysConfig != null)
- {
- return Ok(new { state = 200, allConfigs, currentSysConfig });
- }
- else {
- currentSysConfig = new SysConfig();
- currentSysConfig.site = $"{currentSite}";
- currentSysConfig.nickName = $"{currentSite}";
- currentSysConfig.proDeptId = long.Parse(_configuration["CustomParam:proDeptId"]);
- currentSysConfig.clientKey = new()
- {
- clientID = _configuration["HaBookAuth:CoreService:clientID"],
- clientSecret = _configuration["HaBookAuth:CoreService:clientSecret"]
- };
- currentSysConfig.dDAuth = new()
- {
- agentId = _configuration["DingDingAuth:Agentld"],
- appKey = _configuration["DingDingAuth:appKey"],
- appSecret = _configuration["DingDingAuth:appSecret"]
- };
- currentSysConfig.azureClient = new()
- {
- storage = _configuration["Azure:Storage:ConnectionString"],
- cosmos = _configuration["Azure:Cosmos:ConnectionString"],
- redis = _configuration["Azure:Redis:ConnectionString"],
- servicBus = _configuration["Azure:ServiceBus:ConnectionString"]
- };
- return Ok(new { state = 201, allConfigs, currentSysConfig });
- }
- }
- /// <summary>
- /// 修改或者添加配置文件,到多个配置文件中 //已对接
- /// </summary>
- /// <param name="sysConfig"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc")]
- [HttpPost("set-singleconfig")]
- public async Task<IActionResult> SetSingleConfig(SysConfig sysConfig)
- {
- var builder = $"{_hostingEnvironment.ContentRootPath}/JsonFile/SystemConfig.json";
- StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
- StringBuilder stringBuilder = new();
- string text;
- while ((text = streamReader.ReadLine()) != null)
- {
- stringBuilder.Append(text.ToString());
- }
- string input = stringBuilder.ToString();
- List<SysConfig> allConfigs = new();
- if (!string.IsNullOrEmpty(input))
- {
- try
- {
- allConfigs = input.ToObject<List<SysConfig>>();
- }
- catch
- {
- allConfigs.Add(sysConfig);
- }
- var tempSysConfig = allConfigs.Where(x => x.site.Equals(sysConfig.site)).FirstOrDefault();
- if (tempSysConfig != null)
- {
- tempSysConfig.site = sysConfig.site;
- tempSysConfig.nickName = sysConfig.nickName;
- tempSysConfig.proDeptId = sysConfig.proDeptId;
- tempSysConfig.clientKey = sysConfig.clientKey;
- tempSysConfig.dDAuth = sysConfig.dDAuth;
- tempSysConfig.azureClient = sysConfig.azureClient;
- }
- else
- {
- allConfigs.Add(sysConfig);
- }
- }
- else
- {
- allConfigs.Add(sysConfig);
- }
- string ListJson = JsonConvert.SerializeObject(allConfigs, Formatting.Indented);
- System.IO.File.WriteAllText(builder, ListJson);
- return Ok(new { state = 200, allConfigs });
- }
- /// <summary>
- /// 切换站点 //已对接
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc")]
- [HttpPost("cut-site")]
- public async Task<IActionResult> CutSite(JsonElement jsonElement)
- {
- if (!jsonElement.TryGetProperty("site", out JsonElement site)) return BadRequest();
- var builder = $"{_hostingEnvironment.ContentRootPath}/JsonFile/SystemConfig.json";
- var formalDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.json";
- var extendDeploy = $"{_hostingEnvironment.ContentRootPath}/appsettings.Development.json";
- StreamReader streamReader = new(new FileStream(builder, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8);
- StringBuilder stringBuilder = new();
- string text;
- while ((text = streamReader.ReadLine()) != null)
- {
- stringBuilder.Append(text.ToString());
- }
- string input = stringBuilder.ToString();
- List<SysConfig> allConfigs = input.ToObject<List<SysConfig>>();
- var tempSysConfig = allConfigs.Where(x => x.site.Equals($"{site}")).FirstOrDefault();
- // 有排版 注释全无
- 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);
- if (tempSysConfig != null)
- {
- jsonObject["Option"]["Location"] = $"{site}";
- jsonObject["CustomParam"]["proDeptId"] = $"{tempSysConfig.proDeptId}";
- jsonObjectDeve["Option"]["Location"] = $"{site}";
- jsonObjectDeve["CustomParam"]["proDeptId"] = $"{tempSysConfig.proDeptId}";
- jsonObject["HaBookAuth"]["CoreService"]["clientID"] = tempSysConfig.clientKey.clientID;
- jsonObject["HaBookAuth"]["CoreService"]["clientSecret"] = tempSysConfig.clientKey.clientSecret;
- jsonObjectDeve["HaBookAuth"]["CoreService"]["clientID"] = tempSysConfig.clientKey.clientID;
- jsonObjectDeve["HaBookAuth"]["CoreService"]["clientSecret"] = tempSysConfig.clientKey.clientSecret;
- jsonObject["DingDingAuth"]["Agentld"] = $"{tempSysConfig.dDAuth.agentId}";
- jsonObject["DingDingAuth"]["appKey"] = $"{tempSysConfig.dDAuth.appKey}";
- jsonObject["DingDingAuth"]["appSecret"] = $"{tempSysConfig.dDAuth.appSecret}";
- jsonObjectDeve["DingDingAuth"]["Agentld"] = $"{tempSysConfig.dDAuth.agentId}";
- jsonObjectDeve["DingDingAuth"]["appKey"] = $"{tempSysConfig.dDAuth.appKey}";
- jsonObjectDeve["DingDingAuth"]["appSecret"] = $"{tempSysConfig.dDAuth.appSecret}";
- jsonObject["Azure"]["Storage"]["ConnectionString"] = $"{ tempSysConfig.azureClient.storage}";
- jsonObject["Azure"]["Cosmos"]["ConnectionString"] = $"{tempSysConfig.azureClient.cosmos}";
- jsonObject["Azure"]["Redis"]["ConnectionString"] = $"{tempSysConfig.azureClient.redis}";
- jsonObject["Azure"]["ServiceBus"]["ConnectionString"] = $"{tempSysConfig.azureClient.servicBus}";
- jsonObjectDeve["Azure"]["Storage"]["ConnectionString"] = $"{ tempSysConfig.azureClient.storage}";
- jsonObjectDeve["Azure"]["Cosmos"]["ConnectionString"] = $"{tempSysConfig.azureClient.cosmos}";
- jsonObjectDeve["Azure"]["Redis"]["ConnectionString"] = $"{tempSysConfig.azureClient.redis}";
- jsonObjectDeve["Azure"]["ServiceBus"]["ConnectionString"] = $"{tempSysConfig.azureClient.servicBus}";
- }
- else return Ok(new { state = 404,msg = "未找到你需要的参数" });
- 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);
- return Ok(new { state = 200, SysConfig = tempSysConfig });
- }
- }
- }
|