Merge remote-tracking branch 'origin/master'

This commit is contained in:
sjm
2023-12-22 10:38:44 +08:00
19 changed files with 967 additions and 51 deletions

View File

@@ -1,4 +1,81 @@
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<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)
{
String res = "123";
return res;
}
/**
* 删除个人历史数据
*/
@RequestMapping("/delSearchHistoryByUserId")
public String delSearchHistoryByUserId(String userId, String searchKey){
String res = "123";
return res;
}
}

View File

@@ -0,0 +1,188 @@
package com.lovenav.controller;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.lovenav.entity.UrlCateList;
import com.lovenav.entity.UrlList;
import com.lovenav.service.UrlCateListService;
import com.lovenav.service.UrlListService;
import com.lovenav.vo.CateAndUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@RestController
@RequestMapping("/UrlAndCate")
public class UrlAndCateController {
@Autowired
ObjectMapper objectMapper;
@Autowired
UrlCateListService urlCateListService;
@Autowired
UrlListService urlListService;
//处理JSON
@RequestMapping("/disposeJson")
public String disposeJson(@RequestBody String data2 ,String email)
{
String jsonString;
//先转换成ObjectMapper类型
ObjectNode objectNode = objectMapper.createObjectNode();
try {
JsonNode rootNode = objectMapper.readTree(data2);
disposeBookmarkFunction1(rootNode,"top",email);
HashMap<String, Object> result = new HashMap<>();
result.put("code", 200);
result.put("msg", "查询成功");
jsonString = JSONObject.toJSONString(result);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return jsonString;
}
public void disposeBookmarkFunction1(JsonNode rootNode,String parent,String email)
{
for(int i=0;i<rootNode.size();i++)
{
JsonNode sonNode = rootNode.get(i);
if(String.valueOf(sonNode.get("folder")).equals("true")){
urlCateListService.selectAndInsertUrlCate(email,String.valueOf(sonNode.get("name")),parent);
JsonNode children = sonNode.get("children");
disposeBookmarkFunction1(children, String.valueOf(sonNode.get("name")),email);
}else{
String name = String.valueOf(sonNode.get("name"));
String url = String.valueOf(sonNode.get("url"));
String adddata = String.valueOf(sonNode.get("adddata"));
urlListService.selectCateAndInsertUrl(parent,name,"123",url,email);
System.out.println(name +"---" + url + "---" + adddata + "---" + parent );
}
}
return ;
}
//处理成JSON
@RequestMapping("/disposeBookmarkToJson")
public String disposeBookmarkToJson(Integer userId)
{
List<UrlCateList> urlCateLists = urlCateListService.selectUrListByUserId(userId);
List<UrlList> urlLists = urlListService.selectUrList();
List<CateAndUrl> cateAndUrlList = new ArrayList<>();
//预处理为CateAndUrl对象
for( int i = 0 ; i < urlCateLists.size() ; i++)
{
CateAndUrl cateAndUrl = new CateAndUrl();
cateAndUrl.setFloder("true");
cateAndUrl.setName(urlCateLists.get(i).getName());
cateAndUrl.setParentId(urlCateLists.get(i).getRootCateId());
cateAndUrl.setCateId(urlCateLists.get(i).getId());
cateAndUrlList.add(cateAndUrl);
}
for( int i = 0 ; i < urlLists.size() ; i++)
{
CateAndUrl cateAndUrl = new CateAndUrl();
cateAndUrl.setFloder("false");
cateAndUrl.setName(urlLists.get(i).getName());
cateAndUrl.setParentId(urlLists.get(i).getCateId());
cateAndUrl.setUrlId(urlLists.get(i).getId());
cateAndUrlList.add(cateAndUrl);
}
List<CateAndUrl> parentsList = new ArrayList<>();
//声明返回集合
List<CateAndUrl> resultList = new ArrayList<>();
//对全部数据进行遍历
for (CateAndUrl disease : cateAndUrlList) {
//判断是否是第一梯队,或者说是树的根节点,如果是根节点就加入到父类集合与返回集合
if (disease.getParentId() == 0) {
parentsList.add(disease);
resultList.add(disease);
} else {
//对于不是第一梯队的数据进行遍历
for (CateAndUrl parent : parentsList) {
//对数据的pid与父类集合中的父节点进行配对如果配对成功就把数据加入到父节点中的子节点集合
if (parent.getFloder().equals("true") ) {
if( parent.getCateId() == disease.getParentId())
{
parent.getChildUC().add(disease);
//当前数据有可能是别的数据的父节点,加到父类容器
parentsList.add(disease);
break;
}
}
if (parent.getFloder().equals("false") ) {
if(parent.getUrlId() == disease.getParentId())
{
parent.getChildUC().add(disease);
//当前数据有可能是别的数据的父节点,加到父类容器
parentsList.add(disease);
break;
}
}
}
}
}
ObjectMapper mapper = new ObjectMapper();
String Json;
HashMap<String, Object> result = new HashMap<>();
// java对象转换为json字符换
try {
Json = mapper.writeValueAsString(resultList);
result.put("code", 200);
result.put("msg", "处理成功");
result.put("data", resultList);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return JSONObject.toJSONString(result);
}
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("/clickUrl")
public String clickUrl(String urlId)
{
UrlList urlList = urlListService.selectUrlListByUrlId(Long.valueOf(urlId));
HashMap<String, Object> result = new HashMap<>();
result.put("code", 200);
result.put("msg", "查询成功");
result.put("data",urlList);
String jsonString = JSONObject.toJSONString(result);
return jsonString;
}
}

View File

@@ -1,9 +1,16 @@
package com.lovenav.dao;
import com.lovenav.entity.UrlCateList;
import com.lovenav.entity.UrlList;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Mapper
public interface UrlCateListDao {
int deleteByPrimaryKey(Integer id);
@@ -16,4 +23,11 @@ public interface UrlCateListDao {
int updateByPrimaryKeySelective(UrlCateList record);
int updateByPrimaryKey(UrlCateList record);
public UrlCateList selectCateByNameAnduserId(@Param("name")String name, @Param("userId") int userId);
public List<UrlCateList> selectUrListByUserId(Integer userId);
}

View File

@@ -1,9 +1,14 @@
package com.lovenav.dao;
import com.lovenav.entity.UrlList;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Mapper
public interface UrlListDao {
int deleteByPrimaryKey(Long id);
@@ -16,4 +21,12 @@ public interface UrlListDao {
int updateByPrimaryKeySelective(UrlList record);
int updateByPrimaryKey(UrlList record);
public List<UrlList> selectUrList();
public UrlList selectUrListByInput(String input);
public UrlList selectUrlListByUrlId(Long urlId);
}

View File

@@ -2,9 +2,9 @@ package com.lovenav.dao;
import com.lovenav.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
@Repository
@Mapper
public interface UserDao {
@@ -27,4 +27,5 @@ public interface UserDao {
int updateByEmail(User user);
}

View File

@@ -21,7 +21,7 @@ public class UrlCateList implements Serializable {
/**
* 创建时间
*/
private Integer createtime;
private Long createtime;
/**
* url 数量

View File

@@ -36,7 +36,7 @@ public class UrlList implements Serializable {
/**
* 创建时间
*/
private Integer createtime;
private Long createtime;
/**
* 访问数量

View File

@@ -0,0 +1,12 @@
package com.lovenav.service;
import com.lovenav.entity.UrlCateList;
import java.util.List;
public interface UrlCateListService {
public int selectAndInsertUrlCate(String email , String cateName , String parent);
public List<UrlCateList> selectUrListByUserId(Integer userId);
}

View File

@@ -0,0 +1,13 @@
package com.lovenav.service;
import com.lovenav.entity.UrlList;
import java.util.List;
public interface UrlListService {
public int selectCateAndInsertUrl(String parent,String name , String icon ,String url ,String email );
public List<UrlList> selectUrList();
public UrlList selectUrListByInput(String input);
public UrlList selectUrlListByUrlId(Long urlId);
}

View File

@@ -4,6 +4,7 @@ import com.lovenav.entity.User;
public interface UserService {
public String sendEmailActivecode(User user);

View File

@@ -119,7 +119,8 @@ public class RedisServiceImpl implements RedisService {
if (result.size() > HOT_SEARCH_NUMBER) {
break;
}
Long time = Long.valueOf(Objects.requireNonNull(valueOperations.get(val)));
Long time = Long.valueOf(Objects.requireNonNull(valueOperations.get("search:search-time:"+val)));
//返回最近一个月的数据
if ((now - time) < HOT_SEARCH_TIME) {
result.add(val);
@@ -134,7 +135,8 @@ public class RedisServiceImpl implements RedisService {
if (result.size() > HOT_SEARCH_NUMBER) {
break;
}
Long time = Long.valueOf(Objects.requireNonNull(valueOperations.get(val)));
System.out.println(valueOperations.get(val));
Long time = Long.valueOf(Objects.requireNonNull(valueOperations.get("search:search-time:"+val)));
//返回最近一个月的数据
if ((now - time) < HOT_SEARCH_TIME) {
result.add(val);
@@ -186,6 +188,7 @@ public class RedisServiceImpl implements RedisService {
// 没有的话就插入有的话的直接更新add是有就覆盖没有就插入
zSetOperations.incrementScore(RedisKeyUtils.getHotSearchKey(), searchKey, 1);
valueOperations.getAndSet(RedisKeyUtils.getSearchTimeKey(searchKey), String.valueOf(now));
return 1L;
}catch (Exception e){
logger.error("redis发生异常异常原因",e);

View File

@@ -0,0 +1,56 @@
package com.lovenav.service.serviceImpl;
import com.lovenav.dao.UrlCateListDao;
import com.lovenav.dao.UserDao;
import com.lovenav.entity.UrlCateList;
import com.lovenav.entity.User;
import com.lovenav.service.UrlCateListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
@Transactional
@Service("urlCateListService")
public class UrlCateListServiceImpl implements UrlCateListService {
@Autowired
UrlCateListDao urlCateListDao;
@Autowired
UserDao userDao;
public int selectAndInsertUrlCate(String email , String cateName , String parent )
{
//找用户ID
User user = userDao.selectByEmail(email);
int userId = user.getId();
//找父标签有没有
UrlCateList cateParent = urlCateListDao.selectCateByNameAnduserId(parent,userId);
UrlCateList targetCate = new UrlCateList();
//设置属性
targetCate.setName(cateName);
targetCate.setUserId(userId);
Date date = new Date();
targetCate.setCreatetime(date.getTime());
if(cateParent != null)
{
targetCate.setRootCateId(cateParent.getId());
}else{
targetCate.setRootCateId(0);
}
//插入
int flag = urlCateListDao.insert(targetCate);
return flag;
}
public List<UrlCateList> selectUrListByUserId(Integer userId){
return urlCateListDao.selectUrListByUserId(userId);
}
}

View File

@@ -0,0 +1,63 @@
package com.lovenav.service.serviceImpl;
import com.lovenav.dao.UrlCateListDao;
import com.lovenav.dao.UrlListDao;
import com.lovenav.dao.UserDao;
import com.lovenav.entity.UrlCateList;
import com.lovenav.entity.UrlList;
import com.lovenav.entity.User;
import com.lovenav.service.UrlListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
@Transactional
@Service("urlListService")
public class UrlLiserServiceImpl implements UrlListService {
@Autowired
UrlCateListDao urlCateListDao;
@Autowired
UrlListDao urlListDao;
@Autowired
UserDao userDao;
public int selectCateAndInsertUrl(String parent,String name , String icon ,String url ,String email)
{
//找用户ID
User user = userDao.selectByEmail(email);
int userId = user.getId();
UrlCateList urlCateList = urlCateListDao.selectCateByNameAnduserId(parent,userId);
UrlList urlList = new UrlList();
//设置属性
urlList.setCateId(urlCateList.getId());
urlList.setUrl(url);
urlList.setIcon(icon);
urlList.setName(name);
Date date = new Date();
urlList.setCreatetime(date.getTime());
urlList.setViews(Long.valueOf("0"));
int flag = urlListDao.insert(urlList);
return flag;
}
public List<UrlList> selectUrList(){
return urlListDao.selectUrList();
}
public UrlList selectUrListByInput(String input)
{
return urlListDao.selectUrListByInput(input);
}
public UrlList selectUrlListByUrlId(Long urlId)
{
UrlList urlList = urlListDao.selectUrlListByUrlId(urlId);
urlList.setViews(urlList.getViews()+1);
urlListDao.updateByPrimaryKeySelective(urlList);
return urlList;
}
}

View File

@@ -8,43 +8,18 @@ import com.lovenav.service.UserService;
import com.lovenav.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import javax.annotation.Resource;
import java.util.Date;
import java.util.regex.Pattern;
@Service
@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
@Resource
UserDao userDao;
@Override
public String sendEmailActivecode(User user) {
return EmailUtils.sendEmail(user);
}
@Override
public int UserRegister(User user) {
user.setRoleId(Byte.valueOf("1"));
user.setUserStatus(Byte.valueOf("1"));
user.setUserPassword(MD5Utils.md5(user.getUserPassword()));
Date date=new Date();
user.setUserRegistered(date);
return userDao.insert(user);
}
@Override
public User selectUserAlreadyExist(User user) {
System.out.println(user.getUserEmail());
User user1=userDao.selectByEmail(user.getUserEmail());
return user1;
}
@Override
public User userLogin(User user) {
boolean result;
@@ -82,5 +57,32 @@ public class UserServiceImpl implements UserService {
return userDao.updateByEmail(user);
}
@Override
public String sendEmailActivecode(User user) {
return EmailUtils.sendEmail(user);
}
@Override
public int UserRegister(User user) {
user.setRoleId(Byte.valueOf("1"));
user.setUserStatus(Byte.valueOf("1"));
user.setUserPassword(MD5Utils.md5(user.getUserPassword()));
Date date=new Date();
user.setUserRegistered(date);
return userDao.insert(user);
}
@Override
public User selectUserAlreadyExist(User user) {
System.out.println(user.getUserEmail());
User user1=userDao.selectByEmail(user.getUserEmail());
return user1;
}
}

View File

@@ -0,0 +1,96 @@
package com.lovenav.vo;
import java.util.ArrayList;
import java.util.List;
public class CateAndUrl {
String name;
String icon;
String url;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getFloder() {
return floder;
}
public void setFloder(String floder) {
this.floder = floder;
}
public List<CateAndUrl> getChildUC() {
return childUC;
}
public void setChildUC(List<CateAndUrl> childUC) {
this.childUC = childUC;
}
@Override
public String toString() {
return "cateAndUrl{" +
"name='" + name + '\'' +
", icon='" + icon + '\'' +
", url='" + url + '\'' +
", floder='" + floder + '\'' +
", childUC=" + childUC +
'}';
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
String floder;
public Integer getCateId() {
return cateId;
}
public void setCateId(Integer cateId) {
this.cateId = cateId;
}
public Long getUrlId() {
return urlId;
}
public void setUrlId(Long urlId) {
this.urlId = urlId;
}
int parentId;
Integer cateId;
Long urlId;
List<CateAndUrl> childUC = new ArrayList<>();
}