WebHookService.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using HTEXLib.COMM.Helpers;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Net;
  8. using System.Text;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.SDK.DI;
  12. using TEAMModelOS.SDK.Models.Cosmos.BI.BINormal;
  13. using Microsoft.Azure.Functions.Worker.Http;
  14. using Azure.Cosmos;
  15. using TEAMModelOS.SDK.Extension;
  16. using System.Net.Http.Json;
  17. namespace TEAMModelOS.SDK.Models.Service
  18. {
  19. public static class WebHookService
  20. {
  21. /// <summary>
  22. /// 消息发送方法
  23. /// </summary>
  24. /// <param name="data"></param>
  25. /// <param name="domain"></param>
  26. /// <param name="notice"></param>
  27. 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= 60)
  28. {
  29. try
  30. {
  31. var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  32. var client = _httpClient.CreateClient();
  33. client.Timeout= new TimeSpan(0, 0, timeout);
  34. if (!string.IsNullOrWhiteSpace(webhook.head) && !string.IsNullOrWhiteSpace(webhook.token))
  35. {
  36. client.DefaultRequestHeaders.Remove(webhook.head);
  37. client.DefaultRequestHeaders.Add(webhook.head, webhook.token);
  38. }
  39. HttpResponseMessage httpResponse = await client.PostAsJsonAsync($"{webhook.url}", new
  40. {
  41. time = timestamp,
  42. notice = notice,
  43. data = data
  44. });
  45. return httpResponse.StatusCode;
  46. }
  47. catch (Exception e)
  48. {
  49. await _dingDing.SendBotMsg($"第三方webhook回调失败:\n企业:{webhook.config.name}({webhook.config.id})\n地址:{webhook.url}\n通知:{notice}\n原因:{e.Message}{e.StackTrace}", GroupNames.成都开发測試群組);
  50. return HttpStatusCode.InternalServerError;
  51. }
  52. }
  53. /// <summary>
  54. /// 解析HttpRequestData 传递的数据
  55. /// </summary>
  56. /// <typeparam name="T"></typeparam>
  57. /// <param name="request"></param>
  58. /// <returns></returns>
  59. public static async Task<(List<BizConfig> businessConfigs, List<(List<string> urls, string head, string token, BizConfig config)> webhooks)>
  60. GetRequestData(JsonElement json , AzureCosmosFactory _azureCosmos, AzureRedisFactory _azureRedis, AzureStorageFactory _azureStorage)
  61. {
  62. //var response = request.CreateResponse(HttpStatusCode.OK);
  63. List<BizConfig> bizConfigs = new List<BizConfig>();
  64. try
  65. {
  66. var table = _azureStorage.GetCloudTableClient().GetTableReference("IESOpenApi");
  67. if (json.TryGetProperty("school", out JsonElement _school) && !string.IsNullOrWhiteSpace($"{_school}"))
  68. {
  69. string sql = $"select distinct value c from c join s in c.schools where s.id='{_school}' and c.webhook<>null and c.webhook<>'' ";
  70. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal)
  71. .GetItemQueryIterator<BizConfig>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("BizConfig") }))
  72. {
  73. bizConfigs.Add(item);
  74. }
  75. }
  76. if (json.TryGetProperty("bizid", out JsonElement _bizid))
  77. {
  78. try
  79. {
  80. BizConfig bizConfig = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Normal").ReadItemAsync<BizConfig>($"{_bizid}", new PartitionKey("BizConfig"));
  81. bizConfigs.Add(bizConfig);
  82. }
  83. catch (CosmosException ex) when (ex.Status == 404)
  84. {
  85. return (bizConfigs, null);
  86. }
  87. }
  88. if (bizConfigs.IsNotEmpty())
  89. {
  90. var webhookdomain = bizConfigs.Where(z => !string.IsNullOrWhiteSpace(z.webhook)).Select(w => new { w.webhook, w.webhookHead, w.webhookToken, config = w }).ToList();
  91. List<(List<string> urls, string head, string token, BizConfig config)> webhookdomains = new List<(List<string> urls, string head, string token, BizConfig config)>();
  92. webhookdomain.ForEach(x => {
  93. webhookdomains.Add((x.webhook.Trim().Split(",").ToList(), x.webhookHead, x.webhookToken, x.config));
  94. });
  95. return (bizConfigs, webhookdomains);
  96. }
  97. else
  98. {
  99. return (bizConfigs, null);
  100. }
  101. }
  102. catch
  103. {
  104. return (bizConfigs, null);
  105. }
  106. }
  107. }
  108. public class SchoolAuthChange
  109. {
  110. public string bizcode { get; set; }
  111. /// <summary>
  112. /// 添加的学校数据授权
  113. /// </summary>
  114. public List<IdSchool> addSchools { get; set; } = new List<IdSchool>();
  115. /// <summary>
  116. /// 取消的学校数据授权
  117. /// </summary>
  118. public List<IdSchool> rmvSchools { get; set; } = new List<IdSchool>();
  119. }
  120. public class WebhookSchoolData
  121. {
  122. public string school { get; set; }
  123. public string data { get; set; }
  124. }
  125. }