NotifyCore.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 NotifyCore()
  16. {
  17. _anpush_url = Environment.GetEnvironmentVariable("ANPush");
  18. string? publish_url = Environment.GetEnvironmentVariable("NtfyServer");
  19. if (!string.IsNullOrEmpty(publish_url))
  20. {
  21. _base_url = publish_url;
  22. }
  23. }
  24. // 同步版本保持不变
  25. public static void Notify(string message)
  26. {
  27. if (!string.IsNullOrEmpty(_base_url))
  28. {
  29. NifyPlus.Send(_base_url, new NifyMessage { message = message });
  30. }
  31. }
  32. // 新增异步版本
  33. public static async Task NotifyAsync(string message)
  34. {
  35. if (!string.IsNullOrEmpty(_base_url))
  36. {
  37. await NifyPlus.SendAsync(_base_url, new NifyMessage { message = message });
  38. }
  39. }
  40. // 同步版本保持不变
  41. public static void Notify(NifyMessage message)
  42. {
  43. if (!string.IsNullOrEmpty(_base_url))
  44. {
  45. NifyPlus.Send(_base_url, message);
  46. }
  47. }
  48. // 新增异步版本
  49. public static async Task NotifyAsync(NifyMessage message)
  50. {
  51. if (!string.IsNullOrEmpty(_base_url))
  52. {
  53. await NifyPlus.SendAsync(_base_url, message);
  54. }
  55. }
  56. // 同步版本保持不变
  57. public static void AnPushNotify(string title, string content)
  58. {
  59. #if DEBUG
  60. #else
  61. if (!string.IsNullOrEmpty(_anpush_url))
  62. {
  63. string url = _anpush_url;
  64. url = url.Replace("{title}", title?.UrlEncode());
  65. url = url.Replace("{content}", content?.UrlEncode());
  66. try
  67. {
  68. url.HttpRequest();
  69. }
  70. catch (Exception ex) { }
  71. }
  72. #endif
  73. }
  74. // 新增异步版本
  75. public static async Task AnPushNotifyAsync(string title, string content)
  76. {
  77. #if DEBUG
  78. await Task.CompletedTask;
  79. #else
  80. if (!string.IsNullOrEmpty(_anpush_url))
  81. {
  82. string url = _anpush_url;
  83. url = url.Replace("{title}", title?.UrlEncode());
  84. url = url.Replace("{content}", content?.UrlEncode());
  85. try
  86. {
  87. await Task.Run(() => url.HttpRequest());
  88. }
  89. catch (Exception ex) { }
  90. }
  91. #endif
  92. }
  93. }
  94. }