|
@@ -0,0 +1,208 @@
|
|
|
+using Azure.Cosmos;
|
|
|
+using Azure.Storage.Blobs.Models;
|
|
|
+using HTEXLib.COMM.Helpers;
|
|
|
+using Microsoft.Azure.Cosmos.Table;
|
|
|
+using Microsoft.Azure.Functions.Worker;
|
|
|
+using Microsoft.Azure.Functions.Worker.Http;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Net;
|
|
|
+using System.Net.Http;
|
|
|
+using System.Net.Http.Json;
|
|
|
+using System.Reflection;
|
|
|
+using System.Text;
|
|
|
+using System.Text.Json;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using TEAMModelOS.Filter;
|
|
|
+using TEAMModelOS.SDK;
|
|
|
+using TEAMModelOS.SDK.DI;
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
+using TEAMModelOS.SDK.Models;
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos.Teacher;
|
|
|
+using TEAMModelOS.SDK.Models.Table;
|
|
|
+using static TEAMModelOS.SDK.Models.Teacher;
|
|
|
+
|
|
|
+namespace TEAMModelOS.SDK.DI
|
|
|
+{
|
|
|
+ public class WebHookHttpTrigger
|
|
|
+ {
|
|
|
+ private readonly AzureCosmosFactory _azureCosmos;
|
|
|
+ private readonly DingDing _dingDing;
|
|
|
+ private readonly AzureStorageFactory _azureStorage;
|
|
|
+ private readonly AzureRedisFactory _azureRedis;
|
|
|
+ private readonly HttpClient _httpClient;
|
|
|
+ public WebHookHttpTrigger(HttpClient httpClient,AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage , AzureRedisFactory azureRedis)
|
|
|
+ {
|
|
|
+ _azureCosmos = azureCosmos;
|
|
|
+ _dingDing = dingDing;
|
|
|
+ _azureStorage = azureStorage;
|
|
|
+ _azureRedis = azureRedis;
|
|
|
+ _httpClient = httpClient;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 学校数据授权变更,学校基础信息 10开头
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [Function("school-auth-change")]
|
|
|
+ [ApiToken(Auth = "1001", Name = "学校数据授权变更", RWN = "N")]
|
|
|
+ public async Task<HttpResponseData> NoticeSchoolAuthChange([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "webhook/school-auth-change")] HttpRequestData request)
|
|
|
+ {
|
|
|
+ var response = request.CreateResponse(HttpStatusCode.OK);
|
|
|
+ ( List<BusinessConfig> businessConfigs, List<string> webhookdomain, SchoolAuthChange data) = await GetRequestData<SchoolAuthChange>(request);
|
|
|
+ if (webhookdomain.IsNotEmpty())
|
|
|
+ {
|
|
|
+ foreach (var domain in webhookdomain)
|
|
|
+ {
|
|
|
+ await send(new
|
|
|
+ {
|
|
|
+ data.addSchools,
|
|
|
+ data.rmvSchools
|
|
|
+ }, domain, "school-auth-change");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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开头
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [Function("group-member-change")]
|
|
|
+ [ApiToken(Auth = "1201", Name = "名单成员变更", 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 < BusinessConfig > 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");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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))
|
|
|
+ {
|
|
|
+ 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();
|
|
|
+ HttpResponseMessage httpResponse =await _httpClient.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<BusinessConfig> 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;
|
|
|
+ string school = null;
|
|
|
+ List<BusinessConfig> businessConfigs = null ;
|
|
|
+ var table = _azureStorage.GetCloudTableClient().GetTableReference("IESOpenApi");
|
|
|
+ if (json.TryGetProperty("school", out JsonElement _school))
|
|
|
+ {
|
|
|
+ school = _school.GetString();
|
|
|
+
|
|
|
+ List<BusinessSchool> schools = table.FindListByDictSync<BusinessSchool>(new Dictionary<string, object> { { "PartitionKey", $"BusinessSchool" }, { "school", school } });
|
|
|
+ var bizid = schools.Select(x => x.bizid).ToList();
|
|
|
+ List<string> Codes = new List<string>();
|
|
|
+ bizid.ForEach(x => {
|
|
|
+ Codes.Add($" RowKey {QueryComparisons.Equal} '{x}' ");
|
|
|
+ });
|
|
|
+ string tbqurey = $"PartitionKey {QueryComparisons.Equal} 'BusinessConfig' and ( {string.Join(" or ", Codes)} ) ";
|
|
|
+ var result = await table.ExecuteQuerySegmentedAsync(new TableQuery<BusinessConfig>().Where(tbqurey), null);
|
|
|
+ if (result != null) {
|
|
|
+ businessConfigs= result.Results;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (json.TryGetProperty("bizid", out JsonElement _bizid)) {
|
|
|
+ string tbqurey = $"PartitionKey {QueryComparisons.Equal} 'BusinessConfig' and RowKey {QueryComparisons.Equal} '{_bizid}' ";
|
|
|
+ var result = await table.ExecuteQuerySegmentedAsync(new TableQuery<BusinessConfig>().Where(tbqurey), null);
|
|
|
+ if (result != null)
|
|
|
+ {
|
|
|
+ businessConfigs = result.Results;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (json.TryGetProperty("data", out JsonElement _data) && businessConfigs.IsNotEmpty())
|
|
|
+ {
|
|
|
+ var webhookdomain = businessConfigs.Where(z => !string.IsNullOrWhiteSpace(z.webhookdomain)).ToList();
|
|
|
+ List<string> webhookdomains = new List<string>();
|
|
|
+ webhookdomain.ForEach(x => {
|
|
|
+ webhookdomains.AddRange(x.webhookdomain.Split(",").Select(y =>x.https==1? $"https://{y}":$"http://{y}"));
|
|
|
+ });
|
|
|
+ return ( businessConfigs, webhookdomains, _data.ToObject<T>());
|
|
|
+ }
|
|
|
+ else { return ( businessConfigs,null , default(T)); }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public class WebhookSchoolData {
|
|
|
+ public string school { get; set; }
|
|
|
+ public string data { get; set; }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|