From b71a1438d89e002debfcc78ec10beff9f3202e99 Mon Sep 17 00:00:00 2001
From: lyp <255258@gongniu.com>
Date: Sun, 14 Jan 2024 15:33:05 +0800
Subject: [PATCH 1/4] =?UTF-8?q?1.=20=E4=BF=9D=E5=AD=98=E9=80=BB=E8=BE=91?=
=?UTF-8?q?=E5=AE=8C=E5=96=84=202.=20=E5=B1=82=E7=BA=A7=E8=AF=84=E8=AE=BA?=
=?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AE=8C=E5=96=84=20=E8=AF=84=E8=AE=BA?=
=?UTF-8?q?=E6=A8=A1=E5=9D=97=E9=80=BB=E8=BE=91=E8=A1=A5=E5=85=85=E6=8F=90?=
=?UTF-8?q?=E4=BA=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
luoo_comment/pom.xml | 6 +
.../comment/controller/CommentController.java | 44 ++----
.../java/com/luoo/comment/dao/CommentDao.java | 4 +-
.../luoo/comment/enums/CommentStatusEnum.java | 29 ++++
.../java/com/luoo/comment/pojo/Comment.java | 124 ++++------------
.../com/luoo/comment/pojo/CommentDto.java | 37 +++++
.../java/com/luoo/comment/pojo/CommentVo.java | 78 ++++++++++
.../luoo/comment/service/CommentService.java | 81 ++---------
.../service/impl/CommentServiceImpl.java | 136 ++++++++++++++++++
9 files changed, 338 insertions(+), 201 deletions(-)
create mode 100644 luoo_comment/src/main/java/com/luoo/comment/enums/CommentStatusEnum.java
create mode 100644 luoo_comment/src/main/java/com/luoo/comment/pojo/CommentDto.java
create mode 100644 luoo_comment/src/main/java/com/luoo/comment/pojo/CommentVo.java
create mode 100644 luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java
diff --git a/luoo_comment/pom.xml b/luoo_comment/pom.xml
index 998f534..f6007ec 100644
--- a/luoo_comment/pom.xml
+++ b/luoo_comment/pom.xml
@@ -34,6 +34,12 @@
spring-cloud-starter-config
+
+ cn.hutool
+ hutool-all
+ 5.8.16
+
+
net.renfei
ip2location
diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java
index 36f4504..7799fbb 100644
--- a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java
+++ b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java
@@ -1,19 +1,20 @@
package com.luoo.comment.controller;
-import com.luoo.comment.pojo.Comment;
-import com.luoo.comment.service.CommentService;
import api.PageResult;
import api.Result;
import api.StatusCode;
-
-import java.util.List;
-
+import com.luoo.comment.pojo.Comment;
+import com.luoo.comment.pojo.CommentDto;
+import com.luoo.comment.pojo.CommentVo;
+import com.luoo.comment.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
+import java.util.List;
+
@RestController
@CrossOrigin
@RequestMapping("/comment")
@@ -25,12 +26,6 @@ public class CommentController {
@Autowired
private RedisTemplate redisTemplate;
- @GetMapping
- public Result> findAll(){
- return Result.success(commentService.findAll());
- }
-
-
@GetMapping("/findByVolid/{volid}")
public Result> findByVolid(@PathVariable String volid){
return Result.success(commentService.findByVolid(volid));
@@ -43,18 +38,9 @@ public class CommentController {
}
- @PostMapping
- public Result save(@RequestBody Comment comment){
- commentService.save(comment);
- return Result.success();
-
- }
-
-
- @PutMapping("/{commentId}")
- public Result update(@PathVariable String commentId,@RequestBody Comment comment){
- comment.set_id(commentId);
- commentService.update(comment);
+ @PostMapping("/save")
+ public Result save(@RequestBody CommentDto commentDto){
+ commentService.save(commentDto);
return Result.success();
}
@@ -63,14 +49,14 @@ public class CommentController {
public Result delete(@PathVariable String commentId){
commentService.deleteById(commentId);
return Result.success();
-
}
- @GetMapping("/{parentid}/{page}/{size}")
- public Result> findByParentid(@PathVariable String parentid,@PathVariable int page,@PathVariable int size){
-
- Page pageData = commentService.findByParentId(parentid,page,size);
- return Result.success(new PageResult(pageData.getTotalElements(),pageData.getContent()));
+ @GetMapping("/findByParentId")
+ public Result> findByParentId(@RequestParam String parentId,
+ @RequestParam int page,
+ @RequestParam int size){
+ Page pageData = commentService.findByParentId(parentId,page,size);
+ return Result.success(new PageResult<>(pageData.getTotalElements(),pageData.getContent()));
}
@PutMapping("/thumbup/{commentId}")
diff --git a/luoo_comment/src/main/java/com/luoo/comment/dao/CommentDao.java b/luoo_comment/src/main/java/com/luoo/comment/dao/CommentDao.java
index 49b9625..9adfced 100644
--- a/luoo_comment/src/main/java/com/luoo/comment/dao/CommentDao.java
+++ b/luoo_comment/src/main/java/com/luoo/comment/dao/CommentDao.java
@@ -9,8 +9,8 @@ import java.util.List;
public interface CommentDao extends MongoRepository {
- public Page findByParentid(String parentid, Pageable pageable);
+ Page findByParentId(String parentId, Pageable pageable);
- public List findByArticleid(String articleid);
+ List findByArticleId(String articleId);
}
diff --git a/luoo_comment/src/main/java/com/luoo/comment/enums/CommentStatusEnum.java b/luoo_comment/src/main/java/com/luoo/comment/enums/CommentStatusEnum.java
new file mode 100644
index 0000000..274389a
--- /dev/null
+++ b/luoo_comment/src/main/java/com/luoo/comment/enums/CommentStatusEnum.java
@@ -0,0 +1,29 @@
+package com.luoo.comment.enums;
+
+public enum CommentStatusEnum {
+
+ /**
+ * 正常
+ */
+ NORMAL(1),
+
+ /**
+ * 删除
+ */
+ DELETE(2),
+
+ /**
+ * 审核中
+ */
+ AUDITING(3);
+
+ private final int value;
+
+ CommentStatusEnum(int value) {
+ this.value = value;
+ }
+
+ public int getValue() {
+ return value;
+ }
+}
diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/Comment.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/Comment.java
index 4c3b7b4..314fd22 100644
--- a/luoo_comment/src/main/java/com/luoo/comment/pojo/Comment.java
+++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/Comment.java
@@ -1,23 +1,24 @@
package com.luoo.comment.pojo;
+import lombok.Data;
+import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Id;
import java.io.Serializable;
import java.util.Date;
+@Data
public class Comment implements Serializable {
@Id
- private String _id;
+ private String id;
// 评论内容
private String content;
// 发布时间
- private Date publishtime;
- private String userid;
- // 昵称
- private String nickname;
- // 访问数
- private Integer visits;
+ private Date publishTime;
+
+ private String userId;
+
// 点赞数
private Integer thumbup;
// 转发数
@@ -26,105 +27,30 @@ public class Comment implements Serializable {
private Integer comment;
// 状态
- private String state;
-
-
- private String parentid;
- private String articleid;
-
- public String get_id() {
- return _id;
- }
-
- public void set_id(String _id) {
- this._id = _id;
- }
-
- public String getContent() {
- return content;
- }
-
- public void setContent(String content) {
- this.content = content;
- }
+ private Integer state;
- public Date getPublishtime() {
- return publishtime;
- }
+ /**
+ * 父节点 ID
+ */
+ private String parentId;
- public void setPublishtime(Date publishtime) {
- this.publishtime = publishtime;
- }
+ private String articleId;
- public String getUserid() {
- return userid;
- }
+ private Integer level;
- public void setUserid(String userid) {
- this.userid = userid;
- }
+ private String targetId;
- public String getNickname() {
- return nickname;
- }
+ /**
+ * 评论类型
+ */
+ private Integer type;
- public void setNickname(String nickname) {
- this.nickname = nickname;
- }
-
- public Integer getVisits() {
- return visits;
- }
-
- public void setVisits(Integer visits) {
- this.visits = visits;
- }
+ private String city;
- public Integer getThumbup() {
- return thumbup;
+ public CommentVo convertVo () {
+ CommentVo commentVo = new CommentVo();
+ BeanUtils.copyProperties(this, commentVo);
+ return commentVo;
}
- public void setThumbup(Integer thumbup) {
- this.thumbup = thumbup;
- }
-
- public Integer getShare() {
- return share;
- }
-
- public void setShare(Integer share) {
- this.share = share;
- }
-
- public Integer getComment() {
- return comment;
- }
-
- public void setComment(Integer comment) {
- this.comment = comment;
- }
-
- public String getState() {
- return state;
- }
-
- public void setState(String state) {
- this.state = state;
- }
-
- public String getParentid() {
- return parentid;
- }
-
- public void setParentid(String parentid) {
- this.parentid = parentid;
- }
-
- public String getArticleid() {
- return articleid;
- }
-
- public void setArticleid(String articleid) {
- this.articleid = articleid;
- }
}
diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentDto.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentDto.java
new file mode 100644
index 0000000..edd315b
--- /dev/null
+++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentDto.java
@@ -0,0 +1,37 @@
+package com.luoo.comment.pojo;
+
+import lombok.Data;
+
+import java.io.Serializable;
+
+@Data
+public class CommentDto implements Serializable {
+
+ /**
+ * 文章 ID
+ */
+ private String articleId;
+
+ /**
+ * 评论内容
+ */
+ private String content;
+
+ /**
+ * 父节点 ID
+ */
+ private String parentId;
+
+ /**
+ * 目标 ID
+ */
+ private String targetId;
+
+ /**
+ * IP 地址
+ */
+ private String ipAddress;
+
+ private String userId;
+
+}
diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentVo.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentVo.java
new file mode 100644
index 0000000..ec880bf
--- /dev/null
+++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentVo.java
@@ -0,0 +1,78 @@
+package com.luoo.comment.pojo;
+
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+public class CommentVo {
+
+ private String id;
+ /**
+ * 评论内容
+ */
+ private String content;
+ /**
+ * 发布时间
+ */
+ private Date publishTime;
+ /**
+ * 用户 ID
+ */
+ private String userId;
+ /**
+ * 昵称
+ */
+ private String nickName;
+ /**
+ * 点赞数
+ */
+ private Integer thumbup;
+ /**
+ * 转发数
+ */
+ private Integer share;
+ /**
+ * 评论数量
+ */
+ private Integer comment;
+ /**
+ * 状态
+ */
+ private String state;
+
+ /**
+ * 父节点 ID
+ */
+ private String parentId;
+
+ /**
+ * 文章
+ */
+ private String articleId;
+
+ /**
+ * 层级
+ */
+ private Integer level;
+
+ /**
+ * 目标ID
+ */
+ private String targetId;
+
+ /**
+ * 目标对象
+ */
+ private CommentVo targetComment;
+
+ /**
+ * 评论类型
+ */
+ private String type;
+
+ /**
+ * 城市
+ */
+ private String city;
+}
diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java b/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java
index 97aad8e..7b371ad 100644
--- a/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java
+++ b/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java
@@ -1,87 +1,26 @@
package com.luoo.comment.service;
-import com.luoo.comment.dao.CommentDao;
import com.luoo.comment.pojo.Comment;
-import org.springframework.beans.factory.annotation.Autowired;
+import com.luoo.comment.pojo.CommentDto;
+import com.luoo.comment.pojo.CommentVo;
import org.springframework.data.domain.Page;
-import org.springframework.data.domain.PageRequest;
-import org.springframework.data.domain.Pageable;
-import org.springframework.data.mongodb.core.MongoTemplate;
-import org.springframework.data.mongodb.core.query.Criteria;
-import org.springframework.data.mongodb.core.query.Query;
-import org.springframework.data.mongodb.core.query.Update;
-import org.springframework.stereotype.Service;
-import org.springframework.transaction.annotation.Transactional;
-import util.IdWorker;
-import java.util.Date;
import java.util.List;
-@Service
-@Transactional
-public class CommentService {
+public interface CommentService {
- @Autowired
- private CommentDao commentDao;
+ Comment findById(String id);
- @Autowired
- private IdWorker idWorker;
+ void save(CommentDto commentDto);
- @Autowired
- private MongoTemplate mongoTemplate;
- public List findAll() {
- return commentDao.findAll();
- }
+ void update(Comment comment);
- public Comment findById(String id) {
- return commentDao.findById(id).get();
- }
+ void deleteById(String id);
- public void save(Comment comment){
- comment.set_id(idWorker.nextId()+"");
- comment.setPublishtime(new Date());
- comment.setVisits(0);
- comment.setShare(0);
- comment.setThumbup(0);
- comment.setComment(0);
- comment.setState("1");
- // 如果当前添加的吐槽,有父节点,那么父节点的吐槽回复数要加一
- if (comment.getParentid()!=null && !"".equals(comment.getParentid())) {
- Query query = new Query();
- query.addCriteria(Criteria.where("_id").is(comment.getParentid()));
- Update update = new Update();
- update.inc("comment",1);
- mongoTemplate.updateFirst(query,update,"spit");
- }
- commentDao.save(comment);
- }
+ Page findByParentId(String parentId, int page, int size);
- public void update(Comment comment) {
- commentDao.save(comment);
- }
+ void thumbup(String commentId);
- public void deleteById(String id) {
- commentDao.deleteById(id);
- }
-
-
- public Page findByParentId(String parentid,int page,int size){
-
- Pageable pageable = PageRequest.of(page-1,size);
- return commentDao.findByParentid(parentid,pageable);
- }
-
- public void thumbup(String commentId) {
-
- Query query = new Query();
- query.addCriteria(Criteria.where("_id").is("1"));
- Update update = new Update();
- update.inc("thumbup",1);
- mongoTemplate.updateFirst(query,update,"spit");
- }
-
- public List findByVolid(String volid) {
- return commentDao.findByArticleid(volid);
- }
+ List findByVolid(String volid);
}
diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java b/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java
new file mode 100644
index 0000000..233dbd2
--- /dev/null
+++ b/luoo_comment/src/main/java/com/luoo/comment/service/impl/CommentServiceImpl.java
@@ -0,0 +1,136 @@
+package com.luoo.comment.service.impl;
+
+
+import cn.hutool.core.util.ObjectUtil;
+import com.luoo.comment.dao.CommentDao;
+import com.luoo.comment.enums.CommentStatusEnum;
+import com.luoo.comment.pojo.Comment;
+import com.luoo.comment.pojo.CommentDto;
+import com.luoo.comment.pojo.CommentVo;
+import com.luoo.comment.service.CommentService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.*;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
+import org.springframework.data.mongodb.core.query.Update;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import util.IdWorker;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+@Service
+@Transactional
+public class CommentServiceImpl implements CommentService {
+
+ @Autowired
+ private CommentDao commentDao;
+
+ @Autowired
+ private IdWorker idWorker;
+
+ @Autowired
+ private MongoTemplate mongoTemplate;
+
+
+ public Comment findById(String id) {
+ return commentDao.findById(id).get();
+ }
+
+ public void save(CommentDto commentDto){
+ Comment comment = new Comment();
+ comment.setContent(commentDto.getContent());
+ comment.setPublishTime(new Date());
+ comment.setUserId(commentDto.getUserId());
+ comment.setComment(0);
+ comment.setParentId(commentDto.getParentId());
+ comment.setArticleId(commentDto.getArticleId());
+ comment.setLevel(1);
+ comment.setTargetId(commentDto.getTargetId());
+ comment.setType(0);
+ comment.setCity("上海");
+ comment.setId(String.valueOf(idWorker.nextId()));
+ comment.setShare(0);
+ comment.setThumbup(0);
+ comment.setComment(0);
+ comment.setState(CommentStatusEnum.NORMAL.getValue());
+ // 如果当前添加的吐槽,有父节点,那么父节点的吐槽回复数要加一
+ if (ObjectUtil.isNotEmpty(comment.getParentId())) {
+ // 更新父节点的评论回复数
+ Query query = new Query();
+ query.addCriteria(Criteria.where("_id").is(comment.getParentId()));
+ Update update = new Update();
+ update.inc("comment",1);
+ mongoTemplate.updateFirst(query,update,Comment.class);
+ }
+
+ if (ObjectUtil.isNotEmpty(comment.getTargetId())) {
+ Comment parentIdComment = mongoTemplate.findById(comment.getTargetId(), Comment.class);
+ comment.setLevel(parentIdComment.getLevel()+1);
+ Query query = new Query();
+ query.addCriteria(Criteria.where("_id").is(comment.getTargetId()));
+ Update update = new Update();
+ update.inc("comment",1);
+ mongoTemplate.updateFirst(query,update,Comment.class);
+ }
+ commentDao.save(comment);
+ }
+
+ public void update(Comment comment) {
+ commentDao.save(comment);
+ }
+
+ public void deleteById(String id) {
+ commentDao.deleteById(id);
+ }
+
+
+ public Page findByParentId(String parentId,int page,int size){
+ Pageable pageable = PageRequest.of(page, size);
+ Query query = new Query();
+ Criteria criteria = Criteria.where("parentId").is(parentId);
+ query.addCriteria(criteria);
+ Sort sort = Sort.by(Sort.Direction.ASC, "age");
+ query.with(sort);
+ query.with(pageable);
+ List comments = mongoTemplate.find(query, Comment.class);
+
+ List commentVos = comments.stream().map(Comment::convertVo).collect(Collectors.toList());
+
+ List targetComments = comments.stream().map(Comment::getTargetId).collect(Collectors.toList());
+
+ Map idCommentMap = commentVos.stream().collect(Collectors.toMap(CommentVo::getId, Function.identity()));
+
+ long total = mongoTemplate.count(query, Comment.class);
+
+// boolean isChange = targetComments.removeAll(idCommentMap.keySet());
+
+ for (CommentVo commentVo : commentVos) {
+ if (commentVo.getLevel() > 2 && ObjectUtil.isNotEmpty(commentVo.getTargetId())) {
+ CommentVo targetCommentVo = idCommentMap.get(commentVo.getTargetId());
+ if (targetCommentVo != null) {
+ commentVo.setTargetComment(targetCommentVo);
+ }
+ }
+ }
+ return new PageImpl<>(commentVos, pageable, total);
+ }
+
+ public void thumbup(String commentId) {
+
+ Query query = new Query();
+ query.addCriteria(Criteria.where("_id").is("1"));
+ Update update = new Update();
+ update.inc("thumbup",1);
+ mongoTemplate.updateFirst(query,update,"spit");
+ }
+
+ public List findByVolid(String volid) {
+ return commentDao.findByArticleId(volid);
+ }
+}
From b0dbe104545295dcb2d60cfdea5742a0779157e5 Mon Sep 17 00:00:00 2001
From: lyp <255258@gongniu.com>
Date: Sun, 14 Jan 2024 16:12:32 +0800
Subject: [PATCH 2/4] =?UTF-8?q?1.=20=E9=80=9A=E8=BF=87ID=E9=9B=86=E5=90=88?=
=?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=94=A8=E6=88=B7=E5=88=97=E8=A1=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../luoo/user/controller/UserController.java | 39 ++++++++++---------
.../main/java/com/luoo/user/dao/UserDao.java | 9 ++++-
.../com/luoo/user/service/UserService.java | 37 ++++++++----------
3 files changed, 45 insertions(+), 40 deletions(-)
diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java
index 1310299..41d453a 100644
--- a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java
+++ b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java
@@ -1,19 +1,8 @@
package com.luoo.user.controller;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.TimeUnit;
-
-import javax.servlet.http.HttpServletResponse;
-
-import org.springframework.beans.BeanUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.domain.Page;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.web.bind.annotation.*;
-
+import api.PageResult;
+import api.Result;
+import api.StatusCode;
import com.luoo.user.annotation.GlobalInterceptor;
import com.luoo.user.annotation.VerifyParam;
import com.luoo.user.constants.Constants;
@@ -23,15 +12,23 @@ import com.luoo.user.enums.VerifyRegexEnum;
import com.luoo.user.pojo.User;
import com.luoo.user.service.UserService;
import com.luoo.user.util.NickNameUtil;
-
-import api.PageResult;
-import api.Result;
-import api.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.web.bind.annotation.*;
import util.IdWorker;
import util.JwtUtil;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
/**
* 控制器层
*
@@ -268,4 +265,10 @@ public class UserController {
return Result.success();
}
+ @DeleteMapping("/{findUserByIds}")
+ public List findUserByIds(@RequestBody List userId){
+ List byIds = userService.findByIds(userId);
+ return byIds;
+ }
+
}
diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserDao.java
index 688daa6..ca5f105 100644
--- a/luoo_user/src/main/java/com/luoo/user/dao/UserDao.java
+++ b/luoo_user/src/main/java/com/luoo/user/dao/UserDao.java
@@ -1,12 +1,13 @@
package com.luoo.user.dao;
+import com.luoo.user.pojo.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
-
-import com.luoo.user.pojo.User;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
+import java.util.List;
+
/**
* 数据访问接口
* @author Administrator
@@ -26,4 +27,8 @@ public interface UserDao extends JpaRepository,JpaSpecificationExec
public User findByLoginname(String loginname);
+
+ @Query("SELECT * FROM tb_user m WHERE m.id IN ?1")
+ List findUserByIds(List ids);
+
}
diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserService.java b/luoo_user/src/main/java/com/luoo/user/service/UserService.java
index d6999d2..9e68602 100644
--- a/luoo_user/src/main/java/com/luoo/user/service/UserService.java
+++ b/luoo_user/src/main/java/com/luoo/user/service/UserService.java
@@ -1,17 +1,11 @@
package com.luoo.user.service;
-import java.util.*;
-import java.util.concurrent.TimeUnit;
-
-import javax.persistence.criteria.CriteriaBuilder;
-import javax.persistence.criteria.CriteriaQuery;
-import javax.persistence.criteria.Predicate;
-import javax.persistence.criteria.Root;
-import javax.servlet.http.HttpServletRequest;
-
+import com.luoo.user.constants.Constants;
+import com.luoo.user.dao.UserDao;
+import com.luoo.user.pojo.User;
+import com.luoo.user.util.NickNameUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
-import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
@@ -19,20 +13,18 @@ import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
-
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;
-
-import com.apifan.common.random.RandomSource;
-import com.luoo.user.constants.Constants;
-import com.luoo.user.dao.UserDao;
-import com.luoo.user.dto.response.UserRespDTO;
-import com.luoo.user.enumerate.Gender;
-import com.luoo.user.pojo.User;
-import com.luoo.user.util.NickNameUtil;
-
import util.JwtUtil;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.Root;
+import javax.servlet.http.HttpServletRequest;
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+
/**
* 服务层
*
@@ -119,6 +111,11 @@ public class UserService {
return userDao.findById(id).get();
}
+
+ public List findByIds(List ids) {
+ return userDao.findUserByIds(ids);
+ }
+
/**
* 增加
*
From 7d984e4c7fa5b7309e2a4a6e70734cf8d8de0043 Mon Sep 17 00:00:00 2001
From: lyp <255258@gongniu.com>
Date: Sun, 14 Jan 2024 16:21:03 +0800
Subject: [PATCH 3/4] 1. fix api request 2. and todo feign
---
luoo_comment/pom.xml | 4 ++++
.../main/java/com/luoo/comment/client/UserClient.java | 11 +++++++++++
.../java/com/luoo/user/controller/UserController.java | 2 +-
3 files changed, 16 insertions(+), 1 deletion(-)
create mode 100644 luoo_comment/src/main/java/com/luoo/comment/client/UserClient.java
diff --git a/luoo_comment/pom.xml b/luoo_comment/pom.xml
index f6007ec..f3d5cee 100644
--- a/luoo_comment/pom.xml
+++ b/luoo_comment/pom.xml
@@ -58,6 +58,10 @@
org.springframework.boot
spring-boot-starter-actuator
+
+ org.springframework.cloud
+ spring-cloud-openfeign-core
+
app
diff --git a/luoo_comment/src/main/java/com/luoo/comment/client/UserClient.java b/luoo_comment/src/main/java/com/luoo/comment/client/UserClient.java
new file mode 100644
index 0000000..8506208
--- /dev/null
+++ b/luoo_comment/src/main/java/com/luoo/comment/client/UserClient.java
@@ -0,0 +1,11 @@
+package com.luoo.comment.client;
+
+
+import org.springframework.cloud.openfeign.FeignClient;
+
+@FeignClient("luoo-user")
+public interface UserClient {
+
+ //todo 根据用户 IDs 获取用户昵称集合
+
+}
diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java
index 41d453a..c1331a4 100644
--- a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java
+++ b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java
@@ -265,7 +265,7 @@ public class UserController {
return Result.success();
}
- @DeleteMapping("/{findUserByIds}")
+ @GetMapping("/findUserByIds")
public List findUserByIds(@RequestBody List userId){
List byIds = userService.findByIds(userId);
return byIds;
From 0bb25f2f73222dc909e9b54f6ea44c2df99f2768 Mon Sep 17 00:00:00 2001
From: lyp <255258@gongniu.com>
Date: Sun, 21 Jan 2024 18:59:47 +0800
Subject: [PATCH 4/4] 1. handle user info
---
.../luoo/comment/config/TokenInterceptor.java | 58 +++++++++++++
.../luoo/comment/constant/GlobalConstant.java | 9 ++
.../comment/controller/BaseController.java | 21 +++++
.../comment/controller/CommentController.java | 12 ++-
.../com/luoo/comment/pojo/LoginAuthDto.java | 29 +++++++
.../luoo/comment/utils/ThreadLocalMap.java | 84 +++++++++++++++++++
6 files changed, 209 insertions(+), 4 deletions(-)
create mode 100644 luoo_comment/src/main/java/com/luoo/comment/config/TokenInterceptor.java
create mode 100644 luoo_comment/src/main/java/com/luoo/comment/constant/GlobalConstant.java
create mode 100644 luoo_comment/src/main/java/com/luoo/comment/controller/BaseController.java
create mode 100644 luoo_comment/src/main/java/com/luoo/comment/pojo/LoginAuthDto.java
create mode 100644 luoo_comment/src/main/java/com/luoo/comment/utils/ThreadLocalMap.java
diff --git a/luoo_comment/src/main/java/com/luoo/comment/config/TokenInterceptor.java b/luoo_comment/src/main/java/com/luoo/comment/config/TokenInterceptor.java
new file mode 100644
index 0000000..256dd1d
--- /dev/null
+++ b/luoo_comment/src/main/java/com/luoo/comment/config/TokenInterceptor.java
@@ -0,0 +1,58 @@
+package com.luoo.comment.config;
+
+import com.luoo.comment.constant.GlobalConstant;
+import com.luoo.comment.pojo.LoginAuthDto;
+import com.luoo.comment.utils.ThreadLocalMap;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.http.HttpHeaders;
+import org.springframework.stereotype.Component;
+import org.springframework.web.servlet.HandlerInterceptor;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+@Component
+@Slf4j
+public class TokenInterceptor implements HandlerInterceptor {
+
+
+ @Override
+ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+ return HandlerInterceptor.super.preHandle(request, response, handler);
+ }
+
+ @Override
+ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
+
+ String token = StringUtils.substringAfter(request.getHeader(HttpHeaders.AUTHORIZATION), "Bearer ");
+ LoginAuthDto loginUser= decode(token);
+ if (loginUser == null) {
+ log.error("获取用户信息失败, 不允许操作,需重新登录");
+ throw new RuntimeException("获取用户信息失败, 不允许操作,需重新登录");
+ }
+ log.info("请求uri => {} method => {} user => {} userId => {} token => {}",
+ request.getRequestURI(), request.getMethod(), loginUser.getLoginName(), loginUser.getUserId(),token);
+ }
+
+ @Override
+ public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
+ ThreadLocalMap.remove(GlobalConstant.TOKEN_AUTH_DTO);
+ }
+
+ /**
+ * 解析token
+ * @param token
+ * @return
+ */
+ public static LoginAuthDto decode(String token){
+// Jwt decode = JwtHelper.decode(token);
+// return decodeLoginUser(decode);
+ // 解析 token
+ return null;
+ }
+
+
+
+}
diff --git a/luoo_comment/src/main/java/com/luoo/comment/constant/GlobalConstant.java b/luoo_comment/src/main/java/com/luoo/comment/constant/GlobalConstant.java
new file mode 100644
index 0000000..12f2ed3
--- /dev/null
+++ b/luoo_comment/src/main/java/com/luoo/comment/constant/GlobalConstant.java
@@ -0,0 +1,9 @@
+package com.luoo.comment.constant;
+
+public class GlobalConstant {
+
+ /**
+ * 全局用户名
+ */
+ public static final String TOKEN_AUTH_DTO = "CURRENT_USER_DTO";
+}
diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/BaseController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/BaseController.java
new file mode 100644
index 0000000..cca3b1d
--- /dev/null
+++ b/luoo_comment/src/main/java/com/luoo/comment/controller/BaseController.java
@@ -0,0 +1,21 @@
+package com.luoo.comment.controller;
+
+import cn.hutool.core.util.ObjectUtil;
+import com.luoo.comment.pojo.LoginAuthDto;
+import com.luoo.comment.utils.ThreadLocalMap;
+
+public class BaseController {
+
+ /**
+ * 全局用户名
+ */
+ public static final String TOKEN_AUTH_DTO = "CURRENT_USER_DTO";
+
+ protected LoginAuthDto getLoginAuthDto() {
+ LoginAuthDto loginAuthDto = (LoginAuthDto) ThreadLocalMap.get(TOKEN_AUTH_DTO);
+ if (ObjectUtil.isEmpty(loginAuthDto)) {
+ throw new RuntimeException();
+ }
+ return loginAuthDto;
+ }
+}
diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java
index 7799fbb..e374675 100644
--- a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java
+++ b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java
@@ -7,6 +7,7 @@ import api.StatusCode;
import com.luoo.comment.pojo.Comment;
import com.luoo.comment.pojo.CommentDto;
import com.luoo.comment.pojo.CommentVo;
+import com.luoo.comment.pojo.LoginAuthDto;
import com.luoo.comment.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
@@ -18,7 +19,7 @@ import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/comment")
-public class CommentController {
+public class CommentController extends BaseController{
@Autowired
private CommentService commentService;
@@ -40,6 +41,8 @@ public class CommentController {
@PostMapping("/save")
public Result save(@RequestBody CommentDto commentDto){
+ LoginAuthDto loginAuthDto = getLoginAuthDto();
+ commentDto.setUserId(loginAuthDto.getUserId());
commentService.save(commentDto);
return Result.success();
@@ -61,12 +64,13 @@ public class CommentController {
@PutMapping("/thumbup/{commentId}")
public Result thumbup(@PathVariable String commentId){
- String userid="111";
- if (redisTemplate.opsForValue().get("thumbup_"+userid)!=null){
+ LoginAuthDto loginAuthDto = getLoginAuthDto();
+ String userId = loginAuthDto.getUserId();
+ if (redisTemplate.opsForValue().get("thumbup_"+userId)!=null){
return Result.failed(StatusCode.COMMENT_REPEAT_SUBMIT);
}
commentService.thumbup(commentId);
- redisTemplate.opsForValue().set("thumbup_"+userid,1);
+ redisTemplate.opsForValue().set("thumbup_"+userId,1);
return Result.success();
}
}
diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/LoginAuthDto.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/LoginAuthDto.java
new file mode 100644
index 0000000..6dee043
--- /dev/null
+++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/LoginAuthDto.java
@@ -0,0 +1,29 @@
+package com.luoo.comment.pojo;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.io.Serializable;
+
+@Data
+@ApiModel(value = "登录人信息")
+public class LoginAuthDto implements Serializable {
+ private static final long serialVersionUID = -1137852221455042256L;
+ @ApiModelProperty(value = "用户ID")
+ private String userId;
+ @ApiModelProperty(value = "登录名")
+ private String loginName;
+ @ApiModelProperty(value = "用户名")
+ private String userName;
+
+ public LoginAuthDto() {
+ }
+
+ public LoginAuthDto(String userId, String loginName, String userName) {
+ this.userId = userId;
+ this.loginName = loginName;
+ this.userName = userName;
+ }
+
+}
diff --git a/luoo_comment/src/main/java/com/luoo/comment/utils/ThreadLocalMap.java b/luoo_comment/src/main/java/com/luoo/comment/utils/ThreadLocalMap.java
new file mode 100644
index 0000000..92bccf6
--- /dev/null
+++ b/luoo_comment/src/main/java/com/luoo/comment/utils/ThreadLocalMap.java
@@ -0,0 +1,84 @@
+package com.luoo.comment.utils;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.util.HashMap;
+import java.util.Map;
+
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public class ThreadLocalMap {
+ /**
+ * The constant threadContext.
+ */
+ private final static ThreadLocal