Merge remote-tracking branch 'origin/wx-dev' into wx-dev

This commit is contained in:
ronger 2020-09-14 10:38:36 +08:00
commit af1ba638f4
11 changed files with 194 additions and 13 deletions

View File

@ -0,0 +1,19 @@
package com.rymcu.vertical.mapper;
import com.rymcu.vertical.core.mapper.Mapper;
import com.rymcu.vertical.entity.Follow;
import org.apache.ibatis.annotations.Param;
/**
* @author ronger
*/
public interface FollowMapper extends Mapper<Follow> {
/**
* 判断是否关注
* @param followingId
* @param followerId
* @param followingType
* @return
*/
Boolean isFollow(@Param("followingId") Integer followingId, @Param("followerId") Integer followerId, @Param("followingType") String followingType);
}

View File

@ -0,0 +1,35 @@
package com.rymcu.vertical.service;
import com.rymcu.vertical.core.service.Service;
import com.rymcu.vertical.entity.Follow;
import com.rymcu.vertical.web.api.exception.BaseApiException;
/**
* @author ronger
*/
public interface FollowService extends Service<Follow> {
/**
* 判断是否关注
* @param followingId
* @param followingType
* @return
* @throws BaseApiException
*/
Boolean isFollow(Integer followingId, String followingType) throws BaseApiException;
/**
* 关注操作
* @param follow
* @return
* @throws BaseApiException
*/
Boolean follow(Follow follow) throws BaseApiException;
/**
* 取消关注操作
* @param follow
* @return
* @throws BaseApiException
*/
Boolean cancelFollow(Follow follow) throws BaseApiException;
}

View File

