using CacheManager.Core;
using Masuit.MyBlogs.Core.Common;
using Masuit.MyBlogs.Core.Extensions;
using Masuit.MyBlogs.Core.Infrastructure.Services.Interface;
using Masuit.MyBlogs.Core.Models.DTO;
using Masuit.MyBlogs.Core.Models.Entity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
namespace Masuit.MyBlogs.Core.Controllers
{
///
/// 站内搜索
///
public class SearchController : BaseController
{
///
///
///
public ISearchDetailsService SearchDetailsService { get; set; }
public IPostService PostService { get; set; }
public ICacheManager CacheManager { get; set; }
///
/// 搜索页
///
///
///
///
///
[Route("s/{wd?}/{page:int?}/{size:int?}")]
public ActionResult Search(string wd = "", int page = 1, int size = 15)
{
var nul = new List();
ViewBag.Elapsed = 0;
ViewBag.Total = 0;
ViewBag.PageSize = size;
ViewBag.Keyword = wd;
if (Regex.Match(wd ?? "", CommonHelper.BanRegex).Length + Regex.Match(wd ?? "", CommonHelper.ModRegex).Length > 0)
{
return RedirectToAction("Search", "Search", new { wd = "" });
}
string ip = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
string key = "Search:" + ip;
if (CacheManager.Exists(key))
{
var hotSearches = RedisHelper.Get>("SearchRank:Week").Take(10).ToList();
ViewBag.hotSearches = hotSearches;
ViewBag.ErrorMsg = "10秒内只能搜索1次!";
return View(nul);
}
wd = wd?.Trim().Replace("+", " ");
if (!string.IsNullOrWhiteSpace(wd) && !wd.Contains("锟斤拷"))
{
if (!HttpContext.Session.TryGetValue("search:" + wd, out _) && !HttpContext.Request.IsRobot())
{
SearchDetailsService.AddEntity(new SearchDetails
{
KeyWords = wd,
SearchTime = DateTime.Now,
IP = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString()
});
SearchDetailsService.SaveChanges();
HttpContext.Session.Set("search:" + wd, wd.ToByteArray());
}
var posts = PostService.SearchPage(page, size, wd);
ViewBag.Elapsed = posts.Elapsed;
ViewBag.Total = posts.Total;
if (posts.Total > 1)
{
CacheManager.AddOrUpdate(key, wd, s => wd);
CacheManager.Expire(key, TimeSpan.FromSeconds(10));
}
ViewBag.hotSearches = new List();
return View(posts.Results);
}
ViewBag.hotSearches = RedisHelper.Get>("SearchRank:Week").Take(10).ToList();
return View(nul);
}
///
/// 关键词搜索记录
///
///
///
///
///
[Authority, HttpPost, ResponseCache(Duration = 600, VaryByQueryKeys = new[] { "page", "size", "search" }, VaryByHeader = "Cookie")]
public ActionResult SearchList(int page = 1, int size = 10, string search = "")
{
var where = string.IsNullOrEmpty(search) ? (Expression>)(s => true) : s => s.KeyWords.Contains(search);
var list = SearchDetailsService.GetPages(page, size, out int total, where, s => s.SearchTime, false).ToList();
var pageCount = Math.Ceiling(total * 1.0 / size).ToInt32();
return PageResult(list, pageCount, total);
}
///
/// 热词
///
///
[Authority, HttpPost, ResponseCache(Duration = 600, VaryByHeader = "Cookie")]
public ActionResult HotKey()
{
return ResultData(new
{
month = RedisHelper.Get>("SearchRank:Month"),
week = RedisHelper.Get>("SearchRank:Week"),
today = RedisHelper.Get>("SearchRank:Today")
});
}
///
/// 删除搜索记录
///
///
///
[HttpPost, Authority]
public ActionResult Delete(int id)
{
bool b = SearchDetailsService.DeleteByIdSaved(id);
return ResultData(null, b, b ? "删除成功!" : "删除失败!");
}
}
}