HangfireBackJob.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using Common;
  2. using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
  3. using Masuit.MyBlogs.Core.Models.DTO;
  4. using Masuit.MyBlogs.Core.Models.Entity;
  5. using Masuit.MyBlogs.Core.Models.Enum;
  6. using Masuit.Tools.Core.Net;
  7. using Masuit.Tools.NoSQL;
  8. using Masuit.Tools.Systems;
  9. using System;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net.Http;
  13. using System.Net.Http.Headers;
  14. using System.Threading.Tasks;
  15. namespace Masuit.MyBlogs.Core.Extensions.Hangfire
  16. {
  17. public class HangfireBackJob : IHangfireBackJob
  18. {
  19. private readonly IUserInfoService _userInfoService;
  20. private readonly IPostService _postService;
  21. private readonly ISystemSettingService _settingService;
  22. private readonly ISearchDetailsService _searchDetailsService;
  23. private readonly ILinksService _linksService;
  24. private readonly RedisHelper _redisHelper;
  25. private readonly IHttpClientFactory _httpClientFactory;
  26. public HangfireBackJob(IUserInfoService userInfoService, IPostService postService, ISystemSettingService settingService, ISearchDetailsService searchDetailsService, ILinksService linksService, RedisHelper redisHelper, IHttpClientFactory httpClientFactory)
  27. {
  28. _userInfoService = userInfoService;
  29. _postService = postService;
  30. _settingService = settingService;
  31. _searchDetailsService = searchDetailsService;
  32. _linksService = linksService;
  33. _redisHelper = redisHelper;
  34. _httpClientFactory = httpClientFactory;
  35. }
  36. public void LoginRecord(UserInfoOutputDto userInfo, string ip, LoginType type)
  37. {
  38. var result = ip.GetPhysicsAddressInfo().Result;
  39. if (result?.Status == 0)
  40. {
  41. string addr = result.AddressResult.FormattedAddress;
  42. string prov = result.AddressResult.AddressComponent.Province;
  43. LoginRecord record = new LoginRecord()
  44. {
  45. IP = ip,
  46. LoginTime = DateTime.Now,
  47. LoginType = type,
  48. PhysicAddress = addr,
  49. Province = prov
  50. };
  51. UserInfo u = _userInfoService.GetByUsername(userInfo.Username);
  52. u.LoginRecord.Add(record);
  53. _userInfoService.UpdateEntitySaved(u);
  54. string content = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "template\\login.html").Replace("{{name}}", u.Username).Replace("{{time}}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Replace("{{ip}}", record.IP).Replace("{{address}}", record.PhysicAddress);
  55. CommonHelper.SendMail(_settingService.GetFirstEntity(s => s.Name.Equals("Title")).Value + "账号登录通知", content, _settingService.GetFirstEntity(s => s.Name.Equals("ReceiveEmail")).Value);
  56. }
  57. }
  58. public void PublishPost(Post p)
  59. {
  60. p.Status = Status.Pended;
  61. p.PostDate = DateTime.Now;
  62. p.ModifyDate = DateTime.Now;
  63. Post post = _postService.GetById(p.Id);
  64. if (post is null)
  65. {
  66. _postService.AddEntitySaved(post);
  67. }
  68. else
  69. {
  70. post.Status = Status.Pended;
  71. post.PostDate = DateTime.Now;
  72. post.ModifyDate = DateTime.Now;
  73. _postService.UpdateEntitySaved(post);
  74. }
  75. }
  76. public void RecordPostVisit(int pid)
  77. {
  78. Post post = _postService.GetById(pid);
  79. var record = post.PostAccessRecord.FirstOrDefault(r => r.AccessTime == DateTime.Today);
  80. if (record != null)
  81. {
  82. record.ClickCount += 1;
  83. }
  84. else
  85. {
  86. post.PostAccessRecord.Add(new PostAccessRecord
  87. {
  88. ClickCount = 1,
  89. AccessTime = DateTime.Today
  90. });
  91. }
  92. _postService.UpdateEntitySaved(post);
  93. }
  94. public static void InterceptLog(IpIntercepter s)
  95. {
  96. using (RedisHelper redisHelper = RedisHelper.GetInstance())
  97. {
  98. redisHelper.StringIncrement("interceptCount");
  99. redisHelper.ListLeftPush("intercept", s);
  100. }
  101. }
  102. /// <summary>
  103. /// 每天的任务
  104. /// </summary>
  105. public void EverydayJob()
  106. {
  107. CommonHelper.IPErrorTimes.RemoveWhere(kv => kv.Value < 100); //将访客访问出错次数少于100的移开
  108. _redisHelper.SetString("ArticleViewToken", SnowFlake.GetInstance().GetUniqueShortId(6)); //更新加密文章的密码
  109. _redisHelper.StringIncrement("Interview:RunningDays");
  110. DateTime time = DateTime.Now.AddMonths(-1);
  111. _searchDetailsService.DeleteEntitySaved(s => s.SearchTime < time);
  112. foreach (var p in _postService.GetAll().AsParallel())
  113. {
  114. try
  115. {
  116. p.AverageViewCount = p.PostAccessRecord.Average(r => r.ClickCount);
  117. p.TotalViewCount = p.PostAccessRecord.Sum(r => r.ClickCount);
  118. _postService.UpdateEntitySaved(p);
  119. }
  120. catch (Exception)
  121. {
  122. }
  123. }
  124. }
  125. /// <summary>
  126. /// 检查友链
  127. /// </summary>
  128. public void CheckLinks()
  129. {
  130. var links = _linksService.LoadEntities(l => !l.Except).AsParallel();
  131. Parallel.ForEach(links, link =>
  132. {
  133. Uri uri = new Uri(link.Url);
  134. HttpClient client = _httpClientFactory.CreateClient();
  135. client.DefaultRequestHeaders.UserAgent.Add(ProductInfoHeaderValue.Parse("Mozilla/5.0"));
  136. client.DefaultRequestHeaders.Referrer = new Uri("https://masuit.com");
  137. client.Timeout = TimeSpan.FromHours(10);
  138. client.GetAsync(uri).ContinueWith(async t =>
  139. {
  140. if (t.IsCanceled || t.IsFaulted)
  141. {
  142. link.Status = Status.Unavailable;
  143. return;
  144. }
  145. var res = await t;
  146. if (res.IsSuccessStatusCode)
  147. {
  148. link.Status = !(await res.Content.ReadAsStringAsync()).Contains(CommonHelper.SystemSettings["Domain"]) ? Status.Unavailable : Status.Available;
  149. }
  150. else
  151. {
  152. link.Status = Status.Unavailable;
  153. }
  154. _linksService.UpdateEntity(link);
  155. }).Wait();
  156. });
  157. _linksService.SaveChanges();
  158. }
  159. }
  160. }