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.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.incrementScore(searchKey); //返回网站数据 UrlList urlList = urlListService.selectUrListByInput(searchKey); HashMap 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 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) { String res = "123"; return res; } /** * 删除个人历史数据 */ @RequestMapping("/delSearchHistoryByUserId") public String delSearchHistoryByUserId(String userId, String searchKey){ String res = "123"; return res; } }