CoreController.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using Microsoft.AspNetCore.Mvc;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Runtime.InteropServices;
  5. using System.Text.Json;
  6. using System.Threading.Tasks;
  7. using System;
  8. using TEAMModelOS.SDK;
  9. using System.IO;
  10. using TEAMModelOS.SDK.Models;
  11. using TEAMModelOS.SDK.DI;
  12. using Microsoft.Extensions.Options;
  13. using System.Net.Http;
  14. using TEAMModelOS.Models;
  15. using static TEAMModelOS.SDK.CoreAPIHttpService;
  16. using System.Collections.Generic;
  17. using System.IdentityModel.Tokens.Jwt;
  18. using System.Text;
  19. using System.Web;
  20. using TEAMModelOS.SDK.Extension;
  21. using System.Net;
  22. using Microsoft.Extensions.Configuration;
  23. using System.Configuration;
  24. using TEAMModelOS.SDK.Models.Cosmos.BI;
  25. namespace TEAMModelBI.Controllers.BISystem
  26. {
  27. [Route("core")]
  28. [ApiController]
  29. public class CoreController : ControllerBase
  30. {
  31. private readonly AzureCosmosFactory _azureCosmos;
  32. private readonly AzureRedisFactory _azureRedis;
  33. private readonly AzureStorageFactory _azureStorage;
  34. private readonly DingDing _dingDing;
  35. private readonly IPSearcher _searcher;
  36. private readonly CoreAPIHttpService _coreAPIHttpService;
  37. private readonly IConfiguration _configuration;
  38. private readonly Option _option;
  39. public CoreController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, CoreAPIHttpService coreAPIHttpService, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, IPSearcher searcher, IHttpClientFactory httpClient)
  40. {
  41. _azureCosmos = azureCosmos;
  42. _azureRedis = azureRedis;
  43. _coreAPIHttpService = coreAPIHttpService;
  44. _configuration = configuration;
  45. _azureStorage = azureStorage;
  46. _dingDing = dingDing;
  47. _searcher = searcher;
  48. _option = option?.Value;
  49. }
  50. /// <summary>
  51. ///获取版本信息
  52. /// </summary>
  53. /// <param name="request"></param>
  54. /// <returns></returns>
  55. [HttpPost("system-info")]
  56. [RequestSizeLimit(100_000_000)] //最大100m左右
  57. public async Task<IActionResult> SystemInfo(JsonElement request)
  58. {
  59. Type attr = this.GetType();
  60. string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location);
  61. Assembly assembly = Assembly.LoadFrom($"{currentDirectory}\\TEAMModelBI.dll");
  62. var description = assembly.GetCustomAttribute<AssemblyDescriptionAttribute>().Description;
  63. //var v1 = Assembly.GetEntryAssembly().GetName().Version;
  64. //var v2 = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
  65. // var description = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyDescriptionAttribute>().Description;
  66. var version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
  67. long nowtime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  68. //Console.WriteLine($"Assembly.GetEntryAssembly().GetName().Version: " +
  69. // $"{Assembly.GetEntryAssembly().GetName().Version}");5.2107.12.1
  70. //Console.WriteLine($"Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version:" +
  71. // $"{Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version}");5.2107.12.1
  72. //Console.WriteLine($"Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion:" +
  73. // $"{Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion}");5.2107.12
  74. var IpPort = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
  75. if (string.IsNullOrEmpty(IpPort))
  76. {
  77. IpPort = HttpContext.Connection.RemoteIpAddress.ToString();
  78. }
  79. if (IpPort.Contains("::"))
  80. {
  81. IpPort = "127.0.0.1";
  82. }
  83. string ip = IpPort.Split(":")[0];
  84. string os = "";
  85. if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
  86. {//Linux
  87. os = "Linux";
  88. }
  89. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  90. {//Windows
  91. os = "Windows";
  92. }
  93. else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  94. {//OSX
  95. os = "OSX";
  96. }
  97. string region = await _searcher.SearchIpAsync(ip);
  98. return Ok(new { os, version, description, nowtime, region, ip });
  99. }
  100. public class NotifyData
  101. {
  102. public string notifyCode { get; set; }
  103. public string data { get; set; }
  104. public int notifyEvent { get; set; }
  105. public string ticket { get; set; }
  106. public string token { get; set; }
  107. }
  108. /// <summary>
  109. /// 处理信息
  110. /// </summary>
  111. /// <param name="notifyData"></param>
  112. /// <returns></returns>
  113. [HttpGet("process-notify")]
  114. public async Task<IActionResult> ProcessNotify([FromQuery] NotifyData notifyData)
  115. {
  116. string msg = "";
  117. /// code =-1 参数异常,0 凭据失效 1不是管理员,2操作成功,3服务端异常
  118. int code = -1;
  119. string scope = "";
  120. //string lang = _option.Location.Contains("China") ? "zh-cn" : "en-us";
  121. //string path = Path.Combine("", $"Lang/{lang}.json");
  122. //var jsonAuth = System.IO.File.ReadAllText(path, Encoding.UTF8);
  123. //var jsonData = jsonAuth.ToObject<JsonElement>();
  124. //var status = jsonData.GetProperty("notify-status");
  125. var status = "";
  126. try
  127. {
  128. string opttmdid = "";
  129. if (!string.IsNullOrWhiteSpace(notifyData.ticket))
  130. {
  131. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  132. (HttpStatusCode statusCode, CoreAPIToken token) = await _coreAPIHttpService.GetCoreAPIoAuth2Token(
  133. new Dictionary<string, object> { { "client_id", clientID }, { "grant_type", "authorization_code" }, { "code", notifyData.ticket } }, _option.Location, _configuration, _dingDing);
  134. if (statusCode.Equals(HttpStatusCode.OK))
  135. {
  136. var jwt = new JwtSecurityToken(token.id_token);
  137. opttmdid = jwt.Payload.Sub;
  138. }
  139. else
  140. {
  141. code = 0;
  142. msg = "凭据失效";
  143. }
  144. scope = "HiTA";
  145. }
  146. else if (!string.IsNullOrWhiteSpace(notifyData.token) && string.IsNullOrWhiteSpace(opttmdid))
  147. {
  148. var jwt = new JwtSecurityToken(notifyData.token);
  149. opttmdid = jwt.Payload.Sub;
  150. scope = "BI";
  151. }
  152. if (string.IsNullOrWhiteSpace(opttmdid))
  153. {
  154. code = 1;
  155. msg = "参数异常";
  156. //msg = status.TryGetProperty($"code{code}", out JsonElement text) ? $"{text}" : msg;
  157. }
  158. var rdata = HttpUtility.UrlDecode(notifyData.data, Encoding.UTF8);
  159. var dataByte = Convert.FromBase64String(rdata);
  160. string data = Encoding.Unicode.GetString(dataByte);
  161. Dictionary<string, object> dict = data.ToObject<Dictionary<string, object>>();
  162. dict.TryGetValue("notifyCode", out object notifyCode); //消息类型
  163. dict.TryGetValue("noticeId", out object noticeId); //存在BI中的消息id
  164. dict.TryGetValue("tmdid", out object _tmdid); //发送人ID
  165. dict.TryGetValue("sendId", out object _sendId); //接收人群
  166. switch ($"{notifyCode}")
  167. {
  168. case "notify_agree": //同意拒绝
  169. if (notifyData.notifyEvent == 1)
  170. {
  171. code = 1;
  172. msg = "操作成功";
  173. }
  174. if (notifyData.notifyEvent == 2)
  175. {
  176. code = 1;
  177. msg = "操作成功";
  178. }
  179. break;
  180. case "notify_see": //查看
  181. code = 2;
  182. msg = "查看成功";
  183. break;
  184. default: //默认
  185. code = 0;
  186. msg = "普通通知";
  187. break;
  188. }
  189. }
  190. catch (Exception ex)
  191. {
  192. await _dingDing.SendBotMsg($"{ex.Message}\n{ex.StackTrace}\n{notifyData.ToJsonString()}", GroupNames.成都开发測試群組);
  193. code = 4;
  194. msg = "服务端异常";
  195. }
  196. if (scope.Equals("HiTA"))
  197. {
  198. string HostName = HttpContext.GetHostName();
  199. var rurl = $"https://{HostName}/resultPage?code={code}&msg={HttpUtility.UrlEncode(msg)}";
  200. return Redirect(rurl);
  201. }
  202. else
  203. {
  204. return Ok(new { code, msg });
  205. }
  206. }
  207. }
  208. }