Pārlūkot izejas kodu

按钮跳转处理

Li 2 gadi atpakaļ
vecāks
revīzija
cedcbb42ae
1 mainītis faili ar 130 papildinājumiem un 2 dzēšanām
  1. 130 2
      TEAMModelBI/Controllers/BISystem/CoreController.cs

+ 130 - 2
TEAMModelBI/Controllers/BISystem/CoreController.cs

@@ -12,6 +12,16 @@ 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;
 
 namespace TEAMModelBI.Controllers.BISystem
 {
@@ -25,14 +35,18 @@ namespace TEAMModelBI.Controllers.BISystem
         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, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, IPSearcher searcher, IHttpClientFactory httpClient)
+        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;
-            _azureStorage= azureStorage;
+            _coreAPIHttpService = coreAPIHttpService;
+            _configuration = configuration;
+            _azureStorage = azureStorage;
             _dingDing = dingDing;
             _searcher = searcher;
             _option = option?.Value;
@@ -94,5 +108,119 @@ namespace TEAMModelBI.Controllers.BISystem
 
             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 = 1;
+                    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 = 1;
+                            msg = "操作成功";
+                        }
+                        break;
+                    case "notify_see":  //查看
+                        code = 2;
+                        msg = "查看成功";
+                        break;
+                    default:  //默认
+                        code = 0;
+                        msg = "普通通知";
+                        break;
+                }
+
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"{ex.Message}\n{ex.StackTrace}\n{notifyData.ToJsonString()}", GroupNames.成都开发測試群組);
+                code = 4;
+                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 });
+            }
+        }
+
     }
 }