@ -118,7 +118,7 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
String articleTags = article.getArticleTags();
String articleContent = article.getArticleContent();
String articleContentHtml = article.getArticleContentHtml();
User user = UserUtils.getWxCurrentUser();
User user = UserUtils.getCurrentUserByToken();
String reservedTag = checkTags(articleTags);
boolean notification = false;
if (StringUtils.isNotBlank(reservedTag)) {
@ -249,7 +249,7 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
@Override
public Map share(Integer id) throws BaseApiException {
Article article = articleMapper.selectByPrimaryKey(id);
User user = UserUtils.getWxCurrentUser();
User user = UserUtils.getCurrentUserByToken();
StringBuilder shareUrl = new StringBuilder(article.getArticlePermalink());
shareUrl.append("?s=").append(user.getNickname());
Map map = new HashMap(1);
@ -259,7 +259,7 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
@Override
public List<ArticleDTO> findDrafts() throws BaseApiException {
User user = UserUtils.getWxCurrentUser();
User user = UserUtils.getCurrentUserByToken();
List<ArticleDTO> list = articleMapper.selectDrafts(user.getIdUser());
list.forEach(article -> {
genArticle(article, 0);

View File

@ -0,0 +1,50 @@
package com.rymcu.vertical.service.impl;
import com.rymcu.vertical.core.constant.NotificationConstant;
import com.rymcu.vertical.core.service.AbstractService;
import com.rymcu.vertical.entity.Follow;
import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.mapper.FollowMapper;
import com.rymcu.vertical.service.FollowService;
import com.rymcu.vertical.util.NotificationUtils;
import com.rymcu.vertical.util.UserUtils;
import com.rymcu.vertical.web.api.exception.BaseApiException;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author ronger
*/
@Service
public class FollowServiceImpl extends AbstractService<Follow> implements FollowService {
@Resource
private FollowMapper followMapper;
@Override
public Boolean isFollow(Integer followingId, String followingType) throws BaseApiException {
User tokenUser = UserUtils.getCurrentUserByToken();
Boolean b = followMapper.isFollow(followingId, tokenUser.getIdUser(), followingType);
return b;
}
@Override
public Boolean follow(Follow follow) throws BaseApiException {
User tokenUser = UserUtils.getCurrentUserByToken();
follow.setFollowerId(tokenUser.getIdUser());
int result = followMapper.insertSelective(follow);
if (result > 0) {
NotificationUtils.saveNotification(follow.getFollowingId(), follow.getIdFollow(), NotificationConstant.Follow, tokenUser.getNickname() + " 关注了你!");
}
return result > 0;
}
@Override
public Boolean cancelFollow(Follow follow) throws BaseApiException {
User tokenUser = UserUtils.getCurrentUserByToken();
follow.setFollowerId(tokenUser.getIdUser());
int result = followMapper.delete(follow);
return result == 0;
}
}

View File

@ -5,14 +5,13 @@ import com.rymcu.vertical.dto.ArticleDTO;
import com.rymcu.vertical.dto.Author;
import com.rymcu.vertical.dto.NotificationDTO;
import com.rymcu.vertical.entity.Comment;
import com.rymcu.vertical.entity.Follow;
import com.rymcu.vertical.entity.Notification;
import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.mapper.NotificationMapper;
import com.rymcu.vertical.service.ArticleService;
import com.rymcu.vertical.service.CommentService;
import com.rymcu.vertical.service.NotificationService;
import com.rymcu.vertical.service.UserService;
import com.rymcu.vertical.service.*;
import com.rymcu.vertical.util.BeanCopierUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -35,6 +34,10 @@ public class NotificationServiceImpl extends AbstractService<Notification> imple
private CommentService commentService;
@Resource
private UserService userService;
@Resource
private FollowService followService;
@Value("${resource.domain}")
private String domain;
@Override
public List<Notification> findUnreadNotifications(Integer idUser) {
@ -59,6 +62,7 @@ public class NotificationServiceImpl extends AbstractService<Notification> imple
ArticleDTO article;
Comment comment;
User user;
Follow follow;
switch (notification.getDataType()) {
case "0":
// 系统公告/帖子
@ -70,6 +74,11 @@ public class NotificationServiceImpl extends AbstractService<Notification> imple
break;
case "1":
// 关注
follow = followService.findById(notification.getDataId().toString());
notificationDTO.setDataTitle("关注提醒");
user = userService.findById(follow.getFollowerId().toString());
notificationDTO.setDataUrl(getFollowLink(follow.getFollowingType(), user.getNickname()));
notificationDTO.setAuthor(genAuthor(user));
break;
case "2":
// 回帖
@ -80,10 +89,25 @@ public class NotificationServiceImpl extends AbstractService<Notification> imple
user = userService.findById(comment.getCommentAuthorId().toString());
notificationDTO.setAuthor(genAuthor(user));
break;
default:
break;
}
return notificationDTO;
}
private String getFollowLink(String followingType, String id) {
StringBuilder url = new StringBuilder();
url.append(domain);
switch (followingType) {
case "0":
url = url.append("/user/").append(id);
break;
default:
url.append("/notification");
}
return url.toString();
}
private Author genAuthor(User user) {
Author author = new Author();
author.setUserNickname(user.getNickname());

View File

@ -62,7 +62,7 @@ public class PortfolioServiceImpl extends AbstractService<Portfolio> implements
@Override
public Portfolio postPortfolio(Portfolio portfolio) throws BaseApiException {
User user = UserUtils.getWxCurrentUser();
User user = UserUtils.getCurrentUserByToken();
if (portfolio.getIdPortfolio() == null || portfolio.getIdPortfolio() == 0) {
portfolio.setPortfolioAuthorId(user.getIdUser());
portfolio.setCreatedTime(new Date());
@ -78,7 +78,7 @@ public class PortfolioServiceImpl extends AbstractService<Portfolio> implements
@Override
public Map findUnbindArticles(Integer page, Integer rows, String searchText, Integer idPortfolio) throws BaseApiException {
Map map = new HashMap(1);
User user = UserUtils.getWxCurrentUser();
User user = UserUtils.getCurrentUserByToken();
Portfolio portfolio = portfolioMapper.selectByPrimaryKey(idPortfolio);
if (portfolio == null) {
map.put("message", "该作品集不存在或已被删除!");

View File

@ -39,7 +39,7 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
@Override
@Transactional(rollbackFor = { UnsupportedEncodingException.class,BaseApiException.class })
public Integer saveTagArticle(Article article) throws UnsupportedEncodingException, BaseApiException {
User user = UserUtils.getWxCurrentUser();
User user = UserUtils.getCurrentUserByToken();
String articleTags = article.getArticleTags();
if(StringUtils.isNotBlank(articleTags)){
String[] tags = articleTags.split(",");

View File

@ -25,7 +25,7 @@ public class UserUtils {
* 通过token获取当前用户的信息
* @return
*/
public static User getWxCurrentUser() throws BaseApiException {
public static User getCurrentUserByToken() throws BaseApiException {
String authHeader = ContextHolderUtils.getRequest().getHeader(JwtConstants.AUTHORIZATION);
if (authHeader == null) {
return null;

View File

@ -0,0 +1,45 @@
package com.rymcu.vertical.web.api.follow;
import com.rymcu.vertical.core.result.GlobalResult;
import com.rymcu.vertical.core.result.GlobalResultGenerator;
import com.rymcu.vertical.entity.Follow;
import com.rymcu.vertical.jwt.def.JwtConstants;
import com.rymcu.vertical.service.FollowService;
import com.rymcu.vertical.web.api.exception.BaseApiException;
import com.rymcu.vertical.web.api.exception.ErrorCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
/**
* @author ronger
*/
@RestController
@RequestMapping("/api/v1/follow")
public class FollowController {
@Resource
private FollowService followService;
@GetMapping("/is-follow")
public GlobalResult isFollow(@RequestParam Integer followingId, @RequestParam String followingType) throws BaseApiException {
Boolean b = followService.isFollow(followingId, followingType);
return GlobalResultGenerator.genSuccessResult(b);
}
@PostMapping
public GlobalResult follow(@RequestBody Follow follow) throws BaseApiException {
Boolean b = followService.follow(follow);
return GlobalResultGenerator.genSuccessResult(b);
}
@PostMapping("cancel-follow")
public GlobalResult cancelFollow(@RequestBody Follow follow) throws BaseApiException {
Boolean b = followService.cancelFollow(follow);
return GlobalResultGenerator.genSuccessResult(b);
}
}

View File

@ -30,7 +30,7 @@ public class NotificationController {
@GetMapping("/all")
public GlobalResult notifications(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows) throws BaseApiException {
User user = UserUtils.getWxCurrentUser();
User user = UserUtils.getCurrentUserByToken();
PageHelper.startPage(page, rows);
List<NotificationDTO> list = notificationService.findNotifications(user.getIdUser());
PageInfo<NotificationDTO> pageInfo = new PageInfo(list);
@ -40,7 +40,7 @@ public class NotificationController {
@GetMapping("/unread")
public GlobalResult unreadNotification(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows) throws BaseApiException {
User user = UserUtils.getWxCurrentUser();
User user = UserUtils.getCurrentUserByToken();
PageHelper.startPage(page, rows);
List<Notification> list = notificationService.findUnreadNotifications(user.getIdUser());
PageInfo<Notification> pageInfo = new PageInfo(list);

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.rymcu.vertical.mapper.FollowMapper">
<select id="isFollow" resultType="java.lang.Boolean">
select ifnull((select true from vertical_follow where follower_id = #{followerId}
and following_id = #{followingId} and following_type = #{followingType}), false)
</select>
</mapper>