messageStr = new ArrayList<>();
+ for (ObjectError item:errorList) {
+ log.error("数据校验出现问题:{},异常类型:{}",item.getDefaultMessage(),item.getObjectName());
+ //获取错误信息
+ String message = item.getDefaultMessage();
+ //获取错误的属性名字
+ String field = item.getObjectName();
+ messageStr.add(field+":"+message);
+ }
+ return Msg.fail("参数校验错误",400).add("errorMessage",messageStr);
+ }
+
+
+ @ExceptionHandler(value = Throwable.class)
+ public Msg handleException(Throwable throwable){
+ log.error("错误",throwable);
+ return Msg.fail("系统异常");
+ }
+}
diff --git a/src/main/java/com/javaee/newssystem/controller/NewsController.java b/src/main/java/com/javaee/newssystem/controller/NewsController.java
index 34a30f9..5bd1c9a 100644
--- a/src/main/java/com/javaee/newssystem/controller/NewsController.java
+++ b/src/main/java/com/javaee/newssystem/controller/NewsController.java
@@ -2,13 +2,17 @@ package com.javaee.newssystem.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.javaee.newssystem.entity.HotWord;
import com.javaee.newssystem.entity.Msg;
import com.javaee.newssystem.entity.News;
import com.javaee.newssystem.service.impl.NewsServiceImpl;
import com.javaee.newssystem.utils.CheckClassUtil;
+import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
+import java.util.List;
+
/**
*
* 前端控制器
@@ -117,10 +121,7 @@ public class NewsController {
public Msg deleteTeam(Long newsId){
if(newsId!=null){
Boolean b = newsService.deleteNews(newsId);
- if(b){
- Msg msg = Msg.success("删除成功");
- return msg;
- }
+ return b?Msg.success("删除成功"):Msg.fail("删除失败");
}
return Msg.fail("缺少参数");
}
@@ -137,67 +138,23 @@ public class NewsController {
return Msg.fail("缺少参数");
}
-// //获取已通过消息
-// @GetMapping("/pass")
-// public Msg PassNews(Integer current,Integer size){
-// if(current!=null && size!=null){
-// Page page=newsService.GetPassList(current,size);
-// Msg msg=Msg.success("获取成功");
-// msg.add("data",page);
-// return msg;
-// }
-// return Msg.fail("缺少参数");
-// }
-//
-// //获取未通过消息
-// @GetMapping("/unpass")
-// public Msg UnPassNews(Integer current,Integer size){
-// if(current!=null && size!=null){
-// Page page=newsService.GetUnPassList(current,size);
-// Msg msg=Msg.success("获取成功");
-// msg.add("data",page);
-// return msg;
-// }
-// return Msg.fail("缺少参数");
-// }
-//
-// //获取未审核
-// @GetMapping("/unreviewed")
-// public Msg UnReviewedNews(Integer current,Integer size){
-// if(current!=null && size!=null){
-// Page page=newsService.GetUnReviewed(current,size);
-// Msg msg=Msg.success("获取成功");
-// msg.add("data",page);
-// return msg;
-// }
-// return Msg.fail("缺少参数");
-// }
-//
-// //获取用户已保存
-// @GetMapping("/save")
-// public Msg SaveNews(Long userId,Integer current,Integer size){
-// if(current!=null && size!=null){
-// Page page=newsService.GetSaveNews(userId,current,size);
-// Msg msg=Msg.success("获取成功");
-// msg.add("data",page);
-// return msg;
-// }
-// return Msg.fail("缺少参数");
-// }
-//
-// //管理员审核新闻
-// @PostMapping("/pass")
-// public Msg PassNews(Long newsId){
-// if(newsId!=null){
-// Boolean b = newsService.PassNews(newsId);
-// if(b){
-// Msg msg = Msg.success("审核成功");
-// return msg;
-// }
-// else{
-// return Msg.fail("状态码错误");
-// }
-// }
-// return Msg.fail("缺少参数");
-// }
+// 根据关键词查找消息
+ @GetMapping("search")
+ public Msg searchNews(String key,Integer current,Integer size){
+ if(!StringUtils.isNullOrEmpty(key)){
+ Page page = newsService.searchNews(key,current,size);
+ return Msg.success("获取成功").add("data",page);
+ }
+ return Msg.fail("缺少参数");
+ }
+
+// 获取热搜
+ @GetMapping("/topsearch")
+ public Msg getTopsearch(Integer number){
+ if (number==null){
+ number = 10;
+ }
+ List hotWords = newsService.getHotKey(number);
+ return Msg.success("获取成功").add("data",hotWords);
+ }
}
diff --git a/src/main/java/com/javaee/newssystem/controller/RedisController.java b/src/main/java/com/javaee/newssystem/controller/RedisController.java
new file mode 100644
index 0000000..8f0d510
--- /dev/null
+++ b/src/main/java/com/javaee/newssystem/controller/RedisController.java
@@ -0,0 +1,17 @@
+package com.javaee.newssystem.controller;
+
+
+import com.javaee.newssystem.entity.Msg;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class RedisController {
+
+ @GetMapping("getHotKey")
+ public Msg getHotKey(){
+
+ return null;
+ }
+
+}
diff --git a/src/main/java/com/javaee/newssystem/controller/UserController.java b/src/main/java/com/javaee/newssystem/controller/UserController.java
index 928984a..cd48e8f 100644
--- a/src/main/java/com/javaee/newssystem/controller/UserController.java
+++ b/src/main/java/com/javaee/newssystem/controller/UserController.java
@@ -9,13 +9,12 @@ import com.javaee.newssystem.utils.CheckClassUtil;
import com.javaee.newssystem.utils.JwtUtil;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
-import java.util.Date;
-import java.util.Enumeration;
import java.util.List;
/**
@@ -35,15 +34,14 @@ public class UserController {
// 用户登录
@RequestMapping(value = "/login",method = RequestMethod.POST)
- public Msg Login(String userName,String userPassword){
+ public Msg Login(String userName, String userPassword){
if(StringUtils.isNullOrEmpty(userName)||StringUtils.isNullOrEmpty(userPassword)){
return Msg.fail("登录失败,缺少参数");
}
else {
User user = userService.Login(userName, userPassword);
if (user!=null){
- Msg msg = Msg.success("登录成功");
- msg.add("data",user);
+ Msg msg = Msg.success("登录成功").add("data",user);
// 生成token
msg.add("token", JwtUtil.getToken(user.getUserId(),"用户"));
return msg;
@@ -55,31 +53,21 @@ public class UserController {
// 用户注册
@RequestMapping(value = "/register",method = RequestMethod.POST)
- public Msg Register(User user){
+ public Msg Register(@Validated User user){
if (CheckClassUtil.checkUser(user)){
- userService.Register(user);
- if(user.getUserId()!=null){
- Msg msg = Msg.success("注册成功");
- user.setUserPassword(null);
- msg.add("data",user);
- return msg;
- }
- else
- return Msg.fail("用户名已存在");
+ Boolean b = userService.Register(user);
+ return b?Msg.success("注册成功").add("data",user):Msg.fail("用户名已存在");
}
return Msg.fail("注册失败,缺少参数");
}
// 用户修改信息
@RequestMapping(value = "/update/info",method = RequestMethod.POST)
- public Msg updateInfo(User user,@RequestPart("headImg") MultipartFile headImg){
+ public Msg updateInfo(@Validated User user,@RequestPart("headImg") MultipartFile headImg){
if(user.getUserId()!=null){
user = userService.updateInfo(user, headImg);
if(user!=null){
- Msg msg = Msg.success("修改成功");
- user.setUserPassword(null);
- msg.add("data",user);
- return msg;
+ return Msg.success("修改成功").add("data",user);
}
else
Msg.fail("修改失败");
@@ -102,7 +90,7 @@ public class UserController {
// 用户修改密码
@PostMapping("/update/password")
- public Msg updatePassword(Long userId,String oldPassword,String newPassword){
+ public Msg updatePassword(Long userId, String oldPassword, String newPassword){
if(userId!=null&&CheckClassUtil.checkStringList(oldPassword,newPassword)){
Boolean b = userService.updatePassword(userId,oldPassword,newPassword);
return b?Msg.success("修改密码成功"):Msg.fail("密码错误");
@@ -115,12 +103,7 @@ public class UserController {
public Msg Logout(Long userId){
if(userId!=null){
Boolean b = userService.Logout(userId);
- if(b){
- return Msg.success("注销成功");
- }
- else {
- return Msg.fail("该用户已被注销");
- }
+ return b?Msg.success("注销成功"):Msg.fail("该用户已被注销");
}
return Msg.fail("缺少参数");
}
@@ -130,9 +113,7 @@ public class UserController {
public Msg getFocusUser(Long userId,Integer current,Integer size){
if(userId!=null&¤t!=null&&size!=null){
Page page = userService.getFocusUser(userId, current, size);
- Msg msg = Msg.success("查询成功");
- msg.add("data",page);
- return msg;
+ return Msg.success("查询成功").add("data",page);
}
return Msg.fail("缺少参数");
}
@@ -142,12 +123,7 @@ public class UserController {
public Msg addFocusUser(Long userId,Long focusedUserId){
if(userId!=null&&focusedUserId!=null){
Boolean b = userService.addFocusUser(userId, focusedUserId);
- if(b){
- return Msg.success("添加关注成功");
- }
- else {
- return Msg.fail("添加关注失败");
- }
+ return b?Msg.success("取消关注成功"):Msg.fail("该用户未被关注");
}
return Msg.fail("缺少参数");
}
@@ -157,12 +133,7 @@ public class UserController {
public Msg unfocus(Long userId,Long focusedUserId){
if(userId!=null&&focusedUserId!=null){
Boolean b = userService.unFocus(userId, focusedUserId);
- if(b){
- return Msg.success("取消关注成功");
- }
- else {
- return Msg.fail("该用户未被关注");
- }
+ return b?Msg.success("取消关注成功"):Msg.fail("该用户未被关注");
}
return Msg.fail("缺少参数");
}
@@ -173,9 +144,7 @@ public class UserController {
if(!StringUtils.isNullOrEmpty(userName)){
List userList = userService.search(userName);
if (!userList.isEmpty()){
- Msg msg = Msg.success("查找成功");
- msg.add("data",userList);
- return msg;
+ return Msg.success("查找成功").add("data",userList);
}
else return Msg.fail("不存在此人");
}
diff --git a/src/main/java/com/javaee/newssystem/entity/HotWord.java b/src/main/java/com/javaee/newssystem/entity/HotWord.java
new file mode 100644
index 0000000..9f7f4e2
--- /dev/null
+++ b/src/main/java/com/javaee/newssystem/entity/HotWord.java
@@ -0,0 +1,18 @@
+package com.javaee.newssystem.entity;
+
+
+import lombok.Data;
+
+@Data
+public class HotWord {
+
+ String hotword;
+ Integer num;
+
+
+
+ public HotWord(String hotword, Integer num) {
+ this.hotword = hotword;
+ this.num = num;
+ }
+}
diff --git a/src/main/java/com/javaee/newssystem/entity/Msg.java b/src/main/java/com/javaee/newssystem/entity/Msg.java
index 0f38a69..403ca02 100644
--- a/src/main/java/com/javaee/newssystem/entity/Msg.java
+++ b/src/main/java/com/javaee/newssystem/entity/Msg.java
@@ -1,5 +1,7 @@
package com.javaee.newssystem.entity;
+
+
import java.util.HashMap;
import java.util.Map;
@@ -7,25 +9,31 @@ public class Msg {
private String msg;
// 200:成功,0:失败
private Integer status;
- private Map data = new HashMap<>();
+ private Map data;
public Msg add(String key,Object value){
+ if(data==null){
+ data = new HashMap<>();
+ }
data.put(key,value);
return this;
}
public static Msg success(String msg){
- Msg msgClass = new Msg();
- msgClass.setMsg(msg);
- msgClass.setStatus(200);
- return msgClass;
+ return new Msg(msg,200);
}
public static Msg fail(String msg){
- Msg msgClass = new Msg();
- msgClass.setMsg(msg);
- msgClass.setStatus(0);
- return msgClass;
+ return new Msg(msg,0);
+ }
+
+ public static Msg fail(String msg,Integer status){
+ return new Msg(msg,status);
+ }
+
+ private Msg(String msg, Integer status ) {
+ this.msg = msg;
+ this.status = status;
}
private void setMsg(String msg) {
diff --git a/src/main/java/com/javaee/newssystem/entity/User.java b/src/main/java/com/javaee/newssystem/entity/User.java
index 4aab674..0bdf02b 100644
--- a/src/main/java/com/javaee/newssystem/entity/User.java
+++ b/src/main/java/com/javaee/newssystem/entity/User.java
@@ -2,7 +2,9 @@ package com.javaee.newssystem.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
+import org.springframework.validation.annotation.Validated;
+import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.util.Date;
@@ -14,6 +16,7 @@ import java.util.Date;
* @author qjj
* @since 2022-11-28
*/
+
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@@ -27,21 +30,25 @@ public class User implements Serializable {
/**
* 用户名
*/
+ @Pattern(regexp = "^.{3,20}$",message = "用户名长度必须在3-20之间")
private String userName;
/**
* 密码
*/
+ @Pattern(regexp = "^[a-zA-Z]\\w{5,17}$",message = "密码必须以字母开头,长度在6~18之间,只能包含字母、数字和下划线")
private String userPassword;
/**
* 手机号
*/
+ @Pattern(regexp = "^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$",message = "手机号格式不对")
private String userPhone;
/**
* 邮箱,对照着用户id,登录使用
*/
+ @Pattern(regexp = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$",message = "邮箱格式不对")
private String userEmail;
/**
diff --git a/src/main/java/com/javaee/newssystem/service/INewsService.java b/src/main/java/com/javaee/newssystem/service/INewsService.java
index 3d98e09..372bc9f 100644
--- a/src/main/java/com/javaee/newssystem/service/INewsService.java
+++ b/src/main/java/com/javaee/newssystem/service/INewsService.java
@@ -1,10 +1,13 @@
package com.javaee.newssystem.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.javaee.newssystem.entity.HotWord;
import com.javaee.newssystem.entity.News;
import com.baomidou.mybatisplus.extension.service.IService;
import com.javaee.newssystem.mapper.NewsMapper;
+import java.util.List;
+
/**
*
* 服务类
@@ -44,5 +47,9 @@ public interface INewsService extends IService {
Integer GetFocusNews(Long newsId);
Boolean UnPassNews(Long newsId);
+
+ Page searchNews(String key, Integer current, Integer size);
+
+ List getHotKey(Integer number);
}
diff --git a/src/main/java/com/javaee/newssystem/service/IUserService.java b/src/main/java/com/javaee/newssystem/service/IUserService.java
index 2bb1fe4..43b839c 100644
--- a/src/main/java/com/javaee/newssystem/service/IUserService.java
+++ b/src/main/java/com/javaee/newssystem/service/IUserService.java
@@ -19,7 +19,7 @@ public interface IUserService extends IService {
User Login(String userName,String password);
- void Register(User user);
+ Boolean Register(User user);
User updateInfo(User user, MultipartFile headImg);
diff --git a/src/main/java/com/javaee/newssystem/service/RedisService.java b/src/main/java/com/javaee/newssystem/service/RedisService.java
new file mode 100644
index 0000000..90f72b1
--- /dev/null
+++ b/src/main/java/com/javaee/newssystem/service/RedisService.java
@@ -0,0 +1,9 @@
+package com.javaee.newssystem.service;
+
+import java.util.List;
+
+public interface RedisService {
+
+ List getHotKey();
+
+}
diff --git a/src/main/java/com/javaee/newssystem/service/ScheduledService.java b/src/main/java/com/javaee/newssystem/service/ScheduledService.java
new file mode 100644
index 0000000..7872448
--- /dev/null
+++ b/src/main/java/com/javaee/newssystem/service/ScheduledService.java
@@ -0,0 +1,18 @@
+package com.javaee.newssystem.service;
+
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Service;
+
+@Service
+public class ScheduledService {
+
+ /**
+ * second(秒),minute(分),hour(时),day of month(日),month(月),day of week(周几).
+ *
+ */
+
+// @Scheduled(cron = "0/10 * * * * *")
+ public void hello(){
+ System.out.println("hello(测试定时任务,每10秒输出)");
+ }
+}
diff --git a/src/main/java/com/javaee/newssystem/service/impl/NewsServiceImpl.java b/src/main/java/com/javaee/newssystem/service/impl/NewsServiceImpl.java
index 02bf2bf..7d8e2fe 100644
--- a/src/main/java/com/javaee/newssystem/service/impl/NewsServiceImpl.java
+++ b/src/main/java/com/javaee/newssystem/service/impl/NewsServiceImpl.java
@@ -2,18 +2,21 @@ package com.javaee.newssystem.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.javaee.newssystem.entity.HotWord;
import com.javaee.newssystem.entity.News;
import com.javaee.newssystem.mapper.NewsMapper;
import com.javaee.newssystem.service.INewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.javaee.newssystem.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.core.StringRedisTemplate;
+import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
+import org.w3c.dom.ls.LSInput;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
/**
*
@@ -30,6 +33,12 @@ public class NewsServiceImpl extends ServiceImpl implements IN
@Autowired
NewsMapper newsMapper;
+ @Autowired
+ RedisUtil redisUtil;
+
+ @Autowired
+ RedisTemplate redisTemplate;
+
@Override
public Page GetPublicList(Integer current, Integer size) {
@@ -165,4 +174,32 @@ public class NewsServiceImpl extends ServiceImpl implements IN
newsMapper.insert(news);
}
+ @Override
+ public Page searchNews(String key, Integer current, Integer size) {
+ redisTemplate.opsForZSet().incrementScore("newsHotKey",key,1);
+ QueryWrapper queryWrapper = new QueryWrapper<>();
+ queryWrapper.like("title",key).eq("state","已通过")
+ .or()
+ .like("content",key).eq("state","已通过");
+ Page page = newsMapper.selectPage(new Page<>(current, size), queryWrapper);
+ return page;
+ }
+
+ @Override
+ public List getHotKey(Integer number){
+ List hotWordList = new ArrayList<>();
+ Set> typedTupleSet = redisTemplate.opsForZSet().reverseRangeByScoreWithScores("newsHotKey",1,100);
+ Iterator> iterator = typedTupleSet.iterator();
+ int flag = 0;
+ while (iterator.hasNext()){
+ flag++;
+ ZSetOperations.TypedTuple