diff --git a/src/main/java/com/rymcu/vertical/config/WebMvcConfigurer.java b/src/main/java/com/rymcu/vertical/config/WebMvcConfigurer.java index 7aa8c41..2879de4 100644 --- a/src/main/java/com/rymcu/vertical/config/WebMvcConfigurer.java +++ b/src/main/java/com/rymcu/vertical/config/WebMvcConfigurer.java @@ -71,7 +71,7 @@ public class WebMvcConfigurer extends WebMvcConfigurationSupport { public void addInterceptors(InterceptorRegistry registry) { // TODO 先不拦截接口,进行测试 registry.addInterceptor(restAuthTokenInterceptor()).addPathPatterns("/api/**") - .excludePathPatterns("/api/v1/**"); + .excludePathPatterns("/api/v1/console/**","/api/v1/article/articles/**","/api/v1/article/detail/**"); } diff --git a/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java b/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java index 8f16d2b..bb146a2 100644 --- a/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java +++ b/src/main/java/com/rymcu/vertical/dto/ArticleDTO.java @@ -24,7 +24,9 @@ public class ArticleDTO { /** 浏览总数 */ private Integer articleViewCount ; /** 预览内容 */ - private Integer articlePreviewContent ; + private String articlePreviewContent ; + /** 文章内容 */ + private String articleContent ; /** 评论总数 */ private Integer commentCount ; /** 过去时长 */ diff --git a/src/main/java/com/rymcu/vertical/entity/Article.java b/src/main/java/com/rymcu/vertical/entity/Article.java index c30f3fa..f35c013 100644 --- a/src/main/java/com/rymcu/vertical/entity/Article.java +++ b/src/main/java/com/rymcu/vertical/entity/Article.java @@ -26,9 +26,9 @@ public class Article implements Serializable,Cloneable { /** 文章标签 */ private String articleTags ; /** 浏览总数 */ - private Integer articleViewCount ; + private Integer articleViewCount; /** 预览内容 */ - private Integer articlePreviewContent ; + private String articlePreviewContent ; /** 评论总数 */ private Integer commentCount ; /** 文章永久链接 */ diff --git a/src/main/java/com/rymcu/vertical/entity/ArticleContent.java b/src/main/java/com/rymcu/vertical/entity/ArticleContent.java new file mode 100644 index 0000000..6503a88 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/entity/ArticleContent.java @@ -0,0 +1,20 @@ +package com.rymcu.vertical.entity; + +import lombok.Data; + +import java.util.Date; + +@Data +public class ArticleContent { + + private Integer idArticle; + + private String articleContent; + + private String articleContentHtml; + + private Date createdTime; + + private Date updatedTime; + +} diff --git a/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java b/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java index 3efc1f6..261cae0 100644 --- a/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java +++ b/src/main/java/com/rymcu/vertical/mapper/ArticleMapper.java @@ -4,6 +4,7 @@ import com.rymcu.vertical.core.mapper.Mapper; import com.rymcu.vertical.dto.ArticleDTO; import com.rymcu.vertical.dto.Author; import com.rymcu.vertical.entity.Article; +import com.rymcu.vertical.entity.ArticleContent; import org.apache.ibatis.annotations.Param; import java.util.List; @@ -18,4 +19,6 @@ public interface ArticleMapper extends Mapper
{ Integer insertArticleContent(@Param("idArticle") Integer idArticle, @Param("articleContent") String articleContent, @Param("articleContentHtml") String articleContentHtml); Integer updateArticleContent(@Param("idArticle") Integer idArticle, @Param("articleContent") String articleContent, @Param("articleContentHtml") String articleContentHtml); + + ArticleContent selectArticleContent(@Param("idArticle") Integer idArticle); } diff --git a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java index 121d800..2127fb7 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java @@ -4,8 +4,12 @@ import com.rymcu.vertical.core.service.AbstractService; import com.rymcu.vertical.dto.ArticleDTO; import com.rymcu.vertical.dto.Author; import com.rymcu.vertical.entity.Article; +import com.rymcu.vertical.entity.ArticleContent; import com.rymcu.vertical.mapper.ArticleMapper; import com.rymcu.vertical.service.ArticleService; +import com.rymcu.vertical.util.Html2TextUtil; +import com.rymcu.vertical.util.Utils; +import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -28,7 +32,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti public List articles(String searchText, String tag) { List list = articleMapper.selectArticles(searchText, tag); list.forEach(article->{ - article = genArticle(article); + article = genArticle(article,0); }); return list; } @@ -53,6 +57,14 @@ public class ArticleServiceImpl extends AbstractService
implements Arti article = articleMapper.selectByPrimaryKey(idArticle); article.setArticleTitle(articleTitle); article.setArticleTags(articleTags); + if(StringUtils.isNotBlank(articleContentHtml)){ + Integer length = articleContentHtml.length(); + if(length>200){ + length = 200; + } + String articlePreviewContent = articleContentHtml.substring(0,length); + article.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent)); + } article.setUpdatedTime(new Date()); articleMapper.updateArticleContent(article.getIdArticle(),articleContent,articleContentHtml); } @@ -64,13 +76,28 @@ public class ArticleServiceImpl extends AbstractService
implements Arti @Override public ArticleDTO findArticleDTOById(Integer id) { ArticleDTO articleDTO = articleMapper.selectArticleDTOById(id); - articleDTO = genArticle(articleDTO); + articleDTO = genArticle(articleDTO,1); return articleDTO; } - private ArticleDTO genArticle(ArticleDTO article) { + private ArticleDTO genArticle(ArticleDTO article,Integer type) { Author author = articleMapper.selectAuthor(article.getArticleAuthorId()); article.setArticleAuthor(author); + article.setTimeAgo(Utils.getTimeAgo(article.getUpdatedTime())); + if(type == 1){ + ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); + article.setArticleContent(articleContent.getArticleContentHtml()); + } else { + if(StringUtils.isBlank(article.getArticlePreviewContent())){ + ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); + Integer length = articleContent.getArticleContentHtml().length(); + if(length>200){ + length = 200; + } + String articlePreviewContent = articleContent.getArticleContentHtml().substring(0,length); + article.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent)); + } + } return article; } } diff --git a/src/main/java/com/rymcu/vertical/util/Html2TextUtil.java b/src/main/java/com/rymcu/vertical/util/Html2TextUtil.java new file mode 100644 index 0000000..2ac84cd --- /dev/null +++ b/src/main/java/com/rymcu/vertical/util/Html2TextUtil.java @@ -0,0 +1,43 @@ +package com.rymcu.vertical.util; + +import javax.swing.text.html.HTMLEditorKit; +import javax.swing.text.html.parser.ParserDelegator; +import java.io.*; + +public class Html2TextUtil extends HTMLEditorKit.ParserCallback { + + private static Html2TextUtil html2Text = new Html2TextUtil(); + + private StringBuffer s; + + public Html2TextUtil() { + } + + public void parse(String html) throws IOException { + InputStream inputStream = new ByteArrayInputStream(html.getBytes()); + Reader reader = new InputStreamReader(inputStream); + ParserDelegator delegator = new ParserDelegator(); + s = new StringBuffer(); + // the third parameter is TRUE to ignore charset directive + delegator.parse(reader, this, Boolean.TRUE); + } + + public void handleText(char[] text, int pos) { + s.append(text); + } + + public String getText() { + return s.toString(); + } + + public static String getContent(String str) { + try { + html2Text.parse(str); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return html2Text.getText(); + } + +} diff --git a/src/main/java/com/rymcu/vertical/util/Utils.java b/src/main/java/com/rymcu/vertical/util/Utils.java index 97a4bcf..6e193ff 100644 --- a/src/main/java/com/rymcu/vertical/util/Utils.java +++ b/src/main/java/com/rymcu/vertical/util/Utils.java @@ -6,6 +6,10 @@ import org.apache.shiro.session.InvalidSessionException; import org.apache.shiro.session.Session; import org.apache.shiro.subject.Subject; +import java.text.ParseException; +import java.time.*; +import java.util.Date; + public class Utils { public static final String HASH_ALGORITHM = "SHA-1"; public static final int HASH_INTERATIONS = 1024; @@ -57,4 +61,48 @@ public class Utils { Integer code = (int)((Math.random()*9+1)*100000); return code; } + + public static String getTimeAgo(Date date) { + + String timeAgo; + + Instant instant = date.toInstant(); + ZoneId zone = ZoneId.systemDefault(); + LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone); + LocalDate oldLocalDate = localDateTime.toLocalDate(); + + LocalDate today = LocalDate.now(); + + Period p = Period.between(oldLocalDate, today); + if(p.getYears() > 0){ + timeAgo = p.getYears()+" 年前 "; + }else if(p.getMonths() > 0){ + timeAgo = p.getMonths()+" 月前 "; + }else if(p.getDays() > 0){ + timeAgo = p.getDays()+" 天前 "; + }else { + long to = System.currentTimeMillis(); + long from = date.getTime(); + int hours = (int) ((to - from)/(1000 * 60 * 60)); + if(hours > 0){ + timeAgo = hours+" 小时前 "; + }else { + int minutes = (int) ((to - from)/(1000 * 60)); + if(minutes > 0){ + timeAgo = " 刚刚 "; + }else { + timeAgo = minutes+" 分钟前 "; + } + } + } + return timeAgo; + } + + public static void main(String[] args){ + LocalDate localDate = LocalDate.parse("2019-11-15"); + ZoneId zone = ZoneId.systemDefault(); + Instant instant = localDate.atStartOfDay().atZone(zone).toInstant(); + String s = getTimeAgo(Date.from(instant)); + System.out.println(s); + } } diff --git a/src/main/java/com/rymcu/vertical/web/api/article/ArticleController.java b/src/main/java/com/rymcu/vertical/web/api/article/ArticleController.java index 5381f0a..2c16440 100644 --- a/src/main/java/com/rymcu/vertical/web/api/article/ArticleController.java +++ b/src/main/java/com/rymcu/vertical/web/api/article/ArticleController.java @@ -47,7 +47,7 @@ public class ArticleController { return GlobalResultGenerator.genSuccessResult(map); } - @GetMapping("/{id}") + @GetMapping("/detail/{id}") public GlobalResult detail(@PathVariable Integer id){ ArticleDTO articleDTO = articleService.findArticleDTOById(id); Map map = new HashMap<>(); diff --git a/src/main/java/mapper/ArticleMapper.xml b/src/main/java/mapper/ArticleMapper.xml index 70e28df..6638e06 100644 --- a/src/main/java/mapper/ArticleMapper.xml +++ b/src/main/java/mapper/ArticleMapper.xml @@ -30,6 +30,7 @@ + @@ -37,9 +38,16 @@ - - - + + + + + + + + + + insert into article_content (id_article,article_content,article_content_html,created_time,updated_time) @@ -49,7 +57,7 @@ update article_content set article_content = #{articleContent},article_content_html = #{articleContentHtml},updated_time = sysdate() where id_article = #{idArticle} select art.*,su.nickname,su.avatar_url from article art left join sys_user su on art.article_author_id = su.id where art.id = #{id} + \ No newline at end of file