用户端接口

This commit is contained in:
linfeng 2022-07-27 15:25:54 +08:00
parent 90c7ef02cc
commit b5ea8424e9
11 changed files with 264 additions and 13 deletions

View File

@ -46,5 +46,9 @@ public interface AppUserService extends IService<AppUserEntity> {
void addFollow(AddFollowForm request, AppUserEntity user);
void cancelFollow(AddFollowForm request, AppUserEntity user);
AppPageUtils userFans(Integer page, Integer uid);
AppPageUtils follow(Integer page, AppUserEntity user);
}

View File

@ -26,5 +26,7 @@ public interface CommentService extends IService<CommentEntity> {
List<CommentEntity> getByPid(Long pid);
void deleteByAdmin(List<Long> longs);
Integer getCountByPostId(Integer id);
}

View File

@ -30,5 +30,13 @@ public interface PostService extends IService<PostEntity> {
AppPageUtils lastPost(Integer page);
AppPageUtils followUserPost(Integer page, AppUserEntity user);
void addCollection(AddCollectionForm request, AppUserEntity user);
AppPageUtils myPost(Integer page, AppUserEntity user);
AppPageUtils myCollectPost(Integer page,AppUserEntity user);
PostDetailResponse detail(Integer id);
}

View File

@ -4,9 +4,11 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.linfeng.common.exception.LinfengException;
import io.linfeng.common.response.AppUserResponse;
import io.linfeng.common.response.HomeRateResponse;
import io.linfeng.common.response.TopicUserResponse;
import io.linfeng.common.utils.*;
import io.linfeng.modules.admin.entity.PostEntity;
import io.linfeng.modules.admin.service.*;
@ -195,7 +197,56 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserDao, AppUserEntity> i
public void cancelFollow(AddFollowForm request, AppUserEntity user) {
followDao.cancelFollow(user.getUid(),request.getId());
}
@Override
public AppPageUtils userFans(Integer currPage, Integer uid) {
List<Integer> uidList=followService.getFansList(uid);
if (uidList.isEmpty()) {
return new AppPageUtils(null, 0, 10, currPage);
}
Page<AppUserEntity> page = new Page<>(currPage, 10);
QueryWrapper<AppUserEntity> queryWrapper1 = new QueryWrapper<>();
queryWrapper1.lambda().in(AppUserEntity::getUid, uidList);
Page<AppUserEntity> page1 = this.page(page, queryWrapper1);
AppPageUtils pages = new AppPageUtils(page1);
List<?> data = pages.getData();
List<TopicUserResponse> responseList = new ArrayList<>();
data.forEach(l -> {
TopicUserResponse topicUserResponse = new TopicUserResponse();
BeanUtils.copyProperties(l, topicUserResponse);
Integer follow = followService.isFollow(uid, topicUserResponse.getUid());
topicUserResponse.setHasFollow(follow);
responseList.add(topicUserResponse);
});
pages.setData(responseList);
return pages;
}
@Override
public AppPageUtils follow(Integer currPage, AppUserEntity user) {
List<Integer> followUids = followService.getFollowUids(user);
if (followUids.isEmpty()) {
return new AppPageUtils(null, 0, 10, currPage);
}
Page<AppUserEntity> page = new Page<>(currPage, 10);
QueryWrapper<AppUserEntity> queryWrapper1 = new QueryWrapper<>();
queryWrapper1.lambda().in(AppUserEntity::getUid, followUids);
Page<AppUserEntity> page1 = this.page(page, queryWrapper1);
AppPageUtils pages = new AppPageUtils(page1);
List<?> data = pages.getData();
List<TopicUserResponse> responseList = new ArrayList<>();
data.forEach(l -> {
TopicUserResponse topicUserResponse = new TopicUserResponse();
BeanUtils.copyProperties(l, topicUserResponse);
Integer follow = followService.isFollow(user.getUid(), topicUserResponse.getUid());
topicUserResponse.setHasFollow(follow);
responseList.add(topicUserResponse);
});
pages.setData(responseList);
return pages;
}
private Integer getTotalNum() {
return this.lambdaQuery().select(AppUserEntity::getUid).count();
}

View File

@ -64,4 +64,13 @@ public class CommentServiceImpl extends ServiceImpl<CommentDao, CommentEntity> i
.update();
});
}
@Override
public Integer getCountByPostId(Integer id) {
return baseMapper.selectCount(
new LambdaQueryWrapper<CommentEntity>()
.eq(CommentEntity::getStatus, 1)
.eq(CommentEntity::getPostId, id));
}
}

