using HTEXLib.COMM.Helpers; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Net; using System.Text; using System.Text.Json; using System.Threading.Tasks; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Models.Cosmos.BI.BINormal; using Microsoft.Azure.Functions.Worker.Http; using Azure.Cosmos; using TEAMModelOS.SDK.Extension; using System.Net.Http.Json; namespace TEAMModelOS.SDK.Models.Service { public static class WebHookService { /// /// 消息发送方法 /// /// /// /// public static async Task Send(dynamic data, (string url, string head, string token, BizConfig config) webhook, string notice, IHttpClientFactory _httpClient,DingDing _dingDing,int timeout= 5) { try { var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); var sendData = new { time = timestamp, notice = notice, data = data }; var s = sendData.ToJsonString(); var client = _httpClient.CreateClient(); client.Timeout= new TimeSpan(0, 0, timeout); if (!string.IsNullOrWhiteSpace(webhook.head) && !string.IsNullOrWhiteSpace(webhook.token)) { client.DefaultRequestHeaders.Remove(webhook.head); client.DefaultRequestHeaders.Add(webhook.head, webhook.token); } HttpResponseMessage httpResponse = await client.PostAsJsonAsync($"{webhook.url}", sendData); //await _dingDing.SendBotMsg($"第三方webhook回调成功:\n企业:{webhook.config.name}({webhook.config.id})\n地址:{webhook.url}\n通知:{notice}\n数据:{sendData.ToJsonString()}", GroupNames.成都开发測試群組); return httpResponse.StatusCode; } catch (Exception e) { await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")} 第三方webhook回调失败:\n企业:{webhook.config.name}({webhook.config.id})\n地址:{webhook.url}\n通知:{notice}\n原因:{e.Message}\n{e.StackTrace}", GroupNames.成都开发測試群組); return HttpStatusCode.InternalServerError; } } /// /// 解析HttpRequestData 传递的数据 /// /// /// /// public static async Task<(List businessConfigs, List<(List urls, string head, string token, BizConfig config)> webhooks)> GetRequestData(JsonElement json , AzureCosmosFactory _azureCosmos, AzureRedisFactory _azureRedis, AzureStorageFactory _azureStorage) { //var response = request.CreateResponse(HttpStatusCode.OK); List bizConfigs = new List(); try { if (json.TryGetProperty("school", out JsonElement _school) && !string.IsNullOrWhiteSpace($"{_school}")) { string sql = $"select distinct value c from c join s in c.schools where s.id='{_school}' and c.webhook<>null and c.webhook<>'' "; await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal) .GetItemQueryIterator(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("BizConfig") })) { bizConfigs.Add(item); } } if (json.TryGetProperty("bizid", out JsonElement _bizid)) { try { BizConfig bizConfig = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Normal").ReadItemAsync($"{_bizid}", new PartitionKey("BizConfig")); bizConfigs.Add(bizConfig); } catch (CosmosException ex) when (ex.Status == 404) { return (bizConfigs, null); } } if (bizConfigs.IsNotEmpty()) { var webhookdomain = bizConfigs.Where(z => !string.IsNullOrWhiteSpace(z.webhook)).Select(w => new { w.webhook, w.webhookHead, w.webhookToken, config = w }).ToList(); List<(List urls, string head, string token, BizConfig config)> webhookdomains = new List<(List urls, string head, string token, BizConfig config)>(); webhookdomain.ForEach(x => { webhookdomains.Add((x.webhook.Trim().Split(",").ToList(), x.webhookHead, x.webhookToken, x.config)); }); return (bizConfigs, webhookdomains); } else { return (bizConfigs, null); } } catch { return (bizConfigs, null); } } } public class SchoolAuthChange { public string bizcode { get; set; } /// /// 添加的学校数据授权 /// public List addSchools { get; set; } = new List(); /// /// 取消的学校数据授权 /// public List rmvSchools { get; set; } = new List(); } public class WebhookSchoolData { public string school { get; set; } public string data { get; set; } } }