123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- using Microsoft.AspNetCore.Mvc;
- using System.Linq;
- using System.Reflection;
- using System.Runtime.InteropServices;
- using System.Text.Json;
- using System.Threading.Tasks;
- using System;
- using TEAMModelOS.SDK;
- using System.IO;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK.DI;
- using Microsoft.Extensions.Options;
- using System.Net.Http;
- using TEAMModelOS.Models;
- using static TEAMModelOS.SDK.CoreAPIHttpService;
- using System.Collections.Generic;
- using System.IdentityModel.Tokens.Jwt;
- using System.Text;
- using System.Web;
- using TEAMModelOS.SDK.Extension;
- using System.Net;
- using Microsoft.Extensions.Configuration;
- using System.Configuration;
- using TEAMModelOS.SDK.Models.Cosmos.BI;
- using TEAMModelOS.SDK.Context.Constant;
- namespace TEAMModelBI.Controllers.BISystem
- {
- [Route("core")]
- [ApiController]
- public class CoreController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly AzureRedisFactory _azureRedis;
- private readonly AzureStorageFactory _azureStorage;
- private readonly DingDing _dingDing;
- private readonly IPSearcher _searcher;
- private readonly CoreAPIHttpService _coreAPIHttpService;
- private readonly IConfiguration _configuration;
- private readonly Option _option;
- public CoreController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, CoreAPIHttpService coreAPIHttpService, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, IPSearcher searcher, IHttpClientFactory httpClient)
- {
- _azureCosmos = azureCosmos;
- _azureRedis = azureRedis;
- _coreAPIHttpService = coreAPIHttpService;
- _configuration = configuration;
- _azureStorage = azureStorage;
- _dingDing = dingDing;
- _searcher = searcher;
- _option = option?.Value;
- }
- /// <summary>
- ///获取版本信息
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("system-info")]
- [RequestSizeLimit(100_000_000)] //最大100m左右
- public async Task<IActionResult> SystemInfo(JsonElement request)
- {
- Type attr = this.GetType();
- string currentDirectory = Path.GetDirectoryName(attr.Assembly.Location);
- Assembly assembly = Assembly.LoadFrom($"{currentDirectory}\\TEAMModelBI.dll");
- var description = assembly.GetCustomAttribute<AssemblyDescriptionAttribute>().Description;
- //var v1 = Assembly.GetEntryAssembly().GetName().Version;
- //var v2 = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
- // var description = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyDescriptionAttribute>().Description;
- var version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version;
- long nowtime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- //Console.WriteLine($"Assembly.GetEntryAssembly().GetName().Version: " +
- // $"{Assembly.GetEntryAssembly().GetName().Version}");5.2107.12.1
- //Console.WriteLine($"Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version:" +
- // $"{Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>().Version}");5.2107.12.1
- //Console.WriteLine($"Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion:" +
- // $"{Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion}");5.2107.12
- var IpPort = HttpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault();
- if (string.IsNullOrEmpty(IpPort))
- {
- IpPort = HttpContext.Connection.RemoteIpAddress.ToString();
- }
- if (IpPort.Contains("::"))
- {
- IpPort = "127.0.0.1";
- }
- string ip = IpPort.Split(":")[0];
- string os = "";
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
- {//Linux
- os = "Linux";
- }
- else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
- {//Windows
- os = "Windows";
- }
- else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
- {//OSX
- os = "OSX";
- }
- string region = await _searcher.SearchIpAsync(ip);
- return Ok(new { os, version, description, nowtime, region, ip });
- }
- public class NotifyData
- {
- public string notifyCode { get; set; }
- public string data { get; set; }
- public int notifyEvent { get; set; }
- public string ticket { get; set; }
- public string token { get; set; }
- }
- /// <summary>
- /// 处理信息
- /// </summary>
- /// <param name="notifyData"></param>
- /// <returns></returns>
- [HttpGet("process-notify")]
- public async Task<IActionResult> ProcessNotify([FromQuery] NotifyData notifyData)
- {
- string msg = "";
- /// code =-1 参数异常,0 凭据失效 1不是管理员,2操作成功,3服务端异常
- int code = -1;
- string scope = "";
- //string lang = _option.Location.Contains("China") ? "zh-cn" : "en-us";
- //string path = Path.Combine("", $"Lang/{lang}.json");
- //var jsonAuth = System.IO.File.ReadAllText(path, Encoding.UTF8);
- //var jsonData = jsonAuth.ToObject<JsonElement>();
- //var status = jsonData.GetProperty("notify-status");
- var status = "";
- try
- {
- string opttmdid = "";
- if (!string.IsNullOrWhiteSpace(notifyData.ticket))
- {
- var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
- (HttpStatusCode statusCode, CoreAPIToken token) = await _coreAPIHttpService.GetCoreAPIoAuth2Token(
- new Dictionary<string, object> { { "client_id", clientID }, { "grant_type", "authorization_code" }, { "code", notifyData.ticket } }, _option.Location, _configuration, _dingDing);
- if (statusCode.Equals(HttpStatusCode.OK))
- {
- var jwt = new JwtSecurityToken(token.id_token);
- opttmdid = jwt.Payload.Sub;
- }
- else
- {
- code = 0;
- msg = "凭据失效";
- }
- scope = "HiTA";
- }
- else if (!string.IsNullOrWhiteSpace(notifyData.token) && string.IsNullOrWhiteSpace(opttmdid))
- {
- var jwt = new JwtSecurityToken(notifyData.token);
- opttmdid = jwt.Payload.Sub;
- scope = "BI";
- }
- if (string.IsNullOrWhiteSpace(opttmdid))
- {
- code = 400;
- msg = "参数异常";
- //msg = status.TryGetProperty($"code{code}", out JsonElement text) ? $"{text}" : msg;
- }
- var rdata = HttpUtility.UrlDecode(notifyData.data, Encoding.UTF8);
- var dataByte = Convert.FromBase64String(rdata);
- string data = Encoding.Unicode.GetString(dataByte);
- Dictionary<string, object> dict = data.ToObject<Dictionary<string, object>>();
- dict.TryGetValue("notifyCode", out object notifyCode); //消息类型
- dict.TryGetValue("noticeId", out object noticeId); //存在BI中的消息id
- dict.TryGetValue("tmdid", out object _tmdid); //发送人ID
- dict.TryGetValue("sendId", out object _sendId); //接收人群
- switch ($"{notifyCode}")
- {
- case "notify_agree": //同意拒绝
- if (notifyData.notifyEvent == 1)
- {
- code = 1;
- msg = "操作成功";
- }
- if (notifyData.notifyEvent == 2)
- {
- code = 2;
- msg = "操作成功";
- }
- break;
- case "notify_see": //查看
- code = 1;
- msg = "查看成功";
- break;
- default: //默认
- code = 3;
- msg = "普通通知";
- break;
- }
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"{ex.Message}\n{ex.StackTrace}\n{notifyData.ToJsonString()}", GroupNames.成都开发測試群組);
- code = 500;
- msg = "服务端异常";
- }
- if (scope.Equals("HiTA"))
- {
- string HostName = HttpContext.GetHostName();
- var rurl = $"https://{HostName}/#/resultPage?code={code}&msg={HttpUtility.UrlEncode(msg)}";
- return Redirect(rurl);
- }
- else
- {
- return Ok(new { code, msg });
- }
- }
- }
- }
|