From 81a7e669885c459ad43b9273b29530d12226134d Mon Sep 17 00:00:00 2001 From: wangxulei <727869402@qq.com> Date: Wed, 8 Jan 2025 16:39:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B6=E5=88=B0=E7=82=B9=E8=B5=9E=E6=94=B6?= =?UTF-8?q?=E8=97=8F=E5=85=B3=E6=B3=A8=E7=95=99=E8=A8=80=E6=97=B6=E6=8E=A8?= =?UTF-8?q?=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dd/admin/business/api/AuthActionApi.java | 268 ++++++ .../com/dd/admin/business/api/AuthApi.java | 806 ------------------ .../dd/admin/business/api/AuthAuthorApi.java | 175 ++++ .../dd/admin/business/api/AuthChatApi.java | 143 ++++ .../dd/admin/business/api/AuthNoteApi.java | 382 +++++++++ .../business/api/domain/UnReadCountBean.java | 28 + .../business/chat/service/ChatService.java | 1 + .../follow/service/FollowService.java | 3 + .../service/impl/FollowServiceImpl.java | 17 + .../business/reply/service/ReplyService.java | 4 + .../reply/service/impl/ReplyServiceImpl.java | 23 + .../starNotes/service/StarNotesService.java | 3 + .../service/impl/StarNotesServiceImpl.java | 18 + .../upNotes/service/UpNotesService.java | 4 + .../service/impl/UpNotesServiceImpl.java | 19 + .../webSocket/MyWebSocketMsgHandler.java | 2 +- .../admin/business/webSocket/SocketMsg.java | 1 + .../webSocket/handler/P2PMessageHandler.java | 2 +- .../business/webSocket/util/TioUtil.java | 2 +- .../common/utils/SpringContextUtils.java | 52 ++ 20 files changed, 1144 insertions(+), 809 deletions(-) create mode 100644 src/main/java/com/dd/admin/business/api/AuthActionApi.java delete mode 100644 src/main/java/com/dd/admin/business/api/AuthApi.java create mode 100644 src/main/java/com/dd/admin/business/api/AuthAuthorApi.java create mode 100644 src/main/java/com/dd/admin/business/api/AuthChatApi.java create mode 100644 src/main/java/com/dd/admin/business/api/AuthNoteApi.java create mode 100644 src/main/java/com/dd/admin/business/api/domain/UnReadCountBean.java create mode 100644 src/main/java/com/dd/admin/business/webSocket/SocketMsg.java create mode 100644 src/main/java/com/dd/admin/common/utils/SpringContextUtils.java diff --git a/src/main/java/com/dd/admin/business/api/AuthActionApi.java b/src/main/java/com/dd/admin/business/api/AuthActionApi.java new file mode 100644 index 0000000..774451d --- /dev/null +++ b/src/main/java/com/dd/admin/business/api/AuthActionApi.java @@ -0,0 +1,268 @@ +package com.dd.admin.business.api; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.collection.CollectionUtil; +import com.dd.admin.business.author.entity.Author; +import com.dd.admin.business.author.service.AuthorService; +import com.dd.admin.business.chat.service.ChatService; +import com.dd.admin.business.file.service.FileService; +import com.dd.admin.business.follow.domain.FollowDto; +import com.dd.admin.business.follow.entity.Follow; +import com.dd.admin.business.follow.service.FollowService; +import com.dd.admin.business.note.domain.NoteDto; +import com.dd.admin.business.note.entity.Note; +import com.dd.admin.business.note.service.NoteService; +import com.dd.admin.business.noteImg.service.NoteImgService; +import com.dd.admin.business.reply.domain.ReplyDto; +import com.dd.admin.business.reply.domain.ReplyVo; +import com.dd.admin.business.reply.entity.Reply; +import com.dd.admin.business.reply.service.ReplyService; +import com.dd.admin.business.starNotes.entity.StarNotes; +import com.dd.admin.business.starNotes.service.StarNotesService; +import com.dd.admin.business.upNotes.entity.UpNotes; +import com.dd.admin.business.upNotes.service.UpNotesService; +import com.dd.admin.business.upReplys.entity.UpReplys; +import com.dd.admin.business.upReplys.service.UpReplysService; +import com.dd.admin.business.webSocket.MyWebSocketMsgHandler; +import com.dd.admin.business.webSocket.util.TioUtil; +import com.dd.admin.common.aop.operationLog.aop.OperLog; +import com.dd.admin.common.aop.operationLog.aop.OperType; +import com.dd.admin.common.exception.ApiException; +import com.dd.admin.common.model.result.ResultBean; +import com.dd.admin.common.utils.AddressUtils; +import com.dd.admin.common.utils.IPUtils; +import com.dd.admin.common.utils.SpringContextUtils; +import com.dd.admin.common.utils.StringUtil; +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Lazy; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; +import org.tio.websocket.starter.TioWebSocketServerBootstrap; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; + +import java.util.List; + +import static com.dd.admin.common.consts.XhsConst.TRUE; +@ApiModel("用户操作类Api") +@RestController +public class AuthActionApi { + @Autowired + AuthorService authorService; + @Autowired + HttpServletRequest request; + @Autowired + NoteService noteService; + @Value("${server.port}") + String port; + @Autowired + UpNotesService upNotesService; + @Autowired + StarNotesService starNotesService; + @Autowired + FollowService followService; + @Autowired + ReplyService replyService; + @Autowired + UpReplysService upReplysService; + @Autowired + private TioWebSocketServerBootstrap bootstrap; + + @ApiOperation(value = "关注博主") + @ApiOperationSupport(order = 1) + @PostMapping("/api/auth/followAuthor") + @OperLog(operModule = "关注博主",operType = OperType.ADD,operDesc = "关注博主") + public ResultBean followAuthor(@RequestBody FollowDto followDto) { + String followId = String.valueOf(request.getAttribute("authorId")); + Author follow = authorService.getById(followId); + //查看在不在点赞列表 + Follow oneByFollow= followService.selectOneByFollowId(followDto.getAuthorId(), followId); + //不在证明是点赞 + if(oneByFollow==null){ + String authorId = followDto.getAuthorId(); + Author author = authorService.getById(authorId); + + oneByFollow = new Follow(); + oneByFollow.setAuthorId(author.getAuthorId()); + oneByFollow.setAuthorName(author.getAuthorName()); + oneByFollow.setFollowId(follow.getAuthorId()); + oneByFollow.setFollowName(follow.getAuthorName()); + followService.save(oneByFollow); + + TioUtil.sendChatMessageToUser( bootstrap.getServerGroupContext(),author.getAuthorId(),"4",oneByFollow); + }else{ + throw new ApiException("已经关注过了~"); + } + return ResultBean.success(); + }; + + @ApiOperation(value = "取消关注博主") + @ApiOperationSupport(order = 1) + @PostMapping("/api/auth/cancelfollowAuthor") + @OperLog(operModule = "取消关注博主",operType = OperType.ADD,operDesc = "取消关注博主") + public ResultBean cancelfollowAuthor(@RequestBody FollowDto followDto) { + String followId = String.valueOf(request.getAttribute("authorId")); + Author follow = authorService.getById(followId); + //查看在不在关注列表 + Follow oneByFollow= followService.selectOneByFollowId(followDto.getAuthorId(), followId); + //不为空证明关注过 + if(oneByFollow!=null){ + String authorId = followDto.getAuthorId(); + Author author = authorService.getById(authorId); + + oneByFollow.setAuthorId(author.getAuthorId()); + oneByFollow.setAuthorName(author.getAuthorName()); + oneByFollow.setFollowId(follow.getAuthorId()); + oneByFollow.setFollowName(follow.getAuthorName()); + followService.removeById(oneByFollow); + }else{ + throw new ApiException("你还未关注博主~"); + } + return ResultBean.success(); + }; + + @ApiOperation(value = "点赞笔记") + @ApiOperationSupport(order = 1) + @PostMapping("/api/auth/upNote") + @OperLog(operModule = "点赞笔记",operType = OperType.ADD,operDesc = "点赞笔记") + public ResultBean upNote(@RequestBody NoteDto noteDto) { + String followId = String.valueOf(request.getAttribute("authorId")); + Author follow = authorService.getById(followId); + Boolean isUp = Boolean.FALSE; + //查看在不在点赞列表 + UpNotes upNotes = upNotesService.selectOneByFollowId(noteDto.getNoteId(), followId); + //不在证明是点赞 + if(upNotes==null){ + upNotes = new UpNotes(); + upNotes.setAuthorId(noteDto.getAuthorId()); + upNotes.setAuthorName(noteDto.getAuthorName()); + upNotes.setFollowId(follow.getAuthorId()); + upNotes.setFollowName(follow.getAuthorName()); + upNotes.setNoteId(noteDto.getNoteId()); + upNotes.setNoteTitle(noteDto.getNoteTitle()); + isUp = Boolean.TRUE; + upNotesService.save(upNotes); + + //发送点赞信息 + TioUtil.sendChatMessageToUser( bootstrap.getServerGroupContext(),noteDto.getAuthorId(),"1",upNotes); + + }else{ + //在则表示取消赞删除数据 + upNotesService.removeById(upNotes); + } + return ResultBean.success(isUp); + }; + + @ApiOperation(value = "收藏笔记") + @ApiOperationSupport(order = 1) + @PostMapping("/api/auth/starNote") + @OperLog(operModule = "收藏笔记",operType = OperType.ADD,operDesc = "收藏笔记") + public ResultBean starNote(@RequestBody NoteDto noteDto) { + String followId = String.valueOf(request.getAttribute("authorId")); + Author follow = authorService.getById(followId); + Boolean isStar = Boolean.FALSE; + //查看在不在点赞列表 + StarNotes starNotes = starNotesService.selectOneByFollowId(noteDto.getNoteId(), followId); + //不在证明是点赞 + if(starNotes==null){ + starNotes = new StarNotes(); + starNotes.setAuthorId(noteDto.getAuthorId()); + starNotes.setAuthorName(noteDto.getAuthorName()); + starNotes.setFollowId(follow.getAuthorId()); + starNotes.setFollowName(follow.getAuthorName()); + starNotes.setNoteId(noteDto.getNoteId()); + starNotes.setNoteTitle(noteDto.getNoteTitle()); + isStar = Boolean.TRUE; + starNotesService.save(starNotes); + + //发送点赞信息 + TioUtil.sendChatMessageToUser( bootstrap.getServerGroupContext(),noteDto.getAuthorId(),"2",starNotes); + }else{ + //在则表示取消赞删除数据 + starNotesService.removeById(starNotes); + } + return ResultBean.success(isStar); + }; + + @ApiOperation(value = "点赞评论") + @ApiOperationSupport(order = 1) + @PostMapping("/api/auth/upReply") + @OperLog(operModule = "点赞评论",operType = OperType.ADD,operDesc = "点赞评论") + public ResultBean upReply(@RequestBody ReplyDto replyDto) { + String followId = String.valueOf(request.getAttribute("authorId")); + Author follow = authorService.getById(followId); + Boolean isUp = Boolean.FALSE; + //查看在不在点赞列表 + UpReplys upReplys = upReplysService.selectOneByFollowId(replyDto.getReplyId(), followId); + //不在证明是点赞 + if(upReplys==null){ + upReplys = new UpReplys(); + upReplys.setAuthorId(replyDto.getAuthorId()); + upReplys.setAuthorName(replyDto.getAuthorName()); + upReplys.setFollowId(follow.getAuthorId()); + upReplys.setFollowName(follow.getAuthorName()); + upReplys.setReplyId(replyDto.getReplyId()); + upReplys.setReplyContent(replyDto.getReplayContent()); + isUp = Boolean.TRUE; + upReplysService.save(upReplys); + }else{ + //在则表示取消赞删除数据 + upReplysService.removeById(upReplys); + } + return ResultBean.success(isUp); + }; + + @ApiOperation(value = "回复笔记") + @ApiOperationSupport(order = 1) + @PostMapping("/api/auth/replyNote") + @OperLog(operModule = "回复笔记",operType = OperType.ADD,operDesc = "回复笔记") + public ResultBean replyNote(@RequestBody ReplyDto replyDto) { + String authorId = String.valueOf(request.getAttribute("authorId")); + Author author = authorService.getById(authorId); + Note note = noteService.getById(replyDto.getNoteId()); + if (authorId.equals(note.getAuthorId())) { + replyDto.setAuthorReplay(TRUE); + } + + //如果上级pid不为空 设置 上级作者名和id + if(StringUtil.isNotEmpty(replyDto.getParentId())){ + Reply parentReply = replyService.getById(replyDto.getParentId()); + replyDto.setParentAuthorName(parentReply.getAuthorName()); + replyDto.setParentAuthorId(parentReply.getAuthorId()); + //如果top不为空 设置第二级回复的 上级作者名和id + }else if(StringUtil.isNotEmpty(replyDto.getTopParentId())){ + Reply parentReply = replyService.getById(replyDto.getTopParentId()); + replyDto.setParentAuthorName(parentReply.getAuthorName()); + replyDto.setParentAuthorId(parentReply.getAuthorId()); + //如果评论没有上级id和顶级id则回复的是笔记的作者 + }else if(StringUtil.isEmpty(replyDto.getParentId())&&StringUtil.isEmpty(replyDto.getTopParentId())){ + replyDto.setParentAuthorName(note.getAuthorName()); + replyDto.setParentAuthorId(note.getAuthorId()); + } + + + List replyList = replyService.selectReplyList(new ReplyDto().setNoteId(replyDto.getNoteId())); + if (CollectionUtil.isEmpty(replyList)) { + replyDto.setFirstReplay(TRUE); + } + replyDto.setNoteId(note.getNoteId()); + replyDto.setNoteTitle(note.getNoteTitle()); + replyDto.setAuthorId(authorId); + replyDto.setAuthorName(author.getAuthorName()); + replyDto.setAvatarUrl(author.getAvatarUrl()); + replyDto.setIpAddress(IPUtils.getIpAddr(request)); // 请求IP + replyDto.setIpRealAddress(AddressUtils.getRealAddress(replyDto.getIpAddress())); + Reply reply = BeanUtil.copyProperties(replyDto, Reply.class); + replyService.save(reply); + + TioUtil.sendChatMessageToUser( bootstrap.getServerGroupContext(),reply.getParentAuthorId(),"3",reply); + + return ResultBean.success(); + } +} diff --git a/src/main/java/com/dd/admin/business/api/AuthApi.java b/src/main/java/com/dd/admin/business/api/AuthApi.java deleted file mode 100644 index 7c27da2..0000000 --- a/src/main/java/com/dd/admin/business/api/AuthApi.java +++ /dev/null @@ -1,806 +0,0 @@ -package com.dd.admin.business.api; - -import cn.hutool.core.bean.BeanUtil; -import cn.hutool.core.collection.CollectionUtil; -import cn.hutool.core.date.DateUtil; -import com.baomidou.mybatisplus.core.metadata.IPage; -import com.baomidou.mybatisplus.core.metadata.OrderItem; -import com.dd.admin.business.author.entity.Author; -import com.dd.admin.business.author.service.AuthorService; -import com.dd.admin.business.chat.domain.ChatDto; -import com.dd.admin.business.chat.domain.ChatVo; -import com.dd.admin.business.chat.entity.Chat; -import com.dd.admin.business.chat.service.ChatService; -import com.dd.admin.business.file.entity.File; -import com.dd.admin.business.file.service.FileService; -import com.dd.admin.business.follow.domain.FollowDto; -import com.dd.admin.business.follow.domain.FollowVo; -import com.dd.admin.business.follow.entity.Follow; -import com.dd.admin.business.follow.service.FollowService; -import com.dd.admin.business.note.domain.NoteDto; -import com.dd.admin.business.note.domain.NoteVo; -import com.dd.admin.business.note.domain.ReplayMeVo; -import com.dd.admin.business.note.domain.UpMeVo; -import com.dd.admin.business.note.entity.Note; -import com.dd.admin.business.note.service.NoteService; -import com.dd.admin.business.noteImg.entity.NoteImg; -import com.dd.admin.business.noteImg.service.NoteImgService; -import com.dd.admin.business.reply.domain.ReplyDto; -import com.dd.admin.business.reply.domain.ReplyVo; -import com.dd.admin.business.reply.entity.Reply; -import com.dd.admin.business.reply.service.ReplyService; -import com.dd.admin.business.starNotes.domain.StarNotesDto; -import com.dd.admin.business.starNotes.domain.StarNotesVo; -import com.dd.admin.business.starNotes.entity.StarNotes; -import com.dd.admin.business.starNotes.service.StarNotesService; -import com.dd.admin.business.upNotes.domain.UpNotesDto; -import com.dd.admin.business.upNotes.domain.UpNotesVo; -import com.dd.admin.business.upNotes.entity.UpNotes; -import com.dd.admin.business.upNotes.service.UpNotesService; -import com.dd.admin.business.upReplys.entity.UpReplys; -import com.dd.admin.business.upReplys.service.UpReplysService; -import com.dd.admin.common.aop.operationLog.aop.OperLog; -import com.dd.admin.common.aop.operationLog.aop.OperType; -import com.dd.admin.common.exception.ApiException; -import com.dd.admin.common.model.result.ResultBean; -import com.dd.admin.common.utils.AddressUtils; -import com.dd.admin.common.utils.CommonUtil; -import com.dd.admin.common.utils.IPUtils; -import com.dd.admin.common.utils.StringUtil; -import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; -import io.swagger.annotations.ApiOperation; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; - -import javax.servlet.http.HttpServletRequest; -import javax.validation.constraints.NotBlank; -import java.util.*; -import java.util.stream.Collectors; - -import static com.dd.admin.common.consts.XhsConst.TRUE; - -@RestController -public class AuthApi { - @Autowired - AuthorService authorService; - @Autowired - HttpServletRequest request; - @Autowired - NoteService noteService; - @Autowired - FileService fileService; - @Autowired - NoteImgService noteImgService; - @Value("${server.port}") - String port; - @Autowired - UpNotesService upNotesService; - @Autowired - StarNotesService starNotesService; - @Autowired - FollowService followService; - @Autowired - ReplyService replyService; - @Autowired - UpReplysService upReplysService; - @Autowired - ChatService chatService; - - @ApiOperation(value = "获取所有笔记") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/notes") - @OperLog(operModule = "获取所有笔记",operType = OperType.QUERY,operDesc = "获取所有笔记") - public ResultBean> page(NoteDto noteDto) { - String followId = String.valueOf(request.getAttribute("authorId")); - noteDto.setFollowId(followId); - IPage pageInfo = noteService.selectNotePage(noteDto); - return ResultBean.success(pageInfo); - } - - - @ApiOperation(value = "获取所有关注笔记") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getFollowNotes") - @OperLog(operModule = "获取所有关注笔记",operType = OperType.QUERY,operDesc = "获取所有关注笔记") - public ResultBean> getFollowNotes() { - String followId = String.valueOf(request.getAttribute("authorId")); - NoteDto noteDto = new NoteDto(); - noteDto.setFollowId(followId); - List follows = followService.selectFollowListByFollowId(followId); - - IPage pageInfo = new IPage() { - @Override - public List orders() { - return null; - } - - @Override - public List getRecords() { - return null; - } - - @Override - public IPage setRecords(List records) { - return null; - } - - @Override - public long getTotal() { - return 0; - } - - @Override - public IPage setTotal(long total) { - return null; - } - - @Override - public long getSize() { - return 0; - } - - @Override - public IPage setSize(long size) { - return null; - } - - @Override - public long getCurrent() { - return 0; - } - - @Override - public IPage setCurrent(long current) { - return null; - } - }; - if(CollectionUtil.isNotEmpty(follows)){ - String authorIds = CommonUtil.getIds(follows, "authorId"); - noteDto.setAuthorIds(authorIds); - pageInfo = noteService.selectNotePage(noteDto); - } - return ResultBean.success(pageInfo); - } - - - @ApiOperation(value = "获取所有点赞笔记") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getUpNotes") - @OperLog(operModule = "获取所有点赞笔记",operType = OperType.QUERY,operDesc = "获取所有点赞笔记") - public ResultBean> getUpNotes() { - String followId = String.valueOf(request.getAttribute("authorId")); - NoteDto noteDto = new NoteDto(); - noteDto.setFollowId(followId); - noteDto.setMyUpById(followId); - IPage pageInfo = noteService.selectNotePage(noteDto); - return ResultBean.success(pageInfo); - } - - - @ApiOperation(value = "获取所有收藏笔记") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getStarNotes") - @OperLog(operModule = "获取所有收藏笔记",operType = OperType.QUERY,operDesc = "获取所有收藏笔记") - public ResultBean> getStarNotes() { - String followId = String.valueOf(request.getAttribute("authorId")); - NoteDto noteDto = new NoteDto(); - noteDto.setFollowId(followId); - noteDto.setMyStarById(followId); - IPage pageInfo = noteService.selectNotePage(noteDto); - return ResultBean.success(pageInfo); - } - - - @ApiOperation(value = "获取博主信息") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getMine") - @OperLog(operModule = "获取当前博主信息",operType = OperType.QUERY,operDesc = "获取当前博主信息") - public ResultBean getMine() { - String authorId = String.valueOf(request.getAttribute("authorId")); - Author author = authorService.getById(authorId); - if(author.getBirth()!=null){ - author.setAge(DateUtil.ageOfNow(author.getBirth())); - } - - - //关注我的列表 - List followMes = followService.selectFollowList(new FollowDto().setAuthorId(author.getAuthorId())); - List myFollows = followService.selectFollowList(new FollowDto().setFollowId(author.getAuthorId())); - author.setFollow(Long.valueOf(myFollows.size())); - author.setFans(Long.valueOf(followMes.size())); - Long upAndStarTotalCount = authorService.selectAuthorUpAndStarTotalCount(authorId); - author.setUpAndStarCount(upAndStarTotalCount); - return ResultBean.success(author); - } - - - @ApiOperation(value = "修改我的信息") - @ApiOperationSupport(order = 1) - @PostMapping("/api/auth/updateMine") - @OperLog(operModule = "修改我的信息",operType = OperType.EDIT,operDesc = "修改我的信息") - public ResultBean updateAuthorBackGround(@RequestBody Author author) { - authorService.updateById(author); - return ResultBean.success(author); - }; - - - - - @ApiOperation(value = "获取关注目标作者的所有粉丝") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getFollowMes") - @OperLog(operModule = "获取关注目标作者的所有粉丝",operType = OperType.QUERY,operDesc = "获取 关注目标作者的所有粉丝") - public ResultBean getFollowMes(String authorId) { - String myId = String.valueOf(request.getAttribute("authorId")); - //关注我的列表 - List followMes = followService.selectFollowMeList(authorId,myId); - return ResultBean.success(followMes); - } - - @ApiOperation(value = "获取关注目标作者的关注的所有博主") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getMyFollows") - @OperLog(operModule = "getMyFollows",operType = OperType.QUERY,operDesc = "获取关注目标作者的所有粉丝") - public ResultBean getMyFollows(String authorId) { - String myId = String.valueOf(request.getAttribute("authorId")); - //关注我的列表 - List myFollows = followService.selectMyFollowList(authorId,myId); - return ResultBean.success(myFollows); - } - - - @ApiOperation(value = "获取关注目标互相关注的人") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getMyFriends") - @OperLog(operModule = "getMyFriends",operType = OperType.QUERY,operDesc = "获取关注目标互相关注的人") - public ResultBean getMyFriends(String authorId) { - //关注我的列表 - List myFollows = followService.getMyFriends(authorId); - myFollows.stream().forEach(followVo -> { - followVo.setIsFollow(Boolean.TRUE); - followVo.setIsFollowMe(Boolean.TRUE); - }); - return ResultBean.success(myFollows); - } - - - @ApiOperation(value = "获取作者信息") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getAuthor") - @OperLog(operModule = "获取作者信息",operType = OperType.QUERY,operDesc = "获取作者信息") - public ResultBean getAuthor(String authorId) { - String followId = String.valueOf(request.getAttribute("authorId")); - - Author author = authorService.getById(authorId); - //我是否关注过 - Follow isFollow= followService.selectOneByFollowId(authorId, followId); - if(isFollow!=null){ - author.setIsFollow(Boolean.TRUE); - } - //是否关注过我 - Follow isFollowMe= followService.selectOneByFollowId(followId, authorId); - if(isFollowMe!=null){ - author.setIsFollowMe(Boolean.TRUE); - } - - //关注我的列表 - List followMes = followService.selectFollowList(new FollowDto().setAuthorId(author.getAuthorId())); - List myFollows = followService.selectFollowList(new FollowDto().setFollowId(author.getAuthorId())); - - author.setFollow(Long.valueOf(myFollows.size())); - author.setFans(Long.valueOf(followMes.size())); - author.setFollowMes(followMes); - author.setMyFollows(myFollows); - Long upAndStarTotalCount = authorService.selectAuthorUpAndStarTotalCount(authorId); - author.setUpAndStarCount(upAndStarTotalCount); - return ResultBean.success(author); - } - - @ApiOperation(value = "修改头像") - @ApiOperationSupport(order = 1) - @PostMapping("/api/auth/updateAuthorAvatar") - @OperLog(operModule = "修改头像",operType = OperType.EDIT,operDesc = "修改头像") - public ResultBean updateAuthorAvatar(String fileId) { - String followId = String.valueOf(request.getAttribute("authorId")); - Author author = authorService.getById(followId); - author.setAuthorId(followId); - author.setAvatarId(fileId); - File file = fileService.selectFileByFileId(fileId); - String serverName = request.getServerName(); - author.setAvatarUrl("http://" + serverName + ":" + port + file.getFilePath()); - authorService.updateById(author); - return ResultBean.success(author); - }; - - @ApiOperation(value = "修改背景图") - @ApiOperationSupport(order = 1) - @PostMapping("/api/auth/updateAuthorBackGround") - @OperLog(operModule = "修改背景图",operType = OperType.EDIT,operDesc = "修改背景图") - public ResultBean updateAuthorBackGround(String fileId) { - String followId = String.valueOf(request.getAttribute("authorId")); - Author author = authorService.getById(followId); - author.setAuthorId(followId); - author.setBackGroundId(fileId); - File file = fileService.selectFileByFileId(fileId); - String serverName = request.getServerName(); - author.setBackGroundUrl("http://" + serverName + ":" + port + file.getFilePath()); - authorService.updateById(author); - return ResultBean.success(author); - }; - - @ApiOperation(value = "获取目标博主笔记") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getAuthorNotes") - @OperLog(operModule = "获取目标博主笔记",operType = OperType.QUERY,operDesc = "获取目标博主笔记") - public ResultBean> getAuthorNotes(String authorId) { - String followId = String.valueOf(request.getAttribute("authorId")); - NoteDto noteDto = new NoteDto(); - noteDto.setFollowId(followId); - noteDto.setAuthorId(authorId); - IPage pageInfo = noteService.selectNotePage(noteDto); - return ResultBean.success(pageInfo); - } - - - @ApiOperation(value = "获取当前博主笔记") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getMineNotes") - @OperLog(operModule = "获取当前博主笔记",operType = OperType.QUERY,operDesc = "获取当前博主笔记") - public ResultBean> getMineNotes() { - String authorId = String.valueOf(request.getAttribute("authorId")); - NoteDto noteDto = new NoteDto(); - noteDto.setFollowId(authorId); - noteDto.setAuthorId(authorId); - IPage pageInfo = noteService.selectNotePage(noteDto); - return ResultBean.success(pageInfo); - } - - @ApiOperation(value = "关注博主") - @ApiOperationSupport(order = 1) - @PostMapping("/api/auth/followAuthor") - @OperLog(operModule = "关注博主",operType = OperType.ADD,operDesc = "关注博主") - public ResultBean followAuthor(@RequestBody FollowDto followDto) { - String followId = String.valueOf(request.getAttribute("authorId")); - Author follow = authorService.getById(followId); - //查看在不在点赞列表 - Follow oneByFollow= followService.selectOneByFollowId(followDto.getAuthorId(), followId); - //不在证明是点赞 - if(oneByFollow==null){ - String authorId = followDto.getAuthorId(); - Author author = authorService.getById(authorId); - - oneByFollow = new Follow(); - oneByFollow.setAuthorId(author.getAuthorId()); - oneByFollow.setAuthorName(author.getAuthorName()); - oneByFollow.setFollowId(follow.getAuthorId()); - oneByFollow.setFollowName(follow.getAuthorName()); - followService.save(oneByFollow); - }else{ - throw new ApiException("已经关注过了~"); - } - return ResultBean.success(); - }; - - - @ApiOperation(value = "取消关注博主") - @ApiOperationSupport(order = 1) - @PostMapping("/api/auth/cancelfollowAuthor") - @OperLog(operModule = "取消关注博主",operType = OperType.ADD,operDesc = "取消关注博主") - public ResultBean cancelfollowAuthor(@RequestBody FollowDto followDto) { - String followId = String.valueOf(request.getAttribute("authorId")); - Author follow = authorService.getById(followId); - //查看在不在关注列表 - Follow oneByFollow= followService.selectOneByFollowId(followDto.getAuthorId(), followId); - //不为空证明关注过 - if(oneByFollow!=null){ - String authorId = followDto.getAuthorId(); - Author author = authorService.getById(authorId); - - oneByFollow.setAuthorId(author.getAuthorId()); - oneByFollow.setAuthorName(author.getAuthorName()); - oneByFollow.setFollowId(follow.getAuthorId()); - oneByFollow.setFollowName(follow.getAuthorName()); - followService.removeById(oneByFollow); - }else{ - throw new ApiException("你还未关注博主~"); - } - return ResultBean.success(); - }; - - - - - @ApiOperation(value = "点赞笔记") - @ApiOperationSupport(order = 1) - @PostMapping("/api/auth/upNote") - @OperLog(operModule = "点赞笔记",operType = OperType.ADD,operDesc = "点赞笔记") - public ResultBean upNote(@RequestBody NoteDto noteDto) { - String followId = String.valueOf(request.getAttribute("authorId")); - Author follow = authorService.getById(followId); - Boolean isUp = Boolean.FALSE; - //查看在不在点赞列表 - UpNotes upNotes = upNotesService.selectOneByFollowId(noteDto.getNoteId(), followId); - //不在证明是点赞 - if(upNotes==null){ - upNotes = new UpNotes(); - upNotes.setAuthorId(noteDto.getAuthorId()); - upNotes.setAuthorName(noteDto.getAuthorName()); - upNotes.setFollowId(follow.getAuthorId()); - upNotes.setFollowName(follow.getAuthorName()); - upNotes.setNoteId(noteDto.getNoteId()); - upNotes.setNoteTitle(noteDto.getNoteTitle()); - isUp = Boolean.TRUE; - upNotesService.save(upNotes); - }else{ - //在则表示取消赞删除数据 - upNotesService.removeById(upNotes); - } - return ResultBean.success(isUp); - }; - - @ApiOperation(value = "收藏笔记") - @ApiOperationSupport(order = 1) - @PostMapping("/api/auth/starNote") - @OperLog(operModule = "收藏笔记",operType = OperType.ADD,operDesc = "收藏笔记") - public ResultBean starNote(@RequestBody NoteDto noteDto) { - String followId = String.valueOf(request.getAttribute("authorId")); - Author follow = authorService.getById(followId); - Boolean isStar = Boolean.FALSE; - //查看在不在点赞列表 - StarNotes starNotes = starNotesService.selectOneByFollowId(noteDto.getNoteId(), followId); - //不在证明是点赞 - if(starNotes==null){ - starNotes = new StarNotes(); - starNotes.setAuthorId(noteDto.getAuthorId()); - starNotes.setAuthorName(noteDto.getAuthorName()); - starNotes.setFollowId(follow.getAuthorId()); - starNotes.setFollowName(follow.getAuthorName()); - starNotes.setNoteId(noteDto.getNoteId()); - starNotes.setNoteTitle(noteDto.getNoteTitle()); - isStar = Boolean.TRUE; - starNotesService.save(starNotes); - }else{ - //在则表示取消赞删除数据 - starNotesService.removeById(starNotes); - } - return ResultBean.success(isStar); - }; - - - @ApiOperation(value = "创建笔记") - @ApiOperationSupport(order = 1) - @PostMapping("/api/auth/addNote") - @OperLog(operModule = "创建笔记",operType = OperType.ADD,operDesc = "创建笔记") - public ResultBean addNote(@RequestBody NoteDto noteDto) { - String authorId = String.valueOf(request.getAttribute("authorId")); - Author author = authorService.getById(authorId); - - Note note = BeanUtil.copyProperties(noteDto, Note.class); - note.setAuthorId(author.getAuthorId()); - note.setAuthorName(author.getAuthorName()); - note.setAuthorAvatar(author.getAvatarUrl()); - note.setIpAddress(IPUtils.getIpAddr(request)); // 请求IP - note.setIpRealAddress(AddressUtils.getRealAddress(note.getIpAddress())); - noteService.save(note); - - List imgs = noteDto.getImgs(); - List noteImgList = new ArrayList<>(); - for(int i=0;i imgs = noteDto.getImgs(); - List noteImgList = new ArrayList<>(); - for(int i=0;i getNoteReply( ReplyDto replyDto) { - String fellowId = String.valueOf(request.getAttribute("authorId")); - - - String noteId = replyDto.getNoteId(); - List replyList = replyService.selectReplyList(new ReplyDto().setNoteId(noteId).setFollowId(fellowId)); - Integer totalCount = replyList.size(); - // 先构建一个以回复ID为键,回复对象为值的Map,方便后续快速查找回复对象 - // 构建以replyId为键,ReplyVo对象为值的Map,方便后续快速查找回复对象 - Map replyMap = replyList.stream() - .collect(Collectors.toMap(ReplyVo::getReplyId, reply -> reply)); - - List secondLevelReplies = replyList - .stream() - .filter(reply -> - (StringUtil.isNotEmpty(reply.getParentId())||StringUtil.isNotEmpty(reply.getTopParentId())) - ).sorted(Comparator.comparing(ReplyVo::getCreateTime)).collect(Collectors.toList()); - secondLevelReplies.forEach(replyVo -> { - if(StringUtil.isNotEmpty(replyVo.getParentId())){ - replyVo.setParentAuthorId(replyMap.get(replyVo.getParentId()).getAuthorId()); - replyVo.setParentAuthorName(replyMap.get(replyVo.getParentId()).getAuthorName()); - } - }); - - // 获取第一层回复(topId为空且parentId也为空) - List firstLevelReplies = replyList.stream() - .filter(reply -> reply.getTopParentId() == null && reply.getParentId() == null) - .collect(Collectors.toList()); - - // 将第二层回复添加到对应的第一层回复下(通过topId与replyId关联) - firstLevelReplies.forEach(firstLevelReply -> { - String firstLevelReplyId = firstLevelReply.getReplyId(); - List relatedSecondLevelReplies = secondLevelReplies.stream() - .filter(secondLevelReply -> firstLevelReplyId.equals(secondLevelReply.getTopParentId())) - .collect(Collectors.toList()); - if (firstLevelReply.getReply() == null) { - firstLevelReply.setReply(new ArrayList<>()); - } - firstLevelReply.getReply().addAll(relatedSecondLevelReplies); - }); - - - Map map = new HashMap(); - map.put("totalCount",totalCount); - map.put("replys",firstLevelReplies); - - - return ResultBean.success(map); - } - - -// // 先按照parentId进行分组,用于后续查找子回复 -// Map> groupedByParentId = replyList.stream() -// .filter(reply -> reply.getParentId()!= null &&!reply.getParentId().isEmpty()) -// .collect(Collectors.groupingBy(ReplyVo::getParentId)); -// -// // 处理每一个顶级回复(parentId为null的回复),递归构建树形结构 -// List resultList = replyList.stream() -// .filter(reply -> reply.getParentId() == null || reply.getParentId().isEmpty()) -// .peek(parentReply -> { -// // 递归设置子回复列表 -// setChildrenReplies(parentReply, groupedByParentId); -// }) -// .collect(Collectors.toList()); - -// // 第一步:将回复列表按照parentId进行分组,key是parentId,value是对应的回复列表 -// Map> groupedByParentId = replyList.stream() -// .filter(reply -> reply.getParentId()!= null &&!reply.getParentId().isEmpty()) -// .collect(Collectors.groupingBy(ReplyVo::getParentId)); -// -// // 第二步:创建一个新的列表,用于存放最终处理后的回复列表,先将没有parentId的元素添加进去 -// List resultList = replyList.stream() -// .filter(reply -> reply.getParentId() == null || reply.getParentId().isEmpty()) -// .collect(Collectors.toList()); -// -// // 第三步:遍历没有parentId的元素,将其对应的子回复列表(通过parentId从分组中获取)设置到Reply属性下 -// resultList.forEach(parentReply -> { -// String parentId = parentReply.getReplyId(); -// List childrenReplies = groupedByParentId.getOrDefault(parentId, new ArrayList<>()); -// parentReply.setReply(childrenReplies); -// }); - - //不再无限极调用 因为前端页面我不会写。。。 采用和官方一样的@形式 - - - private static void setChildrenReplies(ReplyVo parentReply, Map> groupedByParentId) { - String parentId = parentReply.getReplyId(); - List childrenReplies = groupedByParentId.getOrDefault(parentId, new ArrayList<>()); - // 遍历当前层级的子回复,递归设置它们的子回复 - childrenReplies.forEach(childReply -> { - setChildrenReplies(childReply, groupedByParentId); - }); - parentReply.setReply(childrenReplies); - } - - @ApiOperation(value = "回复笔记") - @ApiOperationSupport(order = 1) - @PostMapping("/api/auth/replyNote") - @OperLog(operModule = "回复笔记",operType = OperType.ADD,operDesc = "回复笔记") - public ResultBean replyNote(@RequestBody ReplyDto replyDto) { - String authorId = String.valueOf(request.getAttribute("authorId")); - Author author = authorService.getById(authorId); - Note note = noteService.getById(replyDto.getNoteId()); - if (authorId.equals(note.getAuthorId())) { - replyDto.setAuthorReplay(TRUE); - } - - //如果上级pid不为空 设置 上级作者名和id - if(StringUtil.isNotEmpty(replyDto.getParentId())){ - Reply parentReply = replyService.getById(replyDto.getParentId()); - replyDto.setParentAuthorName(parentReply.getAuthorName()); - replyDto.setParentAuthorId(parentReply.getAuthorId()); - //如果top不为空 设置第二级回复的 上级作者名和id - }else if(StringUtil.isNotEmpty(replyDto.getTopParentId())){ - Reply parentReply = replyService.getById(replyDto.getTopParentId()); - replyDto.setParentAuthorName(parentReply.getAuthorName()); - replyDto.setParentAuthorId(parentReply.getAuthorId()); - //如果评论没有上级id和顶级id则回复的是笔记的作者 - }else if(StringUtil.isEmpty(replyDto.getParentId())&&StringUtil.isEmpty(replyDto.getTopParentId())){ - replyDto.setParentAuthorName(note.getAuthorName()); - replyDto.setParentAuthorId(note.getAuthorId()); - } - - - List replyList = replyService.selectReplyList(new ReplyDto().setNoteId(replyDto.getNoteId())); - if (CollectionUtil.isEmpty(replyList)) { - replyDto.setFirstReplay(TRUE); - } - replyDto.setNoteId(note.getNoteId()); - replyDto.setNoteTitle(note.getNoteTitle()); - replyDto.setAuthorId(authorId); - replyDto.setAuthorName(author.getAuthorName()); - replyDto.setAvatarUrl(author.getAvatarUrl()); - replyDto.setIpAddress(IPUtils.getIpAddr(request)); // 请求IP - replyDto.setIpRealAddress(AddressUtils.getRealAddress(replyDto.getIpAddress())); - Reply reply = BeanUtil.copyProperties(replyDto, Reply.class); - replyService.save(reply); - return ResultBean.success(); - } - - - @ApiOperation(value = "点赞评论") - @ApiOperationSupport(order = 1) - @PostMapping("/api/auth/upReply") - @OperLog(operModule = "点赞评论",operType = OperType.ADD,operDesc = "点赞评论") - public ResultBean upReply(@RequestBody ReplyDto replyDto) { - String followId = String.valueOf(request.getAttribute("authorId")); - Author follow = authorService.getById(followId); - Boolean isUp = Boolean.FALSE; - //查看在不在点赞列表 - UpReplys upReplys = upReplysService.selectOneByFollowId(replyDto.getReplyId(), followId); - //不在证明是点赞 - if(upReplys==null){ - upReplys = new UpReplys(); - upReplys.setAuthorId(replyDto.getAuthorId()); - upReplys.setAuthorName(replyDto.getAuthorName()); - upReplys.setFollowId(follow.getAuthorId()); - upReplys.setFollowName(follow.getAuthorName()); - upReplys.setReplyId(replyDto.getReplyId()); - upReplys.setReplyContent(replyDto.getReplayContent()); - isUp = Boolean.TRUE; - upReplysService.save(upReplys); - }else{ - //在则表示取消赞删除数据 - upReplysService.removeById(upReplys); - } - return ResultBean.success(isUp); - }; - - - - @ApiOperation(value = "获取聊天记录") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getChatList") - @OperLog(operModule = "获取所有笔记",operType = OperType.QUERY,operDesc = "获取聊天记录") - public ResultBean> getChatList( ChatDto chatDto) { - String followId = String.valueOf(request.getAttribute("authorId")); - Author follow = authorService.getById(followId); - chatDto.setToId(followId); - chatDto.setToName(follow.getAuthorName()); - IPage chatVoIPage = chatService.selectChatPage(chatDto); - - return ResultBean.success(chatVoIPage); - } - - - @ApiOperation(value = "获取聊天列表") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getMessageList") - @OperLog(operModule = "获取聊天列表",operType = OperType.QUERY,operDesc = "获取聊天列表") - public ResultBean> getMessageList( ChatDto chatDto) { - String followId = String.valueOf(request.getAttribute("authorId")); - List chatVos = chatService.selectChatList(followId); - return ResultBean.success(chatVos); - } - - @ApiOperation(value = "读取消息") - @ApiOperationSupport(order = 1) - @PostMapping("/api/auth/readAuthorMessage") - @OperLog(operModule = "读取消息",operType = OperType.OTHER,operDesc = "读取消息") - public ResultBean readAuthorMessage(String authorId) { - String loginId = String.valueOf(request.getAttribute("authorId")); - chatService.readMessage(authorId,loginId); - return ResultBean.success(loginId); - }; - - - @ApiOperation(value = "查询我的未读消息数量") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getUnReadCount") - @OperLog(operModule = "查询我的未读消息数量",operType = OperType.OTHER,operDesc = "查询我的未读消息数量") - public ResultBean getUnReadCount() { - String loginId = String.valueOf(request.getAttribute("authorId")); - Integer unReadCount = chatService.selectUnReadCount(loginId); - return ResultBean.success(unReadCount); - }; - - - @ApiOperation(value = "获取所有收藏点赞我的笔记记录") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getUpMeNotes") - @OperLog(operModule = "获取所有收藏点赞我的笔记记录",operType = OperType.QUERY,operDesc = "获取所有收藏点赞我的笔记记录") - public ResultBean> getUpMeNotes() { - String authorId = String.valueOf(request.getAttribute("authorId")); - IPage upMeVoIPage = noteService.selectUpMeNotes(authorId); - return ResultBean.success(upMeVoIPage); - } - - - @ApiOperation(value = "获取所有评论我的笔记记录") - @ApiOperationSupport(order = 1) - @GetMapping("/api/auth/getReplayMes") - @OperLog(operModule = "获取所有评论我的笔记记录",operType = OperType.QUERY,operDesc = "获取所有评论我的笔记记录") - public ResultBean> getReplayMes() { - String authorId = String.valueOf(request.getAttribute("authorId")); - IPage replyMeNotes = noteService.selectReplyMeNotes(authorId); - return ResultBean.success(replyMeNotes); - } -} diff --git a/src/main/java/com/dd/admin/business/api/AuthAuthorApi.java b/src/main/java/com/dd/admin/business/api/AuthAuthorApi.java new file mode 100644 index 0000000..4235d75 --- /dev/null +++ b/src/main/java/com/dd/admin/business/api/AuthAuthorApi.java @@ -0,0 +1,175 @@ +package com.dd.admin.business.api; + +import cn.hutool.core.date.DateUtil; +import com.dd.admin.business.author.entity.Author; +import com.dd.admin.business.author.service.AuthorService; +import com.dd.admin.business.chat.service.ChatService; +import com.dd.admin.business.file.entity.File; +import com.dd.admin.business.file.service.FileService; +import com.dd.admin.business.follow.domain.FollowDto; +import com.dd.admin.business.follow.domain.FollowVo; +import com.dd.admin.business.follow.entity.Follow; +import com.dd.admin.business.follow.service.FollowService; +import com.dd.admin.business.note.service.NoteService; +import com.dd.admin.business.noteImg.service.NoteImgService; +import com.dd.admin.business.reply.service.ReplyService; +import com.dd.admin.business.starNotes.service.StarNotesService; +import com.dd.admin.business.upNotes.service.UpNotesService; +import com.dd.admin.business.upReplys.service.UpReplysService; +import com.dd.admin.common.aop.operationLog.aop.OperLog; +import com.dd.admin.common.aop.operationLog.aop.OperType; +import com.dd.admin.common.model.result.ResultBean; +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; +@ApiModel("博主类Api") +@RestController +public class AuthAuthorApi { + @Autowired + AuthorService authorService; + @Autowired + HttpServletRequest request; + @Autowired + FileService fileService; + @Value("${server.port}") + String port; + @Autowired + FollowService followService; + @ApiOperation(value = "获取博主信息") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getMine") + @OperLog(operModule = "获取当前博主信息",operType = OperType.QUERY,operDesc = "获取当前博主信息") + public ResultBean getMine() { + String authorId = String.valueOf(request.getAttribute("authorId")); + Author author = authorService.getById(authorId); + if(author.getBirth()!=null){ + author.setAge(DateUtil.ageOfNow(author.getBirth())); + } + //关注我的列表 + List followMes = followService.selectFollowList(new FollowDto().setAuthorId(author.getAuthorId())); + List myFollows = followService.selectFollowList(new FollowDto().setFollowId(author.getAuthorId())); + author.setFollow(Long.valueOf(myFollows.size())); + author.setFans(Long.valueOf(followMes.size())); + Long upAndStarTotalCount = authorService.selectAuthorUpAndStarTotalCount(authorId); + author.setUpAndStarCount(upAndStarTotalCount); + return ResultBean.success(author); + } + + @ApiOperation(value = "获取作者信息") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getAuthor") + @OperLog(operModule = "获取作者信息",operType = OperType.QUERY,operDesc = "获取作者信息") + public ResultBean getAuthor(String authorId) { + String followId = String.valueOf(request.getAttribute("authorId")); + Author author = authorService.getById(authorId); + //我是否关注过 + Follow isFollow= followService.selectOneByFollowId(authorId, followId); + if(isFollow!=null){ + author.setIsFollow(Boolean.TRUE); + } + //是否关注过我 + Follow isFollowMe= followService.selectOneByFollowId(followId, authorId); + if(isFollowMe!=null){ + author.setIsFollowMe(Boolean.TRUE); + } + //关注我的列表 + List followMes = followService.selectFollowList(new FollowDto().setAuthorId(author.getAuthorId())); + List myFollows = followService.selectFollowList(new FollowDto().setFollowId(author.getAuthorId())); + + author.setFollow(Long.valueOf(myFollows.size())); + author.setFans(Long.valueOf(followMes.size())); + author.setFollowMes(followMes); + author.setMyFollows(myFollows); + Long upAndStarTotalCount = authorService.selectAuthorUpAndStarTotalCount(authorId); + author.setUpAndStarCount(upAndStarTotalCount); + return ResultBean.success(author); + } + + @ApiOperation(value = "修改头像") + @ApiOperationSupport(order = 1) + @PostMapping("/api/auth/updateAuthorAvatar") + @OperLog(operModule = "修改头像",operType = OperType.EDIT,operDesc = "修改头像") + public ResultBean updateAuthorAvatar(String fileId) { + String followId = String.valueOf(request.getAttribute("authorId")); + Author author = authorService.getById(followId); + author.setAuthorId(followId); + author.setAvatarId(fileId); + File file = fileService.selectFileByFileId(fileId); + String serverName = request.getServerName(); + author.setAvatarUrl("http://" + serverName + ":" + port + file.getFilePath()); + authorService.updateById(author); + return ResultBean.success(author); + }; + + @ApiOperation(value = "修改背景图") + @ApiOperationSupport(order = 1) + @PostMapping("/api/auth/updateAuthorBackGround") + @OperLog(operModule = "修改背景图",operType = OperType.EDIT,operDesc = "修改背景图") + public ResultBean updateAuthorBackGround(String fileId) { + String followId = String.valueOf(request.getAttribute("authorId")); + Author author = authorService.getById(followId); + author.setAuthorId(followId); + author.setBackGroundId(fileId); + File file = fileService.selectFileByFileId(fileId); + String serverName = request.getServerName(); + author.setBackGroundUrl("http://" + serverName + ":" + port + file.getFilePath()); + authorService.updateById(author); + return ResultBean.success(author); + }; + + @ApiOperation(value = "修改我的信息") + @ApiOperationSupport(order = 1) + @PostMapping("/api/auth/updateMine") + @OperLog(operModule = "修改我的信息",operType = OperType.EDIT,operDesc = "修改我的信息") + public ResultBean updateAuthorBackGround(@RequestBody Author author) { + authorService.updateById(author); + return ResultBean.success(author); + }; + + @ApiOperation(value = "获取关注目标作者的关注的所有博主") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getMyFollows") + @OperLog(operModule = "getMyFollows",operType = OperType.QUERY,operDesc = "获取关注目标作者的所有粉丝") + public ResultBean getMyFollows(String authorId) { + String myId = String.valueOf(request.getAttribute("authorId")); + //关注我的列表 + List myFollows = followService.selectMyFollowList(authorId,myId); + return ResultBean.success(myFollows); + } + + @ApiOperation(value = "获取关注目标作者的所有粉丝") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getFollowMes") + @OperLog(operModule = "获取关注目标作者的所有粉丝",operType = OperType.QUERY,operDesc = "获取 关注目标作者的所有粉丝") + public ResultBean getFollowMes(String authorId) { + String myId = String.valueOf(request.getAttribute("authorId")); + //关注我的列表 + List followMes = followService.selectFollowMeList(authorId,myId); + return ResultBean.success(followMes); + } + + @ApiOperation(value = "获取关注目标互相关注的人") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getMyFriends") + @OperLog(operModule = "getMyFriends",operType = OperType.QUERY,operDesc = "获取关注目标互相关注的人") + public ResultBean getMyFriends(String authorId) { + //关注我的列表 + List myFollows = followService.getMyFriends(authorId); + myFollows.stream().forEach(followVo -> { + followVo.setIsFollow(Boolean.TRUE); + followVo.setIsFollowMe(Boolean.TRUE); + }); + return ResultBean.success(myFollows); + } + + +} diff --git a/src/main/java/com/dd/admin/business/api/AuthChatApi.java b/src/main/java/com/dd/admin/business/api/AuthChatApi.java new file mode 100644 index 0000000..66668c8 --- /dev/null +++ b/src/main/java/com/dd/admin/business/api/AuthChatApi.java @@ -0,0 +1,143 @@ +package com.dd.admin.business.api; + +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.dd.admin.business.api.domain.UnReadCountBean; +import com.dd.admin.business.author.entity.Author; +import com.dd.admin.business.author.service.AuthorService; +import com.dd.admin.business.chat.domain.ChatDto; +import com.dd.admin.business.chat.domain.ChatVo; +import com.dd.admin.business.chat.service.ChatService; +import com.dd.admin.business.file.service.FileService; +import com.dd.admin.business.follow.service.FollowService; +import com.dd.admin.business.note.service.NoteService; +import com.dd.admin.business.noteImg.service.NoteImgService; +import com.dd.admin.business.reply.service.ReplyService; +import com.dd.admin.business.starNotes.service.StarNotesService; +import com.dd.admin.business.upNotes.service.UpNotesService; +import com.dd.admin.business.upReplys.service.UpReplysService; +import com.dd.admin.common.aop.operationLog.aop.OperLog; +import com.dd.admin.common.aop.operationLog.aop.OperType; +import com.dd.admin.common.model.result.ResultBean; +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; +@ApiModel("聊天相关Api") +@RestController +public class AuthChatApi { + @Autowired + HttpServletRequest request; + @Value("${server.port}") + String port; + @Autowired + ChatService chatService; + @Autowired + AuthorService authorService; + @Autowired + UpNotesService upNotesService; + @Autowired + StarNotesService starNotesService; + @Autowired + ReplyService replyService; + @Autowired + FollowService followService; + + @ApiOperation(value = "获取消息列表") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getMessageList") + @OperLog(operModule = "获取消息列表",operType = OperType.QUERY,operDesc = "获取消息列表") + public ResultBean> getMessageList(ChatDto chatDto) { + String followId = String.valueOf(request.getAttribute("authorId")); + List chatVos = chatService.selectChatList(followId); + return ResultBean.success(chatVos); + } + + @ApiOperation(value = "获取聊天记录") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getChatList") + @OperLog(operModule = "获取所有笔记",operType = OperType.QUERY,operDesc = "获取聊天记录") + public ResultBean> getChatList(ChatDto chatDto) { + String followId = String.valueOf(request.getAttribute("authorId")); + Author follow = authorService.getById(followId); + chatDto.setToId(followId); + chatDto.setToName(follow.getAuthorName()); + IPage chatVoIPage = chatService.selectChatPage(chatDto); + + return ResultBean.success(chatVoIPage); + } + + @ApiOperation(value = "读取消息") + @ApiOperationSupport(order = 1) + @PostMapping("/api/auth/readAuthorMessage") + @OperLog(operModule = "读取消息",operType = OperType.OTHER,operDesc = "读取消息") + public ResultBean readAuthorMessage(String authorId) { + String loginId = String.valueOf(request.getAttribute("authorId")); + chatService.readMessage(authorId,loginId); + return ResultBean.success(loginId); + }; + + @ApiOperation(value = "读取点赞笔记消息") + @ApiOperationSupport(order = 2) + @PostMapping("/api/auth/readUpNotesMessage") + @OperLog(operModule = "读取点赞笔记消息", operType = OperType.OTHER, operDesc = "读取点赞笔记消息") + public ResultBean readUpNotesMessage() { + String authorId = String.valueOf(request.getAttribute("authorId")); + upNotesService.readMessage(authorId); + return ResultBean.success(); + } + + @ApiOperation(value = "读取收藏笔记消息") + @ApiOperationSupport(order = 3) + @PostMapping("/api/auth/readStarNotesMessage") + @OperLog(operModule = "读取收藏笔记消息", operType = OperType.OTHER, operDesc = "读取收藏笔记消息") + public ResultBean readStarNotesMessage() { + String authorId = String.valueOf(request.getAttribute("authorId")); + starNotesService.readMessage(authorId); + return ResultBean.success(); + } + + @ApiOperation(value = "读取回复消息") + @ApiOperationSupport(order = 4) + @PostMapping("/api/auth/readReplyMessage") + @OperLog(operModule = "读取回复消息", operType = OperType.OTHER, operDesc = "读取回复消息") + public ResultBean readReplyMessage() { + String authorId = String.valueOf(request.getAttribute("authorId")); + replyService.readMessage(authorId); + return ResultBean.success(); + } + + @ApiOperation(value = "读取关注消息") + @ApiOperationSupport(order = 5) + @PostMapping("/api/auth/readFollowMessage") + @OperLog(operModule = "读取关注消息", operType = OperType.OTHER, operDesc = "读取关注消息") + public ResultBean readFollowMessage() { + String authorId = String.valueOf(request.getAttribute("authorId")); + followService.readMessage(authorId); + return ResultBean.success(); + } + + @ApiOperation(value = "查询我的未读消息数量") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getUnReadCount") + @OperLog(operModule = "查询我的未读消息数量",operType = OperType.OTHER,operDesc = "查询我的未读消息数量") + public ResultBean getUnReadCount() { + String loginId = String.valueOf(request.getAttribute("authorId")); + + Integer chatUnReadCount = chatService.selectUnReadCount(loginId); + Integer upNotesUnReadCount = upNotesService.selectUnReadCount(loginId); + Integer starNotesUnReadCount = starNotesService.selectUnReadCount(loginId); + Integer replyUnReadCount = replyService.selectUnReadCount(loginId); + Integer followUnReadCount = followService.selectUnReadCount(loginId); + Integer totalUnReadCount = 0; + UnReadCountBean unReadCountBean = new UnReadCountBean(chatUnReadCount,upNotesUnReadCount,starNotesUnReadCount,replyUnReadCount,followUnReadCount,totalUnReadCount); + unReadCountBean.calculateTotalUnReadCount(); + return ResultBean.success(unReadCountBean); + }; +} diff --git a/src/main/java/com/dd/admin/business/api/AuthNoteApi.java b/src/main/java/com/dd/admin/business/api/AuthNoteApi.java new file mode 100644 index 0000000..eaad42b --- /dev/null +++ b/src/main/java/com/dd/admin/business/api/AuthNoteApi.java @@ -0,0 +1,382 @@ +package com.dd.admin.business.api; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.date.DateUtil; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.core.metadata.OrderItem; +import com.dd.admin.business.author.entity.Author; +import com.dd.admin.business.author.service.AuthorService; +import com.dd.admin.business.chat.domain.ChatDto; +import com.dd.admin.business.chat.domain.ChatVo; +import com.dd.admin.business.chat.entity.Chat; +import com.dd.admin.business.chat.service.ChatService; +import com.dd.admin.business.file.entity.File; +import com.dd.admin.business.file.service.FileService; +import com.dd.admin.business.follow.domain.FollowDto; +import com.dd.admin.business.follow.domain.FollowVo; +import com.dd.admin.business.follow.entity.Follow; +import com.dd.admin.business.follow.service.FollowService; +import com.dd.admin.business.note.domain.NoteDto; +import com.dd.admin.business.note.domain.NoteVo; +import com.dd.admin.business.note.domain.ReplayMeVo; +import com.dd.admin.business.note.domain.UpMeVo; +import com.dd.admin.business.note.entity.Note; +import com.dd.admin.business.note.service.NoteService; +import com.dd.admin.business.noteImg.entity.NoteImg; +import com.dd.admin.business.noteImg.service.NoteImgService; +import com.dd.admin.business.reply.domain.ReplyDto; +import com.dd.admin.business.reply.domain.ReplyVo; +import com.dd.admin.business.reply.entity.Reply; +import com.dd.admin.business.reply.service.ReplyService; +import com.dd.admin.business.starNotes.domain.StarNotesDto; +import com.dd.admin.business.starNotes.domain.StarNotesVo; +import com.dd.admin.business.starNotes.entity.StarNotes; +import com.dd.admin.business.starNotes.service.StarNotesService; +import com.dd.admin.business.upNotes.domain.UpNotesDto; +import com.dd.admin.business.upNotes.domain.UpNotesVo; +import com.dd.admin.business.upNotes.entity.UpNotes; +import com.dd.admin.business.upNotes.service.UpNotesService; +import com.dd.admin.business.upReplys.entity.UpReplys; +import com.dd.admin.business.upReplys.service.UpReplysService; +import com.dd.admin.common.aop.operationLog.aop.OperLog; +import com.dd.admin.common.aop.operationLog.aop.OperType; +import com.dd.admin.common.exception.ApiException; +import com.dd.admin.common.model.result.ResultBean; +import com.dd.admin.common.utils.AddressUtils; +import com.dd.admin.common.utils.CommonUtil; +import com.dd.admin.common.utils.IPUtils; +import com.dd.admin.common.utils.StringUtil; +import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; +import javax.validation.constraints.NotBlank; +import java.util.*; +import java.util.stream.Collectors; + +import static com.dd.admin.common.consts.XhsConst.TRUE; +@ApiModel("笔记相关Api") +@RestController +public class AuthNoteApi { + @Autowired + AuthorService authorService; + @Autowired + HttpServletRequest request; + @Autowired + NoteService noteService; + @Autowired + FileService fileService; + @Autowired + NoteImgService noteImgService; + @Value("${server.port}") + String port; + @Autowired + FollowService followService; + @Autowired + ReplyService replyService; + + @ApiOperation(value = "获取所有关注笔记") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getFollowNotes") + @OperLog(operModule = "获取所有关注笔记",operType = OperType.QUERY,operDesc = "获取所有关注笔记") + public ResultBean> getFollowNotes() { + String followId = String.valueOf(request.getAttribute("authorId")); + NoteDto noteDto = new NoteDto(); + noteDto.setFollowId(followId); + List follows = followService.selectFollowListByFollowId(followId); + + IPage pageInfo = new IPage() { + @Override + public List orders() { + return null; + } + + @Override + public List getRecords() { + return null; + } + + @Override + public IPage setRecords(List records) { + return null; + } + + @Override + public long getTotal() { + return 0; + } + + @Override + public IPage setTotal(long total) { + return null; + } + + @Override + public long getSize() { + return 0; + } + + @Override + public IPage setSize(long size) { + return null; + } + + @Override + public long getCurrent() { + return 0; + } + + @Override + public IPage setCurrent(long current) { + return null; + } + }; + if(CollectionUtil.isNotEmpty(follows)){ + String authorIds = CommonUtil.getIds(follows, "authorId"); + noteDto.setAuthorIds(authorIds); + pageInfo = noteService.selectNotePage(noteDto); + } + return ResultBean.success(pageInfo); + } + + @ApiOperation(value = "获取所有笔记") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/notes") + @OperLog(operModule = "获取所有笔记",operType = OperType.QUERY,operDesc = "获取所有笔记") + public ResultBean> page(NoteDto noteDto) { + String followId = String.valueOf(request.getAttribute("authorId")); + noteDto.setFollowId(followId); + IPage pageInfo = noteService.selectNotePage(noteDto); + return ResultBean.success(pageInfo); + } + + + //外层倒叙 回复正序 + @ApiOperation(value = "获取笔记评论") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getNoteReply") + @OperLog(operModule = "获取笔记评论",operType = OperType.QUERY,operDesc = "获取笔记评论") + public ResultBean getNoteReply( ReplyDto replyDto) { + String fellowId = String.valueOf(request.getAttribute("authorId")); + + + String noteId = replyDto.getNoteId(); + List replyList = replyService.selectReplyList(new ReplyDto().setNoteId(noteId).setFollowId(fellowId)); + Integer totalCount = replyList.size(); + // 先构建一个以回复ID为键,回复对象为值的Map,方便后续快速查找回复对象 + // 构建以replyId为键,ReplyVo对象为值的Map,方便后续快速查找回复对象 + Map replyMap = replyList.stream() + .collect(Collectors.toMap(ReplyVo::getReplyId, reply -> reply)); + + List secondLevelReplies = replyList + .stream() + .filter(reply -> + (StringUtil.isNotEmpty(reply.getParentId())||StringUtil.isNotEmpty(reply.getTopParentId())) + ).sorted(Comparator.comparing(ReplyVo::getCreateTime)).collect(Collectors.toList()); + secondLevelReplies.forEach(replyVo -> { + if(StringUtil.isNotEmpty(replyVo.getParentId())){ + replyVo.setParentAuthorId(replyMap.get(replyVo.getParentId()).getAuthorId()); + replyVo.setParentAuthorName(replyMap.get(replyVo.getParentId()).getAuthorName()); + } + }); + + // 获取第一层回复(topId为空且parentId也为空) + List firstLevelReplies = replyList.stream() + .filter(reply -> reply.getTopParentId() == null && reply.getParentId() == null) + .collect(Collectors.toList()); + + // 将第二层回复添加到对应的第一层回复下(通过topId与replyId关联) + firstLevelReplies.forEach(firstLevelReply -> { + String firstLevelReplyId = firstLevelReply.getReplyId(); + List relatedSecondLevelReplies = secondLevelReplies.stream() + .filter(secondLevelReply -> firstLevelReplyId.equals(secondLevelReply.getTopParentId())) + .collect(Collectors.toList()); + if (firstLevelReply.getReply() == null) { + firstLevelReply.setReply(new ArrayList<>()); + } + firstLevelReply.getReply().addAll(relatedSecondLevelReplies); + }); + + + Map map = new HashMap(); + map.put("totalCount",totalCount); + map.put("replys",firstLevelReplies); + + + return ResultBean.success(map); + } + + + @ApiOperation(value = "获取目标博主笔记") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getAuthorNotes") + @OperLog(operModule = "获取目标博主笔记",operType = OperType.QUERY,operDesc = "获取目标博主笔记") + public ResultBean> getAuthorNotes(String authorId) { + String followId = String.valueOf(request.getAttribute("authorId")); + NoteDto noteDto = new NoteDto(); + noteDto.setFollowId(followId); + noteDto.setAuthorId(authorId); + IPage pageInfo = noteService.selectNotePage(noteDto); + return ResultBean.success(pageInfo); + } + + @ApiOperation(value = "获取当前博主笔记") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getMineNotes") + @OperLog(operModule = "获取当前博主笔记",operType = OperType.QUERY,operDesc = "获取当前博主笔记") + public ResultBean> getMineNotes() { + String authorId = String.valueOf(request.getAttribute("authorId")); + NoteDto noteDto = new NoteDto(); + noteDto.setFollowId(authorId); + noteDto.setAuthorId(authorId); + IPage pageInfo = noteService.selectNotePage(noteDto); + return ResultBean.success(pageInfo); + } + + + @ApiOperation(value = "获取所有点赞笔记") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getUpNotes") + @OperLog(operModule = "获取所有点赞笔记",operType = OperType.QUERY,operDesc = "获取所有点赞笔记") + public ResultBean> getUpNotes() { + String followId = String.valueOf(request.getAttribute("authorId")); + NoteDto noteDto = new NoteDto(); + noteDto.setFollowId(followId); + noteDto.setMyUpById(followId); + IPage pageInfo = noteService.selectNotePage(noteDto); + return ResultBean.success(pageInfo); + } + + + @ApiOperation(value = "获取所有收藏笔记") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getStarNotes") + @OperLog(operModule = "获取所有收藏笔记",operType = OperType.QUERY,operDesc = "获取所有收藏笔记") + public ResultBean> getStarNotes() { + String followId = String.valueOf(request.getAttribute("authorId")); + NoteDto noteDto = new NoteDto(); + noteDto.setFollowId(followId); + noteDto.setMyStarById(followId); + IPage pageInfo = noteService.selectNotePage(noteDto); + return ResultBean.success(pageInfo); + } + + + @ApiOperation(value = "创建笔记") + @ApiOperationSupport(order = 1) + @PostMapping("/api/auth/addNote") + @OperLog(operModule = "创建笔记",operType = OperType.ADD,operDesc = "创建笔记") + public ResultBean addNote(@RequestBody NoteDto noteDto) { + String authorId = String.valueOf(request.getAttribute("authorId")); + Author author = authorService.getById(authorId); + + Note note = BeanUtil.copyProperties(noteDto, Note.class); + note.setAuthorId(author.getAuthorId()); + note.setAuthorName(author.getAuthorName()); + note.setAuthorAvatar(author.getAvatarUrl()); + note.setIpAddress(IPUtils.getIpAddr(request)); // 请求IP + note.setIpRealAddress(AddressUtils.getRealAddress(note.getIpAddress())); + noteService.save(note); + + List imgs = noteDto.getImgs(); + List noteImgList = new ArrayList<>(); + for(int i=0;i imgs = noteDto.getImgs(); + List noteImgList = new ArrayList<>(); + for(int i=0;i> getUpMeNotes() { + String authorId = String.valueOf(request.getAttribute("authorId")); + IPage upMeVoIPage = noteService.selectUpMeNotes(authorId); + return ResultBean.success(upMeVoIPage); + } + + @ApiOperation(value = "获取所有评论我的笔记记录") + @ApiOperationSupport(order = 1) + @GetMapping("/api/auth/getReplayMes") + @OperLog(operModule = "获取所有评论我的笔记记录",operType = OperType.QUERY,operDesc = "获取所有评论我的笔记记录") + public ResultBean> getReplayMes() { + String authorId = String.valueOf(request.getAttribute("authorId")); + IPage replyMeNotes = noteService.selectReplyMeNotes(authorId); + return ResultBean.success(replyMeNotes); + } +} diff --git a/src/main/java/com/dd/admin/business/api/domain/UnReadCountBean.java b/src/main/java/com/dd/admin/business/api/domain/UnReadCountBean.java new file mode 100644 index 0000000..07cc907 --- /dev/null +++ b/src/main/java/com/dd/admin/business/api/domain/UnReadCountBean.java @@ -0,0 +1,28 @@ +package com.dd.admin.business.api.domain; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class UnReadCountBean { + private Integer chatUnReadCount; + private Integer upNotesUnReadCount; + private Integer starNotesUnReadCount; + private Integer replyUnReadCount; + private Integer followUnReadCount; + private Integer totalUnReadCount; + + // 自定义方法,用于计算总数量(也可以在构造函数等其他地方调用此方法来计算总和) + public void calculateTotalUnReadCount() { + int chatCount = chatUnReadCount!= null? chatUnReadCount : 0; + int upNotesCount = upNotesUnReadCount!= null? upNotesUnReadCount : 0; + int starNotesCount = starNotesUnReadCount!= null? starNotesUnReadCount : 0; + int replyCount = replyUnReadCount!= null? replyUnReadCount : 0; + int followCount = followUnReadCount!= null? followUnReadCount : 0; + + totalUnReadCount = chatCount + upNotesCount + starNotesCount + replyCount + followCount; + } +} diff --git a/src/main/java/com/dd/admin/business/chat/service/ChatService.java b/src/main/java/com/dd/admin/business/chat/service/ChatService.java index 0fa37f7..c345d5c 100644 --- a/src/main/java/com/dd/admin/business/chat/service/ChatService.java +++ b/src/main/java/com/dd/admin/business/chat/service/ChatService.java @@ -27,5 +27,6 @@ public interface ChatService extends IService { List selectChatList(String authorId); void readMessage(String authorId,String loginId); + //未读聊天消息的数量 Integer selectUnReadCount(String authorId); } diff --git a/src/main/java/com/dd/admin/business/follow/service/FollowService.java b/src/main/java/com/dd/admin/business/follow/service/FollowService.java index 3cc1797..889e60e 100644 --- a/src/main/java/com/dd/admin/business/follow/service/FollowService.java +++ b/src/main/java/com/dd/admin/business/follow/service/FollowService.java @@ -33,6 +33,9 @@ public interface FollowService extends IService { List selectMyFollowList( String authorId,String targetId); List selectFollowMeList(String authorId,String targetId); List getMyFriends(String authorId); + //未读聊天消息的数量 + Integer selectUnReadCount(String authorId); + void readMessage(String authorId); } diff --git a/src/main/java/com/dd/admin/business/follow/service/impl/FollowServiceImpl.java b/src/main/java/com/dd/admin/business/follow/service/impl/FollowServiceImpl.java index 6abcd96..37021d1 100644 --- a/src/main/java/com/dd/admin/business/follow/service/impl/FollowServiceImpl.java +++ b/src/main/java/com/dd/admin/business/follow/service/impl/FollowServiceImpl.java @@ -1,8 +1,10 @@ package com.dd.admin.business.follow.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.dd.admin.business.starNotes.entity.StarNotes; import com.dd.admin.business.upNotes.entity.UpNotes; import com.dd.admin.common.model.PageFactory; import com.dd.admin.business.follow.entity.Follow; @@ -66,4 +68,19 @@ public class FollowServiceImpl extends ServiceImpl impleme return baseMapper.getMyFriends(authorId); } + @Override + public Integer selectUnReadCount(String authorId) { + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); + queryWrapper.eq(Follow::getAuthorId,authorId); + queryWrapper.ne(Follow::getFollowId,authorId); + queryWrapper.eq(Follow::getMessageStatus,0); + return baseMapper.selectCount(queryWrapper); + } + @Override + public void readMessage(String authorId) { + LambdaUpdateWrapper queryWrapper = new LambdaUpdateWrapper(); + queryWrapper.eq(Follow::getAuthorId,authorId); + queryWrapper.set(Follow::getMessageStatus,1); + this.update(queryWrapper); + } } diff --git a/src/main/java/com/dd/admin/business/reply/service/ReplyService.java b/src/main/java/com/dd/admin/business/reply/service/ReplyService.java index 3e3677b..b487276 100644 --- a/src/main/java/com/dd/admin/business/reply/service/ReplyService.java +++ b/src/main/java/com/dd/admin/business/reply/service/ReplyService.java @@ -23,4 +23,8 @@ public interface ReplyService extends IService { //回复表-列表 List selectReplyList(ReplyDto replyDto); + //未读聊天消息的数量 + Integer selectUnReadCount(String authorId); + void readMessage(String authorId); + } diff --git a/src/main/java/com/dd/admin/business/reply/service/impl/ReplyServiceImpl.java b/src/main/java/com/dd/admin/business/reply/service/impl/ReplyServiceImpl.java index 973a7c9..c19e7ac 100644 --- a/src/main/java/com/dd/admin/business/reply/service/impl/ReplyServiceImpl.java +++ b/src/main/java/com/dd/admin/business/reply/service/impl/ReplyServiceImpl.java @@ -1,7 +1,11 @@ package com.dd.admin.business.reply.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.dd.admin.business.starNotes.entity.StarNotes; +import com.dd.admin.business.upNotes.entity.UpNotes; import com.dd.admin.common.model.PageFactory; import com.dd.admin.business.reply.entity.Reply; import com.dd.admin.business.reply.mapper.ReplyMapper; @@ -33,4 +37,23 @@ public class ReplyServiceImpl extends ServiceImpl implements public List selectReplyList(ReplyDto replyDto) { return baseMapper.selectReplyList(replyDto); } + + @Override + public Integer selectUnReadCount(String authorId) { + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); + queryWrapper.eq(Reply::getParentAuthorId,authorId); + queryWrapper.ne(Reply::getAuthorId,authorId); + queryWrapper.eq(Reply::getMessageStatus,0); + return baseMapper.selectCount(queryWrapper); + } + + @Override + public void readMessage(String authorId) { + LambdaUpdateWrapper queryWrapper = new LambdaUpdateWrapper(); + queryWrapper.eq(Reply::getParentAuthorId,authorId); + queryWrapper.ne(Reply::getAuthorId,authorId); + queryWrapper.set(Reply::getMessageStatus,1); + this.update(queryWrapper); + } + } diff --git a/src/main/java/com/dd/admin/business/starNotes/service/StarNotesService.java b/src/main/java/com/dd/admin/business/starNotes/service/StarNotesService.java index 1e34ac7..c8f9811 100644 --- a/src/main/java/com/dd/admin/business/starNotes/service/StarNotesService.java +++ b/src/main/java/com/dd/admin/business/starNotes/service/StarNotesService.java @@ -29,5 +29,8 @@ public interface StarNotesService extends IService { StarNotes selectOneByFollowId(String noteId, String followId); + //未读聊天消息的数量 + Integer selectUnReadCount(String authorId); + void readMessage(String authorId); } diff --git a/src/main/java/com/dd/admin/business/starNotes/service/impl/StarNotesServiceImpl.java b/src/main/java/com/dd/admin/business/starNotes/service/impl/StarNotesServiceImpl.java index e455cc7..869c762 100644 --- a/src/main/java/com/dd/admin/business/starNotes/service/impl/StarNotesServiceImpl.java +++ b/src/main/java/com/dd/admin/business/starNotes/service/impl/StarNotesServiceImpl.java @@ -1,6 +1,7 @@ package com.dd.admin.business.starNotes.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.dd.admin.business.upNotes.entity.UpNotes; @@ -44,4 +45,21 @@ public class StarNotesServiceImpl extends ServiceImpl queryWrapper = new LambdaQueryWrapper(); + queryWrapper.eq(StarNotes::getAuthorId,authorId); + queryWrapper.eq(StarNotes::getMessageStatus,0); + return baseMapper.selectCount(queryWrapper); + } + + @Override + public void readMessage(String authorId) { + LambdaUpdateWrapper queryWrapper = new LambdaUpdateWrapper(); + queryWrapper.eq(StarNotes::getAuthorId,authorId); + queryWrapper.ne(StarNotes::getFollowId,authorId); + queryWrapper.set(StarNotes::getMessageStatus,1); + this.update(queryWrapper); + } + } diff --git a/src/main/java/com/dd/admin/business/upNotes/service/UpNotesService.java b/src/main/java/com/dd/admin/business/upNotes/service/UpNotesService.java index 7a05b52..3f9c7b3 100644 --- a/src/main/java/com/dd/admin/business/upNotes/service/UpNotesService.java +++ b/src/main/java/com/dd/admin/business/upNotes/service/UpNotesService.java @@ -27,4 +27,8 @@ public interface UpNotesService extends IService { UpNotes selectOneByFollowId(String noteId ,String followId); + //未读聊天消息的数量 + Integer selectUnReadCount(String authorId); + void readMessage(String authorId); + } diff --git a/src/main/java/com/dd/admin/business/upNotes/service/impl/UpNotesServiceImpl.java b/src/main/java/com/dd/admin/business/upNotes/service/impl/UpNotesServiceImpl.java index 4b89bcd..882b6fb 100644 --- a/src/main/java/com/dd/admin/business/upNotes/service/impl/UpNotesServiceImpl.java +++ b/src/main/java/com/dd/admin/business/upNotes/service/impl/UpNotesServiceImpl.java @@ -1,8 +1,10 @@ package com.dd.admin.business.upNotes.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.dd.admin.business.chat.entity.Chat; import com.dd.admin.common.model.PageFactory; import com.dd.admin.business.upNotes.entity.UpNotes; import com.dd.admin.business.upNotes.mapper.UpNotesMapper; @@ -47,4 +49,21 @@ public class UpNotesServiceImpl extends ServiceImpl impl queryWrapper.eq(UpNotes::getFollowId,followId); return this.getOne(queryWrapper); } + + @Override + public Integer selectUnReadCount(String authorId) { + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper(); + queryWrapper.eq(UpNotes::getAuthorId,authorId); + queryWrapper.eq(UpNotes::getMessageStatus,0); + return baseMapper.selectCount(queryWrapper); + } + + @Override + public void readMessage(String authorId) { + LambdaUpdateWrapper queryWrapper = new LambdaUpdateWrapper(); + queryWrapper.eq(UpNotes::getAuthorId,authorId); + queryWrapper.ne(UpNotes::getFollowId,authorId); + queryWrapper.set(UpNotes::getMessageStatus,1); + this.update(queryWrapper); + } } diff --git a/src/main/java/com/dd/admin/business/webSocket/MyWebSocketMsgHandler.java b/src/main/java/com/dd/admin/business/webSocket/MyWebSocketMsgHandler.java index 65d673a..2f7bc51 100644 --- a/src/main/java/com/dd/admin/business/webSocket/MyWebSocketMsgHandler.java +++ b/src/main/java/com/dd/admin/business/webSocket/MyWebSocketMsgHandler.java @@ -1 +1 @@ -package com.dd.admin.business.webSocket; import com.alibaba.fastjson.JSON; import com.dd.admin.common.utils.AddressUtils; import com.dd.admin.common.utils.IPUtils; import com.dd.admin.common.utils.StringUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.tio.core.ChannelContext; import org.tio.core.Tio; import org.tio.http.common.HttpRequest; import org.tio.http.common.HttpResponse; import org.tio.utils.lock.SetWithLock; import org.tio.websocket.common.WsRequest; import org.tio.websocket.server.handler.IWsMsgHandler; import org.tio.websocket.starter.TioWebSocketServerBootstrap; import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; import java.util.Set; @Component public class MyWebSocketMsgHandler implements IWsMsgHandler { MyWebSocketMsgHandler handler; private static Logger log = LoggerFactory.getLogger(MyWebSocketMsgHandler.class); @PostConstruct public void init() { handler = this; } @Autowired public TioWebSocketServerBootstrap bootstrap; @Autowired Map handlerInterfaceMap; @Override public HttpResponse handshake(HttpRequest request, HttpResponse httpResponse, ChannelContext channelContext) throws Exception { String authorId = request.getParam("authorId"); String authorName = request.getParam("authorName"); Tio.bindUser(channelContext,authorId); String ipAddr = request.getClientIp(); String realAddress = AddressUtils.getRealAddress(ipAddr); System.out.println(authorId+":进入了Tio id:"+authorId+" ip:"+ ipAddr); SetWithLock channelContexts = Tio.getAllChannelContexts(bootstrap.getServerGroupContext()); Set contextList = channelContexts.getObj(); System.out.println("当前在线用户:"); for(ChannelContext context:contextList){ System.out.println(context.userid+"\t"); } Integer count = channelContexts.size(); System.out.println(count); return httpResponse; } @Override public void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception { // System.out.println("握手成功进入群组"); } @Override public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception { System.out.println("接收到bytes消息"); return null; } @Override public Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception { return null; } @Override public Object onText(WsRequest wsRequest, String text, ChannelContext channelContext) throws Exception { if(text.equals("ping")) return null; System.out.println("接收到文本消息:"+text); Map map = JSON.parseObject(text,Map.class); String handlerType =(String)map.get("handlerType"); if(!StringUtil.isEmpty(handlerType)){ MsgHandlerInterface msgHandler = (MsgHandlerInterface) handlerInterfaceMap.get(handlerType); if(msgHandler!=null){ msgHandler.handler(map,channelContext); }else{ log.debug("非法请求..."); } }else{ log.debug("非法请求..."); } System.out.println(map); return null; } } \ No newline at end of file +package com.dd.admin.business.webSocket; import com.alibaba.fastjson.JSON; import com.dd.admin.common.utils.AddressUtils; import com.dd.admin.common.utils.IPUtils; import com.dd.admin.common.utils.StringUtil; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import org.tio.core.ChannelContext; import org.tio.core.Tio; import org.tio.http.common.HttpRequest; import org.tio.http.common.HttpResponse; import org.tio.utils.lock.SetWithLock; import org.tio.websocket.common.WsRequest; import org.tio.websocket.server.handler.IWsMsgHandler; import org.tio.websocket.starter.TioWebSocketServerBootstrap; import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; import java.util.Set; @Configuration @Slf4j public class MyWebSocketMsgHandler implements IWsMsgHandler { @Autowired Map handlerInterfaceMap; @Override public HttpResponse handshake(HttpRequest request, HttpResponse httpResponse, ChannelContext channelContext) throws Exception { String authorId = request.getParam("authorId"); String authorName = request.getParam("authorName"); Tio.bindUser(channelContext,authorId); String ipAddr = request.getClientIp(); String realAddress = AddressUtils.getRealAddress(ipAddr); System.out.println(authorId+":进入了Tio id:"+authorId+" ip:"+ ipAddr); SetWithLock channelContexts = Tio.getAllChannelContexts(channelContext.getGroupContext()); Set contextList = channelContexts.getObj(); System.out.println("当前在线用户:"); for(ChannelContext context:contextList){ System.out.println(context.userid+"\t"); } Integer count = channelContexts.size(); System.out.println(count); return httpResponse; } @Override public void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception { // System.out.println("握手成功进入群组"); } @Override public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception { System.out.println("接收到bytes消息"); return null; } @Override public Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception { return null; } @Override public Object onText(WsRequest wsRequest, String text, ChannelContext channelContext) throws Exception { if(text.equals("ping")) return null; System.out.println("接收到文本消息:"+text); Map map = JSON.parseObject(text,Map.class); String handlerType =(String)map.get("handlerType"); if(!StringUtil.isEmpty(handlerType)){ MsgHandlerInterface msgHandler = (MsgHandlerInterface) handlerInterfaceMap.get(handlerType); if(msgHandler!=null){ msgHandler.handler(map,channelContext); }else{ log.debug("非法请求..."); } }else{ log.debug("非法请求..."); } System.out.println(map); return null; } } \ No newline at end of file diff --git a/src/main/java/com/dd/admin/business/webSocket/SocketMsg.java b/src/main/java/com/dd/admin/business/webSocket/SocketMsg.java new file mode 100644 index 0000000..19e5c84 --- /dev/null +++ b/src/main/java/com/dd/admin/business/webSocket/SocketMsg.java @@ -0,0 +1 @@ +package com.dd.admin.business.webSocket; import lombok.Data; import lombok.experimental.Accessors; @Data @Accessors(chain = true) public class SocketMsg { //握手类型 private String handlerType; private Object body; public SocketMsg(String handlerType, Object body) { super(); this.handlerType = handlerType; this.body = body; } } \ No newline at end of file diff --git a/src/main/java/com/dd/admin/business/webSocket/handler/P2PMessageHandler.java b/src/main/java/com/dd/admin/business/webSocket/handler/P2PMessageHandler.java index fa4b12e..9f95b48 100644 --- a/src/main/java/com/dd/admin/business/webSocket/handler/P2PMessageHandler.java +++ b/src/main/java/com/dd/admin/business/webSocket/handler/P2PMessageHandler.java @@ -1 +1 @@ -package com.dd.admin.business.webSocket.handler; import cn.hutool.core.bean.BeanUtil; import com.dd.admin.business.chat.domain.ChatVo; import com.dd.admin.business.chat.entity.Chat; import com.dd.admin.business.chat.service.ChatService; import com.dd.admin.business.webSocket.MsgHandlerInterface; import com.dd.admin.business.webSocket.util.TioUtil; import com.dd.admin.common.utils.AddressUtils; import com.dd.admin.common.utils.HttpContext; import com.dd.admin.common.utils.IPUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.tio.core.ChannelContext; import org.tio.core.Tio; import org.tio.http.common.HttpRequest; import org.tio.utils.lock.SetWithLock; import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Date; import java.util.Map; import java.util.Set; @Component @Slf4j @Service("5") public class P2PMessageHandler implements MsgHandlerInterface { public static P2PMessageHandler handler; @Autowired ChatService chatService; @Override public Object handler(Map map, ChannelContext context ){ Chat chat = BeanUtil.toBean(map, Chat.class); chat.setIpAddress(context.getClientNode().getIp()); chat.setIpRealAddress(AddressUtils.getRealAddress(chat.getIpAddress())); //ip真实地址 chatService.save(chat); ChatVo chatVo = chatService.selectChat(chat.getChatId()); //t-io支持多点登录,获取的是一个集合,因为此账号可能存在多个连接哦 SetWithLock contexts = Tio.getChannelContextsByUserid(context.getGroupContext(), chat.getToId()); //用户在线 if(contexts!=null && contexts.size() > 0) { Set contextList = contexts.getObj(); //t-io支持多点登录,获取的是一个集合,向集合发送聊天信息 for (ChannelContext con : contextList) { TioUtil.sendMessage(con, "5", chatVo); } } //也要给我自己发用于数据回显 //t-io支持多点登录,获取的是一个集合,因为此账号可能存在多个连接哦 SetWithLock contexts1 = Tio.getChannelContextsByUserid(context.getGroupContext(), chat.getFromId()); //用户在线 if(contexts1!=null && contexts1.size() > 0) { Set contextList = contexts1.getObj(); //t-io支持多点登录,获取的是一个集合,向集合发送聊天信息 for (ChannelContext con : contextList) { TioUtil.sendMessage(con, "5", chatVo); } } return null; }} \ No newline at end of file +package com.dd.admin.business.webSocket.handler; import cn.hutool.core.bean.BeanUtil; import com.dd.admin.business.chat.domain.ChatVo; import com.dd.admin.business.chat.entity.Chat; import com.dd.admin.business.chat.service.ChatService; import com.dd.admin.business.webSocket.MsgHandlerInterface; import com.dd.admin.business.webSocket.util.TioUtil; import com.dd.admin.common.utils.AddressUtils; import com.dd.admin.common.utils.HttpContext; import com.dd.admin.common.utils.IPUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import org.tio.core.ChannelContext; import org.tio.core.Tio; import org.tio.http.common.HttpRequest; import org.tio.utils.lock.SetWithLock; import javax.annotation.PostConstruct; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Date; import java.util.Map; import java.util.Set; @Component @Slf4j @Service("5") public class P2PMessageHandler implements MsgHandlerInterface { public static P2PMessageHandler handler; @Autowired ChatService chatService; @Override public Object handler(Map map, ChannelContext context ){ Chat chat = BeanUtil.toBean(map, Chat.class); chat.setIpAddress(context.getClientNode().getIp()); chat.setIpRealAddress(AddressUtils.getRealAddress(chat.getIpAddress())); //ip真实地址 chatService.save(chat); ChatVo chatVo = chatService.selectChat(chat.getChatId()); TioUtil.sendChatMessageToUser(context.getGroupContext(),chat.getToId(),"5",chatVo); TioUtil.sendChatMessageToUser(context.getGroupContext(),chat.getFromId(),"5",chatVo); return null; }} \ No newline at end of file diff --git a/src/main/java/com/dd/admin/business/webSocket/util/TioUtil.java b/src/main/java/com/dd/admin/business/webSocket/util/TioUtil.java index f70469b..d3d9644 100644 --- a/src/main/java/com/dd/admin/business/webSocket/util/TioUtil.java +++ b/src/main/java/com/dd/admin/business/webSocket/util/TioUtil.java @@ -1 +1 @@ -package com.dd.admin.business.webSocket.util; import com.alibaba.fastjson.JSON; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import org.tio.core.ChannelContext; import org.tio.core.Tio; import org.tio.websocket.common.WsResponse; public class TioUtil { /** * 快速生成WsResponse * @param action * @param body * @return */ public static WsResponse madeWsResponse(String handlerType, Object body){ // String responseMsg = JSON.toJSONString(body); // System.out.println(responseMsg); // 创建ObjectMapper对象 ObjectMapper objectMapper = new ObjectMapper(); // 开启美化输出(格式化)功能 objectMapper.enable(SerializationFeature.INDENT_OUTPUT); // 将List对象转换为JSON字符串 try { String responseMsg = objectMapper.writeValueAsString(body); System.out.println(responseMsg); return WsResponse.fromText(responseMsg,"utf-8"); } catch (JsonProcessingException e) { e.printStackTrace(); } return null; } public static boolean sendMessage(ChannelContext con, String handlerType, Object body){ WsResponse weResponse = madeWsResponse(handlerType,body); return Tio.send(con,weResponse); } } \ No newline at end of file +package com.dd.admin.business.webSocket.util; import com.alibaba.fastjson.JSON; import com.dd.admin.business.chat.domain.ChatVo; import com.dd.admin.business.webSocket.SocketMsg; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import org.tio.core.ChannelContext; import org.tio.core.GroupContext; import org.tio.core.Tio; import org.tio.utils.lock.SetWithLock; import org.tio.websocket.common.WsResponse; import java.util.Set; public class TioUtil { public static WsResponse madeWsResponse(String handlerType, Object body){ SocketMsg msg = new SocketMsg(handlerType,body); String responseMsg = JSON.toJSONString(msg); return WsResponse.fromText(responseMsg,"utf-8"); } public static boolean sendMessage(ChannelContext con, String handlerType, Object body){ WsResponse weResponse = madeWsResponse(handlerType,body); return Tio.send(con,weResponse); } // 封装发送聊天信息给指定用户的方法(根据给定的用户ID发送消息) public static void sendChatMessageToUser(GroupContext groupContext, String tioId,String handlerType, Object object) { // t-io支持多点登录,获取的是一个集合,因为此账号可能存在多个连接哦 SetWithLock contexts1 = Tio.getChannelContextsByUserid(groupContext, tioId); // 用户在线 if (contexts1!= null && contexts1.size() > 0) { Set contextList = contexts1.getObj(); // t-io支持多点登录,获取的是一个集合,向集合发送聊天信息 for (ChannelContext con : contextList) { TioUtil.sendMessage(con, handlerType, object); } } } } \ No newline at end of file diff --git a/src/main/java/com/dd/admin/common/utils/SpringContextUtils.java b/src/main/java/com/dd/admin/common/utils/SpringContextUtils.java new file mode 100644 index 0000000..1c1d3e3 --- /dev/null +++ b/src/main/java/com/dd/admin/common/utils/SpringContextUtils.java @@ -0,0 +1,52 @@ +package com.dd.admin.common.utils; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +@Component("springContextUtils") +public class SpringContextUtils implements ApplicationContextAware { + /** + * Spring应用上下文环境 + */ + private static ApplicationContext applicationContext; + + /** + * 实现ApplicationContextAware接口的回调方法,设置上下文环境 + * + * @param applicationContext + */ + @Override + public void setApplicationContext(ApplicationContext applicationContext) { + applicationContext = applicationContext; + } + + /** + * @return ApplicationContext + */ + public static ApplicationContext getApplicationContext() { + return applicationContext; + } + + /** + * 获取对象 + * + * @param name + * @return Object + * @throws BeansException + */ + public static Object getBean(String name) throws BeansException { + return applicationContext.getBean(name); + } + + /** + * 根据class获取对应的bean对象 + * @param clz + * @return + */ + public static Object getBean(Class clz){ + return applicationContext.getBean(clz); + } + +}