|
@@ -1,6 +1,9 @@
|
|
|
using Azure.Cosmos;
|
|
|
using Azure.Storage.Blobs.Models;
|
|
|
+using Grpc.Core;
|
|
|
using HTEXLib.COMM.Helpers;
|
|
|
+using Microsoft.AspNetCore;
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.Azure.Cosmos.Table;
|
|
|
using Microsoft.Azure.Functions.Worker;
|
|
|
using Microsoft.Azure.Functions.Worker.Http;
|
|
@@ -12,6 +15,7 @@ using System.Net;
|
|
|
using System.Net.Http;
|
|
|
using System.Net.Http.Json;
|
|
|
using System.Reflection;
|
|
|
+using System.Security.Policy;
|
|
|
using System.Text;
|
|
|
using System.Text.Json;
|
|
|
using System.Threading.Tasks;
|
|
@@ -22,6 +26,7 @@ using TEAMModelOS.SDK.Extension;
|
|
|
using TEAMModelOS.SDK.Models;
|
|
|
using TEAMModelOS.SDK.Models.Cosmos.BI.BINormal;
|
|
|
using TEAMModelOS.SDK.Models.Cosmos.Teacher;
|
|
|
+using TEAMModelOS.SDK.Models.Service;
|
|
|
using TEAMModelOS.SDK.Models.Table;
|
|
|
using static TEAMModelOS.SDK.Models.Teacher;
|
|
|
|
|
@@ -34,7 +39,7 @@ namespace TEAMModelOS.SDK.DI
|
|
|
private readonly AzureStorageFactory _azureStorage;
|
|
|
private readonly AzureRedisFactory _azureRedis;
|
|
|
private readonly IHttpClientFactory _httpClient;
|
|
|
- public WebHookHttpTrigger(IHttpClientFactory httpClient,AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage , AzureRedisFactory azureRedis)
|
|
|
+ public WebHookHttpTrigger(IHttpClientFactory httpClient, AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis)
|
|
|
{
|
|
|
_azureCosmos = azureCosmos;
|
|
|
_dingDing = dingDing;
|
|
@@ -52,33 +57,33 @@ namespace TEAMModelOS.SDK.DI
|
|
|
public async Task<HttpResponseData> NoticeSchoolAuthChange([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "webhook/school-auth-change")] HttpRequestData request)
|
|
|
{
|
|
|
var response = request.CreateResponse(HttpStatusCode.OK);
|
|
|
- ( List<BizConfig> businessConfigs, List<string> webhookdomain, SchoolAuthChange data) = await GetRequestData<SchoolAuthChange>(request);
|
|
|
- if (webhookdomain.IsNotEmpty())
|
|
|
+ string datastr = await new StreamReader(request.Body).ReadToEndAsync();
|
|
|
+ var json = JsonDocument.Parse(datastr).RootElement;
|
|
|
+ SchoolAuthChange data = null;
|
|
|
+ if (json.TryGetProperty("data", out JsonElement _data))
|
|
|
{
|
|
|
- foreach (var domain in webhookdomain)
|
|
|
+ data = _data.ToObject<SchoolAuthChange>();
|
|
|
+ }
|
|
|
+
|
|
|
+ (List<BizConfig> businessConfigs, List<(List<string> urls, string head, string token, BizConfig config)> webhooks) = await WebHookService.GetRequestData(json, _azureCosmos, _azureRedis, _azureStorage);
|
|
|
+ if (webhooks.IsNotEmpty())
|
|
|
+ {
|
|
|
+ foreach (var webhook in webhooks)
|
|
|
{
|
|
|
- await send(new
|
|
|
+ foreach (var url in webhook.urls)
|
|
|
{
|
|
|
- data.addSchools,
|
|
|
- data.rmvSchools
|
|
|
- }, domain, "school-auth-change");
|
|
|
+ await WebHookService.Send(new
|
|
|
+ {
|
|
|
+ data.addSchools,
|
|
|
+ data.rmvSchools
|
|
|
+ }, (url, webhook.head, webhook.token, webhook.config), "school-auth-change",_httpClient,_dingDing);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
- public class SchoolAuthChange
|
|
|
- {
|
|
|
- public string bizcode { get; set; }
|
|
|
- /// <summary>
|
|
|
- /// 添加的学校数据授权
|
|
|
- /// </summary>
|
|
|
- public List<IdSchool> addSchools { get; set; } = new List<IdSchool>();
|
|
|
- /// <summary>
|
|
|
- /// 取消的学校数据授权
|
|
|
- /// </summary>
|
|
|
- public List<IdSchool> rmvSchools { get; set; } = new List<IdSchool>();
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
/// <summary>
|
|
|
/// 名单变更通知,名单类12开头
|
|
@@ -86,130 +91,42 @@ namespace TEAMModelOS.SDK.DI
|
|
|
/// <param name="request"></param>
|
|
|
/// <returns></returns>
|
|
|
[Function("group-member-change")]
|
|
|
- [ApiToken(Auth = "1201", Name = "名单成员变更",TName = "變更名單成員資料", EName = "Name List Member Changes", RWN = "N")]
|
|
|
- public async Task<HttpResponseData> NoticeGroupChange([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "webhook/group-member-change")] HttpRequestData request) {
|
|
|
+ [ApiToken(Auth = "1201", Name = "名单成员变更", TName = "變更名單成員資料", EName = "Name List Member Changes", RWN = "N")]
|
|
|
+ public async Task<HttpResponseData> NoticeGroupChange([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "webhook/group-member-change")] HttpRequestData request)
|
|
|
+ {
|
|
|
var response = request.CreateResponse(HttpStatusCode.OK);
|
|
|
- ( List < BizConfig > businessConfigs, List<string> webhookdomain, GroupChange data) = await GetRequestData<GroupChange>(request);
|
|
|
-
|
|
|
- if ( webhookdomain.IsNotEmpty()&& string.IsNullOrWhiteSpace(data.school)) {
|
|
|
- foreach (var domain in webhookdomain) {
|
|
|
- await send(new
|
|
|
- {
|
|
|
- data.status,
|
|
|
- data.type,
|
|
|
- data.listid,
|
|
|
- data.tmdjoin,
|
|
|
- data.tmdleave,
|
|
|
- data.stujoin,
|
|
|
- data.stuleave,
|
|
|
- data.tchjoin,
|
|
|
- data.tchleave,
|
|
|
- data.school
|
|
|
- }, domain,"group-member-change");
|
|
|
- }
|
|
|
+ string datastr = await new StreamReader(request.Body).ReadToEndAsync();
|
|
|
+ var json = JsonDocument.Parse(datastr).RootElement;
|
|
|
+ GroupChange data = null;
|
|
|
+ if (json.TryGetProperty("data", out JsonElement _data)){
|
|
|
+ data= _data.ToObject<GroupChange>();
|
|
|
}
|
|
|
- return response;
|
|
|
- }
|
|
|
- /// <summary>
|
|
|
- /// 测试域名的webhook是否接入
|
|
|
- /// </summary>
|
|
|
- /// <param name="request"></param>
|
|
|
- /// <returns></returns>
|
|
|
- [Function("check-domain-webhook")]
|
|
|
-
|
|
|
- public async Task<HttpResponseData> CheckDomainWebhook([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "webhook/check-domain-webhook")] HttpRequestData request) {
|
|
|
- string data = await new StreamReader(request.Body).ReadToEndAsync();
|
|
|
- var json = JsonDocument.Parse(data).RootElement;
|
|
|
- if (json.TryGetProperty("domain", out JsonElement domain) && json.TryGetProperty("https", out JsonElement _https))
|
|
|
+
|
|
|
+ (List<BizConfig> businessConfigs, List<(List<string> urls, string head, string token, BizConfig config)> webhooks ) = await WebHookService.GetRequestData(json,_azureCosmos,_azureRedis,_azureStorage);
|
|
|
+ if (webhooks.IsNotEmpty() && string.IsNullOrWhiteSpace(data.school))
|
|
|
{
|
|
|
- string https = _https.ValueKind.Equals(JsonValueKind.Number) && int.Parse($"{_https}") == 1 ? "https" : "http";
|
|
|
- string url = $"{https}://{domain}";
|
|
|
- HttpStatusCode httpStatus = await send(new { }, url, "check-domain-webhook");
|
|
|
- var response = request.CreateResponse(httpStatus);
|
|
|
- return response;
|
|
|
- }
|
|
|
- else {
|
|
|
- var response = request.CreateResponse(HttpStatusCode.BadRequest);
|
|
|
- return response;
|
|
|
- }
|
|
|
- }
|
|
|
- /// <summary>
|
|
|
- /// 消息发送方法
|
|
|
- /// </summary>
|
|
|
- /// <param name="data"></param>
|
|
|
- /// <param name="domain"></param>
|
|
|
- /// <param name="notice"></param>
|
|
|
- public async Task<HttpStatusCode> send (dynamic data,string domain, string notice) {
|
|
|
- var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
- var client = _httpClient.CreateClient();
|
|
|
- client.Timeout=new TimeSpan(0,0,10);
|
|
|
- HttpResponseMessage httpResponse =await client.PostAsJsonAsync($"{domain}/webhook", new
|
|
|
- {
|
|
|
- time = timestamp,
|
|
|
- notice =notice,
|
|
|
- data = data
|
|
|
- });
|
|
|
- return httpResponse.StatusCode;
|
|
|
- }
|
|
|
- /// <summary>
|
|
|
- /// 解析HttpRequestData 传递的数据
|
|
|
- /// </summary>
|
|
|
- /// <typeparam name="T"></typeparam>
|
|
|
- /// <param name="request"></param>
|
|
|
- /// <returns></returns>
|
|
|
- public async Task<( List<BizConfig> businessConfigs,List<string> webhookdomain, T data)> GetRequestData<T>(HttpRequestData request)
|
|
|
- {
|
|
|
- //var response = request.CreateResponse(HttpStatusCode.OK);
|
|
|
- string data = await new StreamReader(request.Body).ReadToEndAsync();
|
|
|
- var json = JsonDocument.Parse(data).RootElement;
|
|
|
- List< BizConfig > bizConfigs = new List< BizConfig >();
|
|
|
- try {
|
|
|
- var table = _azureStorage.GetCloudTableClient().GetTableReference("IESOpenApi");
|
|
|
- if (json.TryGetProperty("school", out JsonElement _school) && !string.IsNullOrWhiteSpace($"{_school}"))
|
|
|
+ foreach (var webhook in webhooks)
|
|
|
{
|
|
|
- string sql = $"select distinct value c from c join s in c.schools where s.id='{_school}' ";
|
|
|
-
|
|
|
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal)
|
|
|
- .GetItemQueryIterator<BizConfig>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("BizConfig") }))
|
|
|
+ foreach (var url in webhook.urls)
|
|
|
{
|
|
|
- bizConfigs.Add(item);
|
|
|
+ await WebHookService.Send(new
|
|
|
+ {
|
|
|
+ data.status,
|
|
|
+ data.type,
|
|
|
+ data.listid,
|
|
|
+ data.tmdjoin,
|
|
|
+ data.tmdleave,
|
|
|
+ data.stujoin,
|
|
|
+ data.stuleave,
|
|
|
+ data.tchjoin,
|
|
|
+ data.tchleave,
|
|
|
+ data.school
|
|
|
+ }, (url, webhook.head, webhook.token, webhook.config), "group-member-change", _httpClient, _dingDing);
|
|
|
}
|
|
|
}
|
|
|
- if (json.TryGetProperty("bizid", out JsonElement _bizid))
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- BizConfig bizConfig = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Normal").ReadItemAsync<BizConfig>($"{_bizid}", new PartitionKey("BizConfig"));
|
|
|
- bizConfigs.Add(bizConfig);
|
|
|
- }
|
|
|
- catch (CosmosException ex) when (ex.Status == 404)
|
|
|
- {
|
|
|
- return (bizConfigs, null, default(T));
|
|
|
- }
|
|
|
- }
|
|
|
- if (json.TryGetProperty("data", out JsonElement _data) && bizConfigs.IsNotEmpty())
|
|
|
- {
|
|
|
- var webhookdomain = bizConfigs.Where(z => !string.IsNullOrWhiteSpace(z.webhook)).ToList();
|
|
|
- List<string> webhookdomains = new List<string>();
|
|
|
- webhookdomain.ForEach(x => {
|
|
|
- webhookdomains.AddRange(x.webhook.Split(",").Select(y => x.https == 1 ? $"https://{y}" : $"http://{y}"));
|
|
|
- });
|
|
|
- return (bizConfigs, webhookdomains, _data.ToObject<T>());
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return (bizConfigs, null, default(T));
|
|
|
- }
|
|
|
- } catch {
|
|
|
- return (bizConfigs, null, default(T));
|
|
|
}
|
|
|
+ return response;
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public class WebhookSchoolData {
|
|
|
- public string school { get; set; }
|
|
|
- public string data { get; set; }
|
|
|
- }
|
|
|
-
|
|
|
+
|
|
|
+ }
|
|
|
}
|