用户端首页和用户接口

This commit is contained in:
linfeng 2022-07-27 14:51:29 +08:00
parent a3ddbc0618
commit 90c7ef02cc
10 changed files with 210 additions and 71 deletions

View File

@ -42,5 +42,9 @@ public interface AppUserService extends IService<AppUserEntity> {
AppUserResponse getUserInfo(AppUserEntity user);
void updateAppUserInfo(AppUserUpdateForm appUserUpdateForm, AppUserEntity user);
void addFollow(AddFollowForm request, AppUserEntity user);
void cancelFollow(AddFollowForm request, AppUserEntity user);
}

View File

@ -23,12 +23,12 @@ public interface PostService extends IService<PostEntity> {
PageUtils queryPage(Map<String, Object> params);
Integer findTopicPostCount(Integer topicId);
List<String> findThreeMedia(Integer id);
void deleteByAdmin(List<Integer> integers);
Integer getPostNumByUid(Integer uid);
AppPageUtils lastPost(Integer page);
AppPageUtils followUserPost(Integer page, AppUserEntity user);
}

View File

@ -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<AppUserDao, AppUserEntity> i
@Autowired
private FollowService followService;
@Autowired
private FollowDao followDao;
@Override
public PageUtils queryPage(Map<String, Object> params) {
QueryWrapper<AppUserEntity> queryWrapper=new QueryWrapper<>();
@ -168,6 +174,28 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserDao, AppUserEntity> 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();
}

View File

@ -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<PostDao, PostEntity> implements
private DiscussService discussService;
@Autowired
private MessageService messageService;
@Autowired
private LocalUser localUser;
@Autowired
private FollowService followService;
@Override
public PageUtils queryPage(Map<String, Object> params) {
@ -95,42 +103,7 @@ public class PostServiceImpl extends ServiceImpl<PostDao, PostEntity> implements
return pageUtils;
}
@Override
public Integer findTopicPostCount(Integer topicId) {
LambdaQueryWrapper<PostEntity> lambdaQueryWrapper = Wrappers.lambdaQuery();
lambdaQueryWrapper.eq(PostEntity::getTopicId, topicId);
return baseMapper.selectCount(lambdaQueryWrapper);
}
/**
* 选取圈子中热度最高的三条动态的首图作为展示
*
* @param id
* @return
*/
@Override
public List<String> findThreeMedia(Integer id) {
QueryWrapper<PostEntity> 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<PostEntity> postEntityList = baseMapper.selectList(queryWrapper);
List<String> imageList = new ArrayList<>();
for (int i = 0; i < postEntityList.size(); i++) {
if (!postEntityList.get(i).getMedia().equals("")) {
List<String> 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<PostDao, PostEntity> implements
.count();
}
@Override
public AppPageUtils lastPost(Integer currPage) {
Page<PostEntity> page=new Page<>(currPage,10);
QueryWrapper<PostEntity> 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<Integer> list=followService.getFollowUid(user);
if(list.isEmpty()){
return null;
}
QueryWrapper<PostEntity> queryWrapper=new QueryWrapper<>();
queryWrapper.lambda().in(PostEntity::getUid,list);
queryWrapper.orderByDesc("post_top","id");
Page<PostEntity> pages=new Page<>(page,10);
return this.mapPostList(pages,queryWrapper,user.getUid());
}
/**
* 组装帖子分页
* 在一个循环里 尽量减少数据库查询操作 这种方式并不太好 应该全部查询出来后再set值
*
* @param page
* @param queryWrapper
* @param uid
* @return
*/
public AppPageUtils mapPostList(Page<PostEntity> page,QueryWrapper<PostEntity> queryWrapper,Integer uid){
Page<PostEntity> pages = baseMapper.selectPage(page,queryWrapper);
AppPageUtils appPage=new AppPageUtils(pages);
List<PostEntity> data = (List<PostEntity>) appPage.getData();
List<PostListResponse> 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;
}
}

View File

@ -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("取消关注用户成功");
}
}

View File

@ -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);
}
}

View File

@ -22,5 +22,9 @@ public interface FollowService extends IService<FollowEntity> {
Integer getFollowCount(Integer uid);
Integer getFans(Integer uid);
boolean isFollowOrNot(Integer uid, Integer id);
List<Integer> getFollowUid(AppUserEntity user);
}

View File

@ -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<FollowDao, FollowEntity> impl
return this.lambdaQuery().eq(FollowEntity::getFollowUid,uid).count();
}
@Override
public boolean isFollowOrNot(Integer uid, Integer id) {
LambdaQueryWrapper<FollowEntity> 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<Integer> getFollowUid(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

@ -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

View File

@ -62,7 +62,7 @@
this.getLastPost();
},
onShow() {
this.getMsgNum();
// this.getMsgNum();
},
onReachBottom() {
if (this.current === 0) {