SearchController.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using CacheManager.Core;
  2. using Masuit.MyBlogs.Core.Common;
  3. using Masuit.MyBlogs.Core.Extensions;
  4. using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
  5. using Masuit.MyBlogs.Core.Models.DTO;
  6. using Masuit.MyBlogs.Core.Models.Entity;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Net.Http.Headers;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Linq.Expressions;
  13. using System.Text.RegularExpressions;
  14. namespace Masuit.MyBlogs.Core.Controllers
  15. {
  16. /// <summary>
  17. /// 站内搜索
  18. /// </summary>
  19. public class SearchController : BaseController
  20. {
  21. /// <summary>
  22. ///
  23. /// </summary>
  24. public ISearchDetailsService SearchDetailsService { get; set; }
  25. public IPostService PostService { get; set; }
  26. public ICacheManager<string> CacheManager { get; set; }
  27. /// <summary>
  28. /// 搜索页
  29. /// </summary>
  30. /// <param name="wd"></param>
  31. /// <param name="page"></param>
  32. /// <param name="size"></param>
  33. /// <returns></returns>
  34. [Route("s/{wd?}/{page:int?}/{size:int?}")]
  35. public ActionResult Search(string wd = "", int page = 1, int size = 15)
  36. {
  37. var nul = new List<PostOutputDto>();
  38. ViewBag.Elapsed = 0;
  39. ViewBag.Total = 0;
  40. ViewBag.PageSize = size;
  41. ViewBag.Keyword = wd;
  42. if (Regex.Match(wd ?? "", CommonHelper.BanRegex).Length + Regex.Match(wd ?? "", CommonHelper.ModRegex).Length > 0)
  43. {
  44. return RedirectToAction("Search", "Search", new { wd = "" });
  45. }
  46. string ip = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
  47. string key = "Search:" + ip;
  48. if (CacheManager.Exists(key))
  49. {
  50. var hotSearches = RedisHelper.Get<List<KeywordsRankOutputDto>>("SearchRank:Week").Take(10).ToList();
  51. ViewBag.hotSearches = hotSearches;
  52. ViewBag.ErrorMsg = "10秒内只能搜索1次!";
  53. return View(nul);
  54. }
  55. wd = wd?.Trim().Replace("+", " ");
  56. if (!string.IsNullOrWhiteSpace(wd) && !wd.Contains("锟斤拷"))
  57. {
  58. if (!HttpContext.Session.TryGetValue("search:" + wd, out _) && !HttpContext.Request.IsRobot())
  59. {
  60. SearchDetailsService.AddEntity(new SearchDetails
  61. {
  62. KeyWords = wd,
  63. SearchTime = DateTime.Now,
  64. IP = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString()
  65. });
  66. SearchDetailsService.SaveChanges();
  67. HttpContext.Session.Set("search:" + wd, wd.ToByteArray());
  68. }
  69. var posts = PostService.SearchPage(page, size, wd);
  70. ViewBag.Elapsed = posts.Elapsed;
  71. ViewBag.Total = posts.Total;
  72. if (posts.Total > 1)
  73. {
  74. CacheManager.AddOrUpdate(key, wd, s => wd);
  75. CacheManager.Expire(key, TimeSpan.FromSeconds(10));
  76. }
  77. ViewBag.hotSearches = new List<KeywordsRankOutputDto>();
  78. return View(posts.Results);
  79. }
  80. ViewBag.hotSearches = RedisHelper.Get<List<KeywordsRankOutputDto>>("SearchRank:Week").Take(10).ToList();
  81. return View(nul);
  82. }
  83. /// <summary>
  84. /// 关键词搜索记录
  85. /// </summary>
  86. /// <param name="page"></param>
  87. /// <param name="size"></param>
  88. /// <param name="search"></param>
  89. /// <returns></returns>
  90. [Authority, HttpPost, ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "page", "size", "search" }, VaryByHeader = "Cookie")]
  91. public ActionResult SearchList(int page = 1, int size = 10, string search = "")
  92. {
  93. var where = string.IsNullOrEmpty(search) ? (Expression<Func<SearchDetails, bool>>)(s => true) : s => s.KeyWords.Contains(search);
  94. var list = SearchDetailsService.GetPages<DateTime, SearchDetailsOutputDto>(page, size, out int total, where, s => s.SearchTime, false).ToList();
  95. var pageCount = Math.Ceiling(total * 1.0 / size).ToInt32();
  96. return PageResult(list, pageCount, total);
  97. }
  98. /// <summary>
  99. /// 热词
  100. /// </summary>
  101. /// <returns></returns>
  102. [Authority, HttpPost, ResponseCache(Duration = 600, VaryByHeader = "Cookie")]
  103. public ActionResult HotKey()
  104. {
  105. return ResultData(new
  106. {
  107. month = RedisHelper.Get<List<KeywordsRankOutputDto>>("SearchRank:Month"),
  108. week = RedisHelper.Get<List<KeywordsRankOutputDto>>("SearchRank:Week"),
  109. today = RedisHelper.Get<List<KeywordsRankOutputDto>>("SearchRank:Today")
  110. });
  111. }
  112. /// <summary>
  113. /// 删除搜索记录
  114. /// </summary>
  115. /// <param name="id"></param>
  116. /// <returns></returns>
  117. [HttpPost, Authority]
  118. public ActionResult Delete(int id)
  119. {
  120. bool b = SearchDetailsService.DeleteByIdSaved(id);
  121. return ResultData(null, b, b ? "删除成功!" : "删除失败!");
  122. }
  123. }
  124. }