123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net.Http;
- using System.Net;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Models.Cosmos.BI.BINormal;
- using Microsoft.Azure.Cosmos;
- using TEAMModelOS.SDK.Extension;
- using System.Net.Http.Json;
- namespace TEAMModelOS.SDK.Models.Service
- {
- public static class WebHookService
- {
- /// <summary>
- /// 消息发送方法
- /// </summary>
- /// <param name="data"></param>
- /// <param name="domain"></param>
- /// <param name="notice"></param>
- public static async Task<HttpStatusCode> 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;
- }
- }
- /// <summary>
- /// 解析HttpRequestData 传递的数据
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="request"></param>
- /// <returns></returns>
- public static async Task<(List<BizConfig> businessConfigs, List<(List<string> urls, string head, string token, BizConfig config)> webhooks)>
- GetRequestData(JsonElement json , AzureCosmosFactory _azureCosmos, AzureRedisFactory _azureRedis, AzureStorageFactory _azureStorage)
- {
- //var response = request.CreateResponse(HttpStatusCode.OK);
-
- List<BizConfig> bizConfigs = new List<BizConfig>();
- 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)
- .GetItemQueryIteratorSql<BizConfig>(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<BizConfig>($"{_bizid}", new PartitionKey("BizConfig"));
- bizConfigs.Add(bizConfig);
- }
- catch (CosmosException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
- {
- 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<string> urls, string head, string token, BizConfig config)> webhookdomains = new List<(List<string> 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; }
- /// <summary>
- /// 添加的学校数据授权
- /// </summary>
- public List<IdSchool> addSchools { get; set; } = new List<IdSchool>();
- /// <summary>
- /// 取消的学校数据授权
- /// </summary>
- public List<IdSchool> rmvSchools { get; set; } = new List<IdSchool>();
- }
- public class WebhookSchoolData
- {
- public string school { get; set; }
- public string data { get; set; }
- }
- }
|