CrazyIter_Bin 3 年之前
父节点
当前提交
e8c7bea1ab
共有 2 个文件被更改,包括 129 次插入28 次删除
  1. 9 5
      TEAMModelOS.SDK/DI/HttpTrigger/HttpTrigger.cs
  2. 120 23
      TEAMModelOS/Controllers/Knowledge/KnowledgesController.cs

+ 9 - 5
TEAMModelOS.SDK/DI/HttpTrigger/HttpTrigger.cs

@@ -1,4 +1,5 @@
-using Newtonsoft.Json.Linq;
+
+using Newtonsoft.Json.Linq;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
@@ -30,13 +31,13 @@ namespace TEAMModelOS.SDK.DI
         /// <param name="secret">加簽密鑰</param>
         /// <param name="msg">發送訊息</param>
         /// <returns></returns>
-        public async Task<(int status,string json)> RequestHttpTrigger(object data, string location,string url)
+        public async Task<(int status, string json)> RequestHttpTrigger(object data, string location, string url)
         {
             var keys = HttpTriggerUrl.HttpTrigger地址.GetDescriptionText().Split(',');
             string domain = "";
             if (location.Equals("China-Dep"))
             {
-                domain = keys[0];
+                domain = keys[1];
             }
             else if (location.Equals("China-Test"))
             {
@@ -59,10 +60,11 @@ namespace TEAMModelOS.SDK.DI
                 domain = keys[5];
             }
             string link = domain.Contains("localhost") ? $"http://{domain}/api/{url}" : $"http://{domain}/api/{url}";
-            HttpResponseMessage responseMessage =await _httpClient.PostAsJsonAsync(link, data);
+            HttpContent httpContent = new StringContent(data.ToJsonString());
+            HttpResponseMessage responseMessage = await _httpClient.PostAsync(link, httpContent);
             if (responseMessage.StatusCode == HttpStatusCode.OK)
             {
-                string Content= await responseMessage.Content.ReadAsStringAsync();
+                string Content = await responseMessage.Content.ReadAsStringAsync();
                 Content.ToObject<JsonElement>().TryGetProperty("data", out JsonElement content);
                 return (200, $"{content}");
             }
@@ -80,3 +82,5 @@ namespace TEAMModelOS.SDK.DI
 
     }
 }
+
+

+ 120 - 23
TEAMModelOS/Controllers/Knowledge/KnowledgesController.cs

@@ -26,7 +26,7 @@ namespace TEAMModelOS.Controllers
 {
     [ProducesResponseType(StatusCodes.Status200OK)]
     [ProducesResponseType(StatusCodes.Status400BadRequest)]
-    
+
     [Route("knowledges")]
     [ApiController]
     public class KnowledgesController : ControllerBase
@@ -46,7 +46,7 @@ namespace TEAMModelOS.Controllers
             _azureRedis = azureRedis;
             _httpTrigger = httpTrigger;
         }
-       
+
 
         /**
          * 
@@ -88,7 +88,8 @@ namespace TEAMModelOS.Controllers
         [HttpPost("upsert")]
         [Authorize(Roles = "IES")]
         [AuthToken(Roles = "admin", Permissions = "knowledge-upd")]
-        public async Task<IActionResult> Upsert(Knowledge knowledge) {
+        public async Task<IActionResult> Upsert(Knowledge knowledge)
+        {
             var client = _azureCosmos.GetCosmosClient();
             knowledge.code = $"Knowledge-{knowledge.owner}-{knowledge.subjectId}";
             StringBuilder sql = new StringBuilder($"select value(c) from c where c.periodId = '{knowledge.periodId}'");
@@ -103,7 +104,8 @@ namespace TEAMModelOS.Controllers
                 knowledge.id = old.id;
                 knowledge = await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(knowledge, old.id, new PartitionKey($"{knowledge.code}"));
             }
-            else {
+            else
+            {
                 knowledge.id = Guid.NewGuid().ToString();
                 knowledge = await client.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync(knowledge, new PartitionKey($"{knowledge.code}"));
             }
@@ -160,8 +162,9 @@ namespace TEAMModelOS.Controllers
                 {
                     return BadRequest($"{notinold.ToJsonString()} 不存在原来的知识点中");
                 }
-                var _new= old_new.Select(x => x._new).Where(z => !string.IsNullOrEmpty(z));
-                if (_new != null && _new.Count() > 0) {
+                var _new = old_new.Select(x => x._new).Where(z => !string.IsNullOrEmpty(z));
+                if (_new != null && _new.Count() > 0)
+                {
                     var notinnew = _new.Except(knowledge.points);
                     if (notinnew != null && notinnew.Count() > 0)
                     {
@@ -199,8 +202,9 @@ namespace TEAMModelOS.Controllers
             }
             if (knowledge != null)
             {
-                var notin = dto.points.Where(x => !knowledge.points.Any(y => y.Equals(x))).ToList() ;
-                if (notin.IsNotEmpty()) {
+                var notin = dto.points.Where(x => !knowledge.points.Any(y => y.Equals(x))).ToList();
+                if (notin.IsNotEmpty())
+                {
                     knowledge.points.AddRange(notin);
                     knowledge = await client.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync(knowledge, knowledge.id, new PartitionKey($"{knowledge.code}"));
                 }
@@ -232,19 +236,20 @@ namespace TEAMModelOS.Controllers
         [ProducesDefaultResponseType]
         [HttpPost("find-count")]
         [AuthToken(Roles = "teacher,admin,student", Permissions = "knowledge-read,knowledge-upd")]
-        public async Task<IActionResult> FindCount(Dictionary<string,string> request) 
+        public async Task<IActionResult> FindCount(Dictionary<string, string> request)
         {
             var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
             List<dynamic> datas = new List<dynamic>();
-            foreach (var kp in request) {
+            foreach (var kp in request)
+            {
                 var countPoint = 0;
                 var countBlock = 0;
                 if (!string.IsNullOrWhiteSpace(kp.Value))
                 {
-                    var value =await _azureRedis.GetRedisClient(8).HashGetAsync($"Knowledge:Count:{kp.Key}", kp.Value);
+                    var value = await _azureRedis.GetRedisClient(8).HashGetAsync($"Knowledge:Count:{kp.Key}", kp.Value);
                     if (value != default && !value.IsNullOrEmpty)
                     {
-                        
+
                         JsonElement record = value.ToString().ToObject<JsonElement>();
                         if (record.TryGetProperty("pcount", out JsonElement pcout))
                         {
@@ -254,27 +259,30 @@ namespace TEAMModelOS.Controllers
                         {
                             int.TryParse($"{bcout}", out countBlock);
                         }
-                        if (countPoint == 0) { 
+                        if (countPoint == 0)
+                        {
 
                         }
                     }
                 }
-                else {
+                else
+                {
                     var values = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Knowledge:Count:{kp.Key}");
-                    if (values != null) {
-                        foreach (var value in values) 
+                    if (values != null)
+                    {
+                        foreach (var value in values)
                         {
                             JsonElement record = value.ToString().ToObject<JsonElement>();
                             if (record.TryGetProperty("pcount", out JsonElement pcout))
                             {
-                                if (int.TryParse($"{pcout}", out int countP)) 
+                                if (int.TryParse($"{pcout}", out int countP))
                                 {
                                     countPoint = countPoint + countP;
                                 }
                             }
                             if (record.TryGetProperty("bcount", out JsonElement bcout))
                             {
-                                if(int.TryParse($"{bcout}", out int countB)) 
+                                if (int.TryParse($"{bcout}", out int countB))
                                 {
                                     countBlock = countBlock + countB;
                                 }
@@ -282,13 +290,13 @@ namespace TEAMModelOS.Controllers
                         }
                     }
                 }
-                datas.Add( new { key = kp.Key, countPoint, countBlock });
+                datas.Add(new { key = kp.Key, countPoint, countBlock });
             }
             return Ok(new { datas });
         }
 
 
-          
+
         /**
          * {
             ?"periodId": "ca484aa8-e7b5-4a7c-8ef3-bd9e7b7d4fp2",
@@ -299,17 +307,19 @@ namespace TEAMModelOS.Controllers
         [ProducesDefaultResponseType]
         [HttpPost("find")]
         [AuthToken(Roles = "teacher,admin,student", Permissions = "knowledge-read,knowledge-upd")]
-        public async Task<IActionResult> Find(JsonElement request) {
+        public async Task<IActionResult> Find(JsonElement request)
+        {
             var client = _azureCosmos.GetCosmosClient();
             request.TryGetProperty("periodId", out JsonElement periodId);
             if (!request.TryGetProperty("subjectId", out JsonElement subjectId)) return BadRequest();
             if (!request.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
             string code = $"Knowledge-{school_code}-{subjectId}";
             StringBuilder sql = new StringBuilder($"select value(c) from c");
-            if (periodId.ValueKind.Equals(JsonValueKind.String)) {
+            if (periodId.ValueKind.Equals(JsonValueKind.String))
+            {
                 sql.Append($" where c.periodId = '{periodId}'");
             }
-            List<Knowledge> knowledges = new List<Knowledge>() ;
+            List<Knowledge> knowledges = new List<Knowledge>();
             await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Knowledge>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
             {
                 knowledges.Add(item);
@@ -318,3 +328,90 @@ namespace TEAMModelOS.Controllers
         }
     }
 }
+
+
+using Newtonsoft.Json.Linq;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Net.Http;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using System.Security.Cryptography;
+using System.Net.Http.Json;
+using System.Text.Json;
+using System.IO;
+using TEAMModelOS.SDK.Extension;
+using System.Net;
+
+namespace TEAMModelOS.SDK.DI
+{
+    public class HttpTrigger
+    {
+        private readonly HttpClient _httpClient;
+
+        public HttpTrigger(HttpClient httpClient)
+        {
+            _httpClient = httpClient;
+        }
+        // <summary>
+        /// 请求信息
+        /// </summary>
+        /// <param name="robotUrl">釘釘Robot發送Url</param>
+        /// <param name="secret">加簽密鑰</param>
+        /// <param name="msg">發送訊息</param>
+        /// <returns></returns>
+        public async Task<(int status, string json)> RequestHttpTrigger(object data, string location, string url)
+        {
+            var keys = HttpTriggerUrl.HttpTrigger地址.GetDescriptionText().Split(',');
+            string domain = "";
+            if (location.Equals("China-Dep"))
+            {
+                domain = keys[1];
+            }
+            else if (location.Equals("China-Test"))
+            {
+                domain = keys[1];
+            }
+            else if (location.Equals("China"))
+            {
+                domain = keys[2];
+            }
+            else if (location.Equals("Global-Dep"))
+            {
+                domain = keys[4];
+            }
+            else if (location.Equals("Global-Test"))
+            {
+                domain = keys[4];
+            }
+            else if (location.Equals("Global"))
+            {
+                domain = keys[5];
+            }
+            string link = domain.Contains("localhost") ? $"http://{domain}/api/{url}" : $"http://{domain}/api/{url}";
+            HttpContent httpContent = new StringContent(data.ToJsonString());
+            HttpResponseMessage responseMessage = await _httpClient.PostAsync(link, httpContent);
+            if (responseMessage.StatusCode == HttpStatusCode.OK)
+            {
+                string Content = await responseMessage.Content.ReadAsStringAsync();
+                Content.ToObject<JsonElement>().TryGetProperty("data", out JsonElement content);
+                return (200, $"{content}");
+            }
+            else
+            {
+                string Content = await responseMessage.Content?.ReadAsStringAsync();
+                return (500, Content);
+            }
+        }
+    }
+    public enum HttpTriggerUrl
+    {
+        [Description("localhost:7071,teammodelosfunction-test.chinacloudsites.cn,teammodelosfunction.chinacloudsites.cn,teammodelosfunction.chinacloudsites.cn,teammodelosfunction.chinacloudsites.cn")]
+        HttpTrigger地址,
+
+    }
+}
+
+