WebHookService.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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= 5)
  28. {
  29. try
  30. {
  31. var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  32. var sendData = new
  33. {
  34. time = timestamp,
  35. notice = notice,
  36. data = data
  37. };
  38. var s = sendData.ToJsonString();
  39. var client = _httpClient.CreateClient();
  40. client.Timeout= new TimeSpan(0, 0, timeout);
  41. if (!string.IsNullOrWhiteSpace(webhook.head) && !string.IsNullOrWhiteSpace(webhook.token))
  42. {
  43. client.DefaultRequestHeaders.Remove(webhook.head);
  44. client.DefaultRequestHeaders.Add(webhook.head, webhook.token);
  45. }
  46. HttpResponseMessage httpResponse = await client.PostAsJsonAsync($"{webhook.url}", sendData);
  47. //await _dingDing.SendBotMsg($"第三方webhook回调成功:\n企业:{webhook.config.name}({webhook.config.id})\n地址:{webhook.url}\n通知:{notice}\n数据:{sendData.ToJsonString()}", GroupNames.成都开发測試群組);
  48. return httpResponse.StatusCode;
  49. }
  50. catch (Exception e)
  51. {
  52. 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.成都开发測試群組);
  53. return HttpStatusCode.InternalServerError;
  54. }
  55. }
  56. /// <summary>
  57. /// 解析HttpRequestData 传递的数据
  58. /// </summary>
  59. /// <typeparam name="T"></typeparam>
  60. /// <param name="request"></param>
  61. /// <returns></returns>
  62. public static async Task<(List<BizConfig> businessConfigs, List<(List<string> urls, string head, string token, BizConfig config)> webhooks)>
  63. GetRequestData(JsonElement json , AzureCosmosFactory _azureCosmos, AzureRedisFactory _azureRedis, AzureStorageFactory _azureStorage)
  64. {
  65. //var response = request.CreateResponse(HttpStatusCode.OK);
  66. List<BizConfig> bizConfigs = new List<BizConfig>();
  67. try
  68. {
  69. if (json.TryGetProperty("school", out JsonElement _school) && !string.IsNullOrWhiteSpace($"{_school}"))
  70. {
  71. string sql = $"select distinct value c from c join s in c.schools where s.id='{_school}' and c.webhook<>null and c.webhook<>'' ";
  72. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Normal)
  73. .GetItemQueryIterator<BizConfig>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("BizConfig") }))
  74. {
  75. bizConfigs.Add(item);
  76. }
  77. }
  78. if (json.TryGetProperty("bizid", out JsonElement _bizid))
  79. {
  80. try
  81. {
  82. BizConfig bizConfig = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Normal").ReadItemAsync<BizConfig>($"{_bizid}", new PartitionKey("BizConfig"));
  83. bizConfigs.Add(bizConfig);
  84. }
  85. catch (CosmosException ex) when (ex.Status == 404)
  86. {
  87. return (bizConfigs, null);
  88. }
  89. }
  90. if (bizConfigs.IsNotEmpty())
  91. {
  92. var webhookdomain = bizConfigs.Where(z => !string.IsNullOrWhiteSpace(z.webhook)).Select(w => new { w.webhook, w.webhookHead, w.webhookToken, config = w }).ToList();
  93. List<(List<string> urls, string head, string token, BizConfig config)> webhookdomains = new List<(List<string> urls, string head, string token, BizConfig config)>();
  94. webhookdomain.ForEach(x => {
  95. webhookdomains.Add((x.webhook.Trim().Split(",").ToList(), x.webhookHead, x.webhookToken, x.config));
  96. });
  97. return (bizConfigs, webhookdomains);
  98. }
  99. else
  100. {
  101. return (bizConfigs, null);
  102. }
  103. }
  104. catch
  105. {
  106. return (bizConfigs, null);
  107. }
  108. }
  109. }
  110. public class SchoolAuthChange
  111. {
  112. public string bizcode { get; set; }
  113. /// <summary>
  114. /// 添加的学校数据授权
  115. /// </summary>
  116. public List<IdSchool> addSchools { get; set; } = new List<IdSchool>();
  117. /// <summary>
  118. /// 取消的学校数据授权
  119. /// </summary>
  120. public List<IdSchool> rmvSchools { get; set; } = new List<IdSchool>();
  121. }
  122. public class WebhookSchoolData
  123. {
  124. public string school { get; set; }
  125. public string data { get; set; }
  126. }
  127. }