WebHookService.cs 5.8 KB

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