View File

@ -153,7 +153,72 @@ public class PostServiceImpl extends ServiceImpl<PostDao, PostEntity> implements
return this.mapPostList(pages,queryWrapper,user.getUid());
}
@Override
public void addCollection(AddCollectionForm request, AppUserEntity user) {
Boolean collection = postCollectionService.isCollection(user.getUid(), request.getId());
if(collection){
throw new LinfengException("请勿重复点赞");
}
PostCollectionEntity pc=new PostCollectionEntity();
pc.setPostId(request.getId());
pc.setUid(user.getUid());
postCollectionService.save(pc);
//TODO 消息通知
}
@Override
public AppPageUtils myPost(Integer page, AppUserEntity user) {
QueryWrapper<PostEntity> queryWrapper=new QueryWrapper<>();
queryWrapper.lambda().eq(PostEntity::getUid,user.getUid());
queryWrapper.lambda().orderByDesc(PostEntity::getId);
Page<PostEntity> pages = new Page<>(page,10);
return this.mapPostList(pages, queryWrapper, user.getUid());
}
@Override
public AppPageUtils myCollectPost(Integer page,AppUserEntity user) {
List<Integer> postIdList=postCollectionService.getPostListByUid(user.getUid());
if(postIdList.size()==0){
return new AppPageUtils(null, 0, 10, 1);
}
QueryWrapper<PostEntity> queryWrapper=new QueryWrapper<>();
queryWrapper.lambda().in(PostEntity::getId,postIdList);
queryWrapper.lambda().orderByDesc(PostEntity::getId);
Page<PostEntity> pages = new Page<>(page,10);
return this.mapPostList(pages, queryWrapper, user.getUid());
}
@Override
public PostDetailResponse detail(Integer id) {
PostEntity post = this.getById(id);
if(ObjectUtil.isNull(post)){
throw new LinfengException("该帖子不存在或已删除");
}
AppUserEntity user = localUser.getUser();
post.setReadCount(post.getReadCount()+1);
baseMapper.updateById(post);
PostDetailResponse response=new PostDetailResponse();
BeanUtils.copyProperties(post,response);
AppUserEntity userInfo = appUserService.getById(post.getUid());
response.setUserInfo(userInfo);
if(ObjectUtil.isNull(user)){
response.setIsFollow(false);
response.setIsCollection(false);
}else{
response.setIsFollow(followService.isFollowOrNot(user.getUid(), post.getUid()));
response.setIsCollection(postCollectionService.isCollection(user.getUid(),post.getId()));
}
response.setCollectionCount(postCollectionService.collectCount(post.getId()));
response.setCommentCount(commentService.getCountByPostId(post.getId()));
response.setMedia(JsonUtils.JsonToList(post.getMedia()));//文件处理
return response;
}
/**
* 组装帖子分页
* 在一个循环里 尽量减少数据库查询操作 这种方式并不太好 应该全部查询出来后再set值

View File

@ -1,17 +1,13 @@
package io.linfeng.modules.app.controller;
import io.linfeng.common.response.AppUserResponse;
import io.linfeng.common.utils.AppPageUtils;
import io.linfeng.common.utils.R;
import io.linfeng.common.utils.RedisUtils;
import io.linfeng.common.validator.ValidatorUtils;
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;
import io.linfeng.modules.app.form.*;
import io.linfeng.modules.app.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -40,7 +36,6 @@ public class AppLoginController {
private AppUserService appUserService;
@PostMapping("/sendSmsCode")
@ApiOperation("发送验证码")
public R sendSmsCode(@RequestBody SendCodeForm param){
@ -108,5 +103,22 @@ public class AppLoginController {
appUserService.cancelFollow(request,user);
return R.ok("取消关注用户成功");
}
@Login
@GetMapping("/userFans")
@ApiOperation("我的粉丝分页列表")
public R userFans(@RequestParam("page") Integer page,@LoginUser AppUserEntity user){
AppPageUtils pages =appUserService.userFans(page,user.getUid());
return R.ok().put("result", pages);
}
@Login
@GetMapping("/follow")
@ApiOperation("我的关注分页列表")
public R follow(@RequestParam("page") Integer page,@LoginUser AppUserEntity user){
AppPageUtils pages =appUserService.follow(page,user);
return R.ok().put("result", pages);
}
}

View File

@ -1,20 +1,19 @@
package io.linfeng.modules.app.controller;
import cn.hutool.core.util.ObjectUtil;
import io.linfeng.common.response.PostDetailResponse;
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.form.AddCollectionForm;
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;
import org.springframework.web.bind.annotation.*;
/**
* @author linfeng
@ -27,11 +26,11 @@ public class AppPostController {
@Autowired
private PostService postService;
@Autowired
private PostCollectionService postCollectionService;
@GetMapping("/lastPost")
@ApiOperation("最新动态列表")
public R lastPost(@RequestParam Integer page){
@ -53,4 +52,58 @@ public class AppPostController {
return R.ok().put("result", pages);
}
/**
* 帖子点赞收藏
*/
@Login
@PostMapping("/addCollection")
@ApiOperation("帖子点赞收藏")
public R addCollection(@RequestBody AddCollectionForm request, @LoginUser AppUserEntity user){
postService.addCollection(request,user);
return R.ok();
}
/**
* 帖子取消点赞收藏
*/
@Login
@PostMapping("/cancelCollection")
@ApiOperation("帖子取消点赞收藏")
public R cancelCollection(@RequestBody AddCollectionForm request, @LoginUser AppUserEntity user){
postCollectionService.cancelCollection(request,user);
return R.ok();
}
@Login
@GetMapping("/myPost")
@ApiOperation("我的帖子")
public R myPost(@RequestParam("page") Integer page, @LoginUser AppUserEntity user){
AppPageUtils pages =postService.myPost(page,user);
return R.ok().put("result", pages);
}
@Login
@GetMapping("/myCollectPost")
@ApiOperation("我点赞收藏的帖子")
public R myCollectPost(@RequestParam("page") Integer page, @LoginUser AppUserEntity user){
AppPageUtils pages =postService.myCollectPost(page,user);
return R.ok().put("result", pages);
}
@GetMapping("/detail")
@ApiOperation("获取帖子详情")
public R detail(@RequestParam Integer id){
PostDetailResponse response=postService.detail(id);
return R.ok().put("result", response);
}
}

