CoreController.cs 9.5 KB

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