NotifyCore.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. namespace molilian.core
  10. {
  11. public partial class NotifyCore
  12. {
  13. static string _base_url = string.Empty;
  14. static string _anpush_url = string.Empty;
  15. static string _qyweixin_push_url = string.Empty;
  16. static NotifyCore()
  17. {
  18. _anpush_url = Environment.GetEnvironmentVariable("ANPush");
  19. _qyweixin_push_url = Environment.GetEnvironmentVariable("QYWeixinPush");
  20. string? publish_url = Environment.GetEnvironmentVariable("NtfyServer");
  21. if (!string.IsNullOrEmpty(publish_url))
  22. {
  23. _base_url = publish_url;
  24. }
  25. }
  26. // 同步版本保持不变
  27. public static void Notify(string message)
  28. {
  29. if (!string.IsNullOrEmpty(_base_url))
  30. {
  31. NifyPlus.Send(_base_url, new NifyMessage { message = message });
  32. }
  33. }
  34. // 新增异步版本
  35. public static async Task NotifyAsync(string message)
  36. {
  37. if (!string.IsNullOrEmpty(_base_url))
  38. {
  39. await NifyPlus.SendAsync(_base_url, new NifyMessage { message = message });
  40. }
  41. }
  42. // 同步版本保持不变
  43. public static void Notify(NifyMessage message)
  44. {
  45. if (!string.IsNullOrEmpty(_base_url))
  46. {
  47. NifyPlus.Send(_base_url, message);
  48. }
  49. }
  50. // 新增异步版本
  51. public static async Task NotifyAsync(NifyMessage message)
  52. {
  53. if (!string.IsNullOrEmpty(_base_url))
  54. {
  55. await NifyPlus.SendAsync(_base_url, message);
  56. }
  57. }
  58. public static async Task QYWeixinPushNotifyAsync(string title, string content)
  59. {
  60. #if DEBUG
  61. await Task.CompletedTask;
  62. #else
  63. if (!string.IsNullOrEmpty(_qyweixin_push_url))
  64. {
  65. var args = new
  66. {
  67. msgtype = "markdown",
  68. markdown = new
  69. {
  70. content = $"# {title}\n{content}",
  71. }
  72. };
  73. string body = string.Empty;
  74. try
  75. {
  76. WebClientUtility client = new WebClientUtility();
  77. client.Post(args.Convert2Json());
  78. client.SetContentType("application/json");
  79. var response = await client.RequestAsync(_qyweixin_push_url, "POST");
  80. body = response.Body();
  81. new LoggerLibrary("qyweixin").Info(title, content).Info(body).Save();
  82. }
  83. catch (Exception ex)
  84. {
  85. new LoggerLibrary("qyweixin", "error")
  86. .Info(title, content)
  87. .Info(body)
  88. .Info(ex.Message, ex.StackTrace)
  89. .Save();
  90. }
  91. }
  92. #endif
  93. }
  94. // 同步版本保持不变
  95. public static void AnPushNotify(string title, string content)
  96. {
  97. #if DEBUG
  98. #else
  99. if (!string.IsNullOrEmpty(_anpush_url))
  100. {
  101. string url = _anpush_url;
  102. url = url.Replace("{title}", title?.UrlEncode());
  103. url = url.Replace("{content}", content?.UrlEncode());
  104. try
  105. {
  106. url.HttpRequest();
  107. }
  108. catch (Exception ex) { }
  109. }
  110. #endif
  111. }
  112. // 新增异步版本
  113. public static async Task AnPushNotifyAsync(string title, string content)
  114. {
  115. #if DEBUG
  116. await Task.CompletedTask;
  117. #else
  118. if (!string.IsNullOrEmpty(_anpush_url))
  119. {
  120. string url = _anpush_url;
  121. url = url.Replace("{title}", title?.UrlEncode());
  122. url = url.Replace("{content}", content?.UrlEncode());
  123. try
  124. {
  125. await Task.Run(() => url.HttpRequest());
  126. }
  127. catch (Exception ex) { }
  128. }
  129. #endif
  130. }
  131. }
  132. }