View File

@ -26,5 +26,11 @@ public interface FollowService extends IService<FollowEntity> {
boolean isFollowOrNot(Integer uid, Integer id);
List<Integer> getFollowUid(AppUserEntity user);
List<Integer> getFollowUids(AppUserEntity user);
Integer isFollow(Integer uid,Integer followUid);
List<Integer> getFansList(Integer uid);
}

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.linfeng.modules.admin.entity.AppUserEntity;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@ -62,4 +63,44 @@ public class FollowServiceImpl extends ServiceImpl<FollowDao, FollowEntity> impl
return collect;
}
@Override
public Integer isFollow(Integer uid, Integer followUid) {
LambdaQueryWrapper<FollowEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(FollowEntity::getUid, uid);
lambdaQueryWrapper.eq(FollowEntity::getFollowUid, followUid);
Integer num = baseMapper.selectCount(lambdaQueryWrapper);
if(num == 0){
return 0;
}
LambdaQueryWrapper<FollowEntity> lambdaQueryWrapper2 = Wrappers.lambdaQuery();
lambdaQueryWrapper2.eq(FollowEntity::getUid, followUid);
lambdaQueryWrapper2.eq(FollowEntity::getFollowUid, uid);
Integer num2 = baseMapper.selectCount(lambdaQueryWrapper2);
if(num2==0){
return 2;
}
return 1;
}
@Override
public List<Integer> getFansList(Integer uid) {
List<FollowEntity> list = this.lambdaQuery()
.eq(FollowEntity::getFollowUid, uid)
.orderByDesc(FollowEntity::getId)
.list();
if(list.isEmpty()){
return new ArrayList<>();
}
List<Integer> collect = list.stream().map(FollowEntity::getUid).collect(Collectors.toList());
return collect;
}
@Override
public List<Integer> getFollowUids(AppUserEntity user) {
List<FollowEntity> list = this.lambdaQuery().eq(FollowEntity::getUid, user.getUid()).list();
List<Integer> collect = list.stream().map(FollowEntity::getFollowUid).collect(Collectors.toList());
return collect;
}
}

View File

@ -80,7 +80,7 @@
this.page1 = 1;
this.followUserPost = [];
this.getFollowUserPost();
this.getMsgNum();
// this.getMsgNum();
}
if (this.current === 1) {
this.page2 = 1;