110 lines
3.8 KiB
Java
110 lines
3.8 KiB
Java
package com.lovenav.controller;
|
||
|
||
import com.alibaba.fastjson2.JSONObject;
|
||
import com.lovenav.entity.UrlList;
|
||
import com.lovenav.filter.SensitiveFilter;
|
||
import com.lovenav.service.RedisService;
|
||
import com.lovenav.service.UrlListService;
|
||
import org.slf4j.Logger;
|
||
import org.slf4j.LoggerFactory;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.web.bind.annotation.RequestMapping;
|
||
import org.springframework.web.bind.annotation.RestController;
|
||
|
||
import java.io.IOException;
|
||
import java.util.HashMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
@RestController
|
||
@RequestMapping("/Search")
|
||
public class SearchController {
|
||
private static Logger logger = LoggerFactory.getLogger(SearchController.class);
|
||
|
||
@Autowired
|
||
RedisService redisService ;
|
||
|
||
@Autowired
|
||
UrlListService urlListService;
|
||
|
||
@RequestMapping("/searchByInput")
|
||
public String searchByInput(String searchKey,String userId ) throws IOException {
|
||
String placeholder = "***";
|
||
//非法敏感词汇判断
|
||
SensitiveFilter filter = SensitiveFilter.getInstance();
|
||
String s = filter.replaceSensitiveWord(searchKey, 1, placeholder);
|
||
System.out.println(s);
|
||
int n = filter.CheckSensitiveWord(searchKey,0,2);
|
||
//存在非法字符
|
||
if(n > 0){
|
||
logger.info("这个人输入了非法字符--> {},不知道他到底要查什么~ userid--> {}",searchKey,1);
|
||
return setResult(500,"查询失败");
|
||
}
|
||
|
||
|
||
redisService.addSearchHistoryByUserId(userId, searchKey);
|
||
redisService.incrementScoreByUserId(searchKey);
|
||
redisService.incrementScore(searchKey);
|
||
|
||
//返回网站数据
|
||
UrlList urlList = urlListService.selectUrListByInput(searchKey);
|
||
HashMap<String, Object> result = new HashMap<>();
|
||
result.put("code", 200);
|
||
result.put("msg", "查询成功");
|
||
result.put("data",urlList);
|
||
String jsonString = JSONObject.toJSONString(result);
|
||
|
||
return jsonString;
|
||
}
|
||
public String setResult(Integer code, String msg) {
|
||
HashMap<String, Object> result = new HashMap<>();
|
||
result.put("code", code);
|
||
result.put("msg", msg);
|
||
String jsonString = JSONObject.toJSONString(result);
|
||
return jsonString;
|
||
}
|
||
/**
|
||
* 获取个人历史数据列表
|
||
*/
|
||
@RequestMapping("/getSearchHistoryByUserId")
|
||
public String getSearchHistoryByUserId(String userId)
|
||
{
|
||
List<String> stringList = redisService.getSearchHistoryByUserId(userId);
|
||
HashMap<String, Object> result = new HashMap<>();
|
||
result.put("code", 200);
|
||
result.put("msg", "查询成功");
|
||
result.put("data",stringList);
|
||
String jsonString = JSONObject.toJSONString(result);
|
||
|
||
return jsonString;
|
||
}
|
||
/**
|
||
* 删除个人历史数据
|
||
*/
|
||
@RequestMapping("/delSearchHistoryByUserId")
|
||
public String delSearchHistoryByUserId(String userId, String searchKey){
|
||
Long f = redisService.delSearchHistoryByUserId(userId,searchKey);
|
||
HashMap<String, Object> result = new HashMap<>();
|
||
result.put("code", 200);
|
||
result.put("msg", "删除结果");
|
||
result.put("data",f);
|
||
String jsonString = JSONObject.toJSONString(result);
|
||
|
||
return jsonString;
|
||
}
|
||
/**
|
||
* 根据searchKey搜索其相关最热的前十名 (如果searchKey为null空,则返回redis存储的前十最热词条)
|
||
*/
|
||
@RequestMapping("/getHotList")
|
||
public String getHotList(String searchKey){
|
||
List<String> stringList = redisService.getHotList(searchKey);
|
||
HashMap<String, Object> result = new HashMap<>();
|
||
result.put("code", 200);
|
||
result.put("msg", "查询结果");
|
||
result.put("data",stringList);
|
||
String jsonString = JSONObject.toJSONString(result);
|
||
|
||
return jsonString;
|
||
}
|
||
}
|