From 90c7ef02cc7d73d3d37b85b76df6893c36bebbb0 Mon Sep 17 00:00:00 2001 From: linfeng <2445465217@qq.com> Date: Wed, 27 Jul 2022 14:51:29 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=AB=AF=E9=A6=96=E9=A1=B5?= =?UTF-8?q?=E5=92=8C=E7=94=A8=E6=88=B7=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/admin/service/AppUserService.java | 4 + .../modules/admin/service/PostService.java | 8 +- .../service/impl/AppUserServiceImpl.java | 28 +++++ .../admin/service/impl/PostServiceImpl.java | 101 ++++++++++++------ .../app/controller/AppLoginController.java | 20 ++++ .../app/controller/AppPostController.java | 56 ++++++++++ .../modules/app/service/FollowService.java | 4 + .../app/service/impl/FollowServiceImpl.java | 26 +++++ .../components/post-list/post-list.vue | 32 +----- .../pages/index/index.vue | 2 +- 10 files changed, 210 insertions(+), 71 deletions(-) create mode 100644 src/main/java/io/linfeng/modules/app/controller/AppPostController.java diff --git a/src/main/java/io/linfeng/modules/admin/service/AppUserService.java b/src/main/java/io/linfeng/modules/admin/service/AppUserService.java index c2883d3..9534795 100644 --- a/src/main/java/io/linfeng/modules/admin/service/AppUserService.java +++ b/src/main/java/io/linfeng/modules/admin/service/AppUserService.java @@ -42,5 +42,9 @@ public interface AppUserService extends IService { AppUserResponse getUserInfo(AppUserEntity user); void updateAppUserInfo(AppUserUpdateForm appUserUpdateForm, AppUserEntity user); + + void addFollow(AddFollowForm request, AppUserEntity user); + + void cancelFollow(AddFollowForm request, AppUserEntity user); } diff --git a/src/main/java/io/linfeng/modules/admin/service/PostService.java b/src/main/java/io/linfeng/modules/admin/service/PostService.java index c34b81d..ff92646 100644 --- a/src/main/java/io/linfeng/modules/admin/service/PostService.java +++ b/src/main/java/io/linfeng/modules/admin/service/PostService.java @@ -23,12 +23,12 @@ public interface PostService extends IService { PageUtils queryPage(Map params); - Integer findTopicPostCount(Integer topicId); - - List findThreeMedia(Integer id); - void deleteByAdmin(List integers); Integer getPostNumByUid(Integer uid); + + AppPageUtils lastPost(Integer page); + + AppPageUtils followUserPost(Integer page, AppUserEntity user); } diff --git a/src/main/java/io/linfeng/modules/admin/service/impl/AppUserServiceImpl.java b/src/main/java/io/linfeng/modules/admin/service/impl/AppUserServiceImpl.java index 46b767b..dcbb47b 100644 --- a/src/main/java/io/linfeng/modules/admin/service/impl/AppUserServiceImpl.java +++ b/src/main/java/io/linfeng/modules/admin/service/impl/AppUserServiceImpl.java @@ -10,6 +10,9 @@ import io.linfeng.common.response.HomeRateResponse; import io.linfeng.common.utils.*; import io.linfeng.modules.admin.entity.PostEntity; import io.linfeng.modules.admin.service.*; +import io.linfeng.modules.app.dao.FollowDao; +import io.linfeng.modules.app.entity.FollowEntity; +import io.linfeng.modules.app.form.AddFollowForm; import io.linfeng.modules.app.form.AppUserUpdateForm; import io.linfeng.modules.app.form.SendCodeForm; import io.linfeng.modules.app.form.SmsLoginForm; @@ -43,6 +46,9 @@ public class AppUserServiceImpl extends ServiceImpl i @Autowired private FollowService followService; + @Autowired + private FollowDao followDao; + @Override public PageUtils queryPage(Map params) { QueryWrapper queryWrapper=new QueryWrapper<>(); @@ -168,6 +174,28 @@ public class AppUserServiceImpl extends ServiceImpl i redisUtils.delete("userId:"+user.getUid()); } + @Override + public void addFollow(AddFollowForm request, AppUserEntity user) { + if(request.getId().equals(user.getUid())){ + throw new LinfengException("不能关注自己哦"); + } + boolean isFollow=followService.isFollowOrNot(user.getUid(),request.getId()); + if(isFollow){ + throw new LinfengException("不要重复关注哦"); + } + FollowEntity followEntity=new FollowEntity(); + followEntity.setUid(user.getUid()); + followEntity.setFollowUid(request.getId()); + followEntity.setCreateTime(DateUtil.nowDateTime()); + followService.save(followEntity); + //TODO 消息通知 + } + + @Override + public void cancelFollow(AddFollowForm request, AppUserEntity user) { + followDao.cancelFollow(user.getUid(),request.getId()); + } + private Integer getTotalNum() { return this.lambdaQuery().select(AppUserEntity::getUid).count(); } diff --git a/src/main/java/io/linfeng/modules/admin/service/impl/PostServiceImpl.java b/src/main/java/io/linfeng/modules/admin/service/impl/PostServiceImpl.java index 4d72613..97a1c7e 100644 --- a/src/main/java/io/linfeng/modules/admin/service/impl/PostServiceImpl.java +++ b/src/main/java/io/linfeng/modules/admin/service/impl/PostServiceImpl.java @@ -18,6 +18,8 @@ import io.linfeng.modules.app.entity.TopicAdminEntity; import io.linfeng.modules.app.entity.UserTopicEntity; import io.linfeng.modules.app.form.*; import io.linfeng.modules.app.service.*; +import io.linfeng.modules.app.utils.LocalUser; +import javafx.geometry.Pos; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -25,6 +27,7 @@ import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; @@ -51,6 +54,11 @@ public class PostServiceImpl extends ServiceImpl implements private DiscussService discussService; @Autowired private MessageService messageService; + @Autowired + private LocalUser localUser; + @Autowired + private FollowService followService; + @Override public PageUtils queryPage(Map params) { @@ -95,42 +103,7 @@ public class PostServiceImpl extends ServiceImpl implements return pageUtils; } - @Override - public Integer findTopicPostCount(Integer topicId) { - LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery(); - lambdaQueryWrapper.eq(PostEntity::getTopicId, topicId); - return baseMapper.selectCount(lambdaQueryWrapper); - } - /** - * 选取圈子中热度最高的三条动态的首图作为展示 - * - * @param id - * @return - */ - @Override - public List findThreeMedia(Integer id) { - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.lambda().eq(PostEntity::getTopicId, id); - queryWrapper.lambda().eq(PostEntity::getType, 1); - queryWrapper.lambda().orderByDesc(PostEntity::getReadCount); - queryWrapper.last("limit 10"); - List postEntityList = baseMapper.selectList(queryWrapper); - List imageList = new ArrayList<>(); - for (int i = 0; i < postEntityList.size(); i++) { - if (!postEntityList.get(i).getMedia().equals("")) { - List jsonToList = JsonUtils.JsonToList(postEntityList.get(i).getMedia()); - if (jsonToList.size() > 0) { - if (imageList.size() > 2) { - break; - } else { - imageList.add(jsonToList.get(0)); - } - } - } - } - return imageList; - } @Override @@ -155,4 +128,62 @@ public class PostServiceImpl extends ServiceImpl implements .count(); } + @Override + public AppPageUtils lastPost(Integer currPage) { + Page page=new Page<>(currPage,10); + QueryWrapper queryWrapper=new QueryWrapper<>(); + queryWrapper.orderByDesc("post_top","id"); + AppUserEntity user = localUser.getUser(); + if(user==null){ + return this.mapPostList(page,queryWrapper,0); + } + return this.mapPostList(page,queryWrapper,user.getUid()); + } + + @Override + public AppPageUtils followUserPost(Integer page, AppUserEntity user) { + List list=followService.getFollowUid(user); + if(list.isEmpty()){ + return null; + } + QueryWrapper queryWrapper=new QueryWrapper<>(); + queryWrapper.lambda().in(PostEntity::getUid,list); + queryWrapper.orderByDesc("post_top","id"); + Page pages=new Page<>(page,10); + return this.mapPostList(pages,queryWrapper,user.getUid()); + } + + + /** + * 组装帖子分页 + * 在一个循环里 尽量减少数据库查询操作 这种方式并不太好 应该全部查询出来后再set值 + * + * @param page + * @param queryWrapper + * @param uid + * @return + */ + public AppPageUtils mapPostList(Page page,QueryWrapper queryWrapper,Integer uid){ + Page pages = baseMapper.selectPage(page,queryWrapper); + AppPageUtils appPage=new AppPageUtils(pages); + List data = (List) appPage.getData(); + List responseList=new ArrayList<>(); + data.forEach(l->{ + PostListResponse response=new PostListResponse(); + BeanUtils.copyProperties(l,response); + response.setCollectionCount(postCollectionService.collectCount(response.getId())); + response.setCommentCount(commentService.getCountByTopicId(response.getId())); + response.setUserInfo(appUserService.getById(response.getUid())); + if (uid==0){ + response.setIsCollection(false); + }else{ + response.setIsCollection(postCollectionService.isCollection(uid,response.getId())); + } + response.setMedia(JsonUtils.JsonToList(l.getMedia())); + responseList.add(response); + }); + appPage.setData(responseList); + return appPage; + } + } \ No newline at end of file diff --git a/src/main/java/io/linfeng/modules/app/controller/AppLoginController.java b/src/main/java/io/linfeng/modules/app/controller/AppLoginController.java index f5a020a..564d9b3 100644 --- a/src/main/java/io/linfeng/modules/app/controller/AppLoginController.java +++ b/src/main/java/io/linfeng/modules/app/controller/AppLoginController.java @@ -8,6 +8,7 @@ import io.linfeng.modules.admin.entity.AppUserEntity; import io.linfeng.modules.admin.service.AppUserService; import io.linfeng.modules.app.annotation.Login; import io.linfeng.modules.app.annotation.LoginUser; +import io.linfeng.modules.app.form.AddFollowForm; import io.linfeng.modules.app.form.AppUserUpdateForm; import io.linfeng.modules.app.form.SendCodeForm; import io.linfeng.modules.app.form.SmsLoginForm; @@ -89,4 +90,23 @@ public class AppLoginController { return R.ok("修改成功"); } + + + @Login + @PostMapping("/addFollow") + @ApiOperation("关注用户") + public R addFollow(@LoginUser AppUserEntity user, @RequestBody AddFollowForm request){ + appUserService.addFollow(request,user); + return R.ok("关注用户成功"); + } + + + @Login + @PostMapping("/cancelFollow") + @ApiOperation("取消关注用户") + public R cancelFollow(@LoginUser AppUserEntity user, @RequestBody AddFollowForm request){ + appUserService.cancelFollow(request,user); + return R.ok("取消关注用户成功"); + } + } diff --git a/src/main/java/io/linfeng/modules/app/controller/AppPostController.java b/src/main/java/io/linfeng/modules/app/controller/AppPostController.java new file mode 100644 index 0000000..0136a35 --- /dev/null +++ b/src/main/java/io/linfeng/modules/app/controller/AppPostController.java @@ -0,0 +1,56 @@ +package io.linfeng.modules.app.controller; + +import cn.hutool.core.util.ObjectUtil; +import io.linfeng.common.utils.AppPageUtils; +import io.linfeng.common.utils.R; +import io.linfeng.modules.admin.entity.AppUserEntity; +import io.linfeng.modules.admin.service.PostService; +import io.linfeng.modules.app.annotation.Login; +import io.linfeng.modules.app.annotation.LoginUser; +import io.linfeng.modules.app.service.PostCollectionService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author linfeng + * @date 2022/7/27 14:18 + */ +@Api(tags = "用户端——帖子") +@RestController +@RequestMapping("app/post") +public class AppPostController { + + @Autowired + private PostService postService; + + @Autowired + private PostCollectionService postCollectionService; + + + @GetMapping("/lastPost") + @ApiOperation("最新动态列表") + public R lastPost(@RequestParam Integer page){ + + AppPageUtils pages =postService.lastPost(page); + return R.ok().put("result", pages); + } + + + @Login + @GetMapping("/followUserPost") + @ApiOperation("获取关注用户帖子") + public R followUserPost(@RequestParam Integer page, @LoginUser AppUserEntity user){ + + AppPageUtils pages =postService.followUserPost(page,user); + if(ObjectUtil.isNull(page)){ + return R.error("您没有关注的用户"); + } + return R.ok().put("result", pages); + } + +} \ No newline at end of file diff --git a/src/main/java/io/linfeng/modules/app/service/FollowService.java b/src/main/java/io/linfeng/modules/app/service/FollowService.java index fb84936..10f544f 100644 --- a/src/main/java/io/linfeng/modules/app/service/FollowService.java +++ b/src/main/java/io/linfeng/modules/app/service/FollowService.java @@ -22,5 +22,9 @@ public interface FollowService extends IService { Integer getFollowCount(Integer uid); Integer getFans(Integer uid); + + boolean isFollowOrNot(Integer uid, Integer id); + + List getFollowUid(AppUserEntity user); } diff --git a/src/main/java/io/linfeng/modules/app/service/impl/FollowServiceImpl.java b/src/main/java/io/linfeng/modules/app/service/impl/FollowServiceImpl.java index 7a6cbf8..0540a3a 100644 --- a/src/main/java/io/linfeng/modules/app/service/impl/FollowServiceImpl.java +++ b/src/main/java/io/linfeng/modules/app/service/impl/FollowServiceImpl.java @@ -1,7 +1,14 @@ package io.linfeng.modules.app.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import io.linfeng.modules.admin.entity.AppUserEntity; import org.springframework.stereotype.Service; + +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; + import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -36,4 +43,23 @@ public class FollowServiceImpl extends ServiceImpl impl return this.lambdaQuery().eq(FollowEntity::getFollowUid,uid).count(); } + @Override + public boolean isFollowOrNot(Integer uid, Integer id) { + LambdaQueryWrapper queryWrapper= Wrappers.lambdaQuery(); + queryWrapper.eq(FollowEntity::getUid,uid); + queryWrapper.eq(FollowEntity::getFollowUid,id); + Integer num = baseMapper.selectCount(queryWrapper); + if(num==0){ + return false; + } + return true; + } + + @Override + public List getFollowUid(AppUserEntity user) { + List list = this.lambdaQuery().eq(FollowEntity::getUid, user.getUid()).list(); + List collect = list.stream().map(FollowEntity::getFollowUid).collect(Collectors.toList()); + return collect; + } + } \ No newline at end of file diff --git a/src/main/resources/static/linfeng-community-uniapp-ky/components/post-list/post-list.vue b/src/main/resources/static/linfeng-community-uniapp-ky/components/post-list/post-list.vue index 517d4ee..6145645 100644 --- a/src/main/resources/static/linfeng-community-uniapp-ky/components/post-list/post-list.vue +++ b/src/main/resources/static/linfeng-community-uniapp-ky/components/post-list/post-list.vue @@ -123,32 +123,12 @@ }, data() { return { - showAction: false, - - choosePost: '', - chooseIndex: '', + }; }, methods: { - copyPageUrl(id) { - let that = this; - uni.setClipboardData({ - data: this.$c.shareH5Url+'pages/post/post?id=' + id, - success: function() { - uni.hideToast(); - that.$q.toast('复制成功', 'success'); - that.showShare = false; - } - }); - }, - onActive(postInfo, index) { - this.showAction = true; - this.choosePost = postInfo; - this.chooseIndex = index; - }, - cancelCollection(id, index) { this.$H .post('post/cancelCollection', { @@ -174,16 +154,6 @@ } }); }, - - follow() { - this.$H - .post('user/addFollow', { - id: this.choosePost.uid - }) - .then(res => { - this.$u.toast(res.msg); - }); - }, previewImage(url, urls) { uni.previewImage({ current: url, // 当前显示图片的http链接 diff --git a/src/main/resources/static/linfeng-community-uniapp-ky/pages/index/index.vue b/src/main/resources/static/linfeng-community-uniapp-ky/pages/index/index.vue index 0785978..444c699 100644 --- a/src/main/resources/static/linfeng-community-uniapp-ky/pages/index/index.vue +++ b/src/main/resources/static/linfeng-community-uniapp-ky/pages/index/index.vue @@ -62,7 +62,7 @@ this.getLastPost(); }, onShow() { - this.getMsgNum(); + // this.getMsgNum(); }, onReachBottom() { if (this.current === 0) {