From abe80d4eeac16ee26eeef8f5110cca8503a7cfa1 Mon Sep 17 00:00:00 2001 From: wangqing <408564126@qq.com> Date: Sat, 27 Jan 2024 17:24:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E6=B3=A8=E5=A5=BD=E5=8F=8B=E6=97=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=91=E9=80=81=E6=B6=88=E6=81=AF=E4=B8=9A?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/dto/UserMessageDto.java | 15 +++++++++++- .../user/controller/FriendController.java | 23 ++++++++++++++++++- .../user/listener/UserMessageListener.java | 9 ++++++++ .../java/com/luoo/user/pojo/UserMessage.java | 15 +++++++++++- .../luoo/user/service/UserMessageService.java | 3 --- 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/luoo_common/src/main/java/dto/UserMessageDto.java b/luoo_common/src/main/java/dto/UserMessageDto.java index d0c9b9f..c724718 100644 --- a/luoo_common/src/main/java/dto/UserMessageDto.java +++ b/luoo_common/src/main/java/dto/UserMessageDto.java @@ -20,10 +20,23 @@ public class UserMessageDto implements Serializable { private String messageId; /** - * 消息ID + * 接收人ID */ private String userId; + /** + * 消息类型 1新评论 ,2新赞,3新关注, 4私信 5.系统消息 + */ + private Integer type; + + /** + * 当消息类型是新评论 新评论 新赞 新关注 私信时,显示发起用户的头像,当时系统消息时,显示默认系统消息icon。 + */ + private String sendUserAvatar; + + private String sendUserId; + + private String sendUserNickName; /** * 消息标题 diff --git a/luoo_user/src/main/java/com/luoo/user/controller/FriendController.java b/luoo_user/src/main/java/com/luoo/user/controller/FriendController.java index 84a7399..6a167af 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/FriendController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/FriendController.java @@ -3,11 +3,15 @@ package com.luoo.user.controller; import api.Result; import api.StatusCode; +import com.luoo.user.pojo.UserInfo; import com.luoo.user.service.FriendService; import com.luoo.user.service.UserInfoService; +import constants.Constants; import controller.BaseController; import dto.UserLoginDto; +import dto.UserMessageDto; import io.jsonwebtoken.Claims; +import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -26,6 +30,9 @@ public class FriendController extends BaseController { @Autowired private UserInfoService userInfoService; + @Autowired + private RabbitTemplate rabbitTemplate; + /** * 添加好友或者添加非好友(关注还是拉黑) 1是关注,2是拉黑 @@ -37,7 +44,7 @@ public class FriendController extends BaseController { //验证是否登录,并且拿到ID String token = (String) request.getAttribute("claims_user"); - UserLoginDto userLoginDto = getUserLoginDto(token); + UserLoginDto userLoginDto = getUserLoginDto("Bearer "+token); if (null == userLoginDto) { return Result.unauthorized(null); } @@ -52,6 +59,20 @@ public class FriendController extends BaseController { } if (flag == 1) { userInfoService.updatefanscountandfollowcount(1,userid,friendid); + + UserInfo userInfo =userInfoService.findById(userid); + /** + * 向friend发送消息 + */ + UserMessageDto userMessageDto = new UserMessageDto(); + userMessageDto.setType(3); //消息类型 1新评论 ,2新赞,3新关注, 4私信 5.系统消息 + userMessageDto.setUserId(friendid); + userMessageDto.setContent(userInfo.getNickName()+" 关注了你"); + userMessageDto.setTitle("您有新的粉丝"); + userMessageDto.setSendUserAvatar(Constants.RESOURCE_PREFIX+userInfo.getAvatar()); + userMessageDto.setSendUserId(userid); + userMessageDto.setSendUserNickName(userInfo.getNickName()); + rabbitTemplate.convertAndSend("userMessage",userMessageDto); return Result.success(); } diff --git a/luoo_user/src/main/java/com/luoo/user/listener/UserMessageListener.java b/luoo_user/src/main/java/com/luoo/user/listener/UserMessageListener.java index 79a706a..239328a 100644 --- a/luoo_user/src/main/java/com/luoo/user/listener/UserMessageListener.java +++ b/luoo_user/src/main/java/com/luoo/user/listener/UserMessageListener.java @@ -9,6 +9,9 @@ import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import util.IdWorker; + +import java.util.Date; @Component @RabbitListener(queues = "userMessage") @@ -17,12 +20,18 @@ public class UserMessageListener { @Autowired private UserMessageDao userMessageDao; + @Autowired + private IdWorker idWorker; + @RabbitHandler public void executeSendUserMessage(UserMessageDto userMessageDto) { UserMessage userMessage = new UserMessage(); BeanUtils.copyProperties(userMessageDto,userMessage); + userMessage.setMessageId(idWorker.nextId()+""); //id + userMessage.setHaveRead(0); //初始状态未读 + userMessage.setSendTime(new Date()); //消息产生的时间 userMessageDao.save(userMessage); } diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/UserMessage.java b/luoo_user/src/main/java/com/luoo/user/pojo/UserMessage.java index 12102e8..c4b1ea7 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/UserMessage.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/UserMessage.java @@ -22,11 +22,24 @@ public class UserMessage implements Serializable { private String messageId; /** - * 消息ID + * 接收人ID */ private String userId; + /** + * 消息类型 1新评论 ,2新赞,3新关注, 4私信 5.系统消息 + */ + private Integer type; + + /** + * 当消息类型是新评论 新评论 新赞 新关注 私信时,显示发起用户的头像,当时系统消息时,显示默认系统消息icon。 + */ + private String sendUserAvatar; + + private String sendUserId; + + private String sendUserNickName; /** * 消息标题 */ diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserMessageService.java b/luoo_user/src/main/java/com/luoo/user/service/UserMessageService.java index 533c1d9..98ef26a 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserMessageService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserMessageService.java @@ -35,9 +35,6 @@ public class UserMessageService { public void sendUserMessage(UserMessageDto userMessageDto) { - userMessageDto.setMessageId(idWorker.nextId()+""); - userMessageDto.setHaveRead(0); - userMessageDto.setSendTime(new Date()); rabbitTemplate.convertAndSend("userMessage",userMessageDto); }