package com.light.hexo.core.admin.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.light.hexo.common.base.BaseServiceImpl;
import com.light.hexo.core.admin.component.BaiDuPushService;
import com.light.hexo.common.constant.ConfigEnum;
import com.light.hexo.common.constant.HexoExceptionEnum;
import com.light.hexo.mapper.mapper.PostMapper;
import com.light.hexo.mapper.base.BaseMapper;
import com.light.hexo.mapper.model.*;
import com.light.hexo.common.event.PostEvent;
import com.light.hexo.core.admin.service.*;
import com.light.hexo.core.portal.constant.PageConstant;
import com.light.hexo.core.portal.model.HexoPageInfo;
import com.light.hexo.common.base.BaseRequest;
import com.light.hexo.common.component.event.BaseEvent;
import com.light.hexo.common.component.event.EventEnum;
import com.light.hexo.common.component.event.EventPublisher;
import com.light.hexo.common.constant.HexoConstant;
import com.light.hexo.common.exception.GlobalException;
import com.light.hexo.common.request.PostRequest;
import com.light.hexo.common.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.WebApplicationContext;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;
import javax.servlet.ServletContext;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @Author MoonlightL
* @ClassName: PostServiceImpl
* @ProjectName hexo-boot
* @Description: 文章 Service 实现
* @DateTime 2020/7/31 17:28
*/
@CacheConfig(cacheNames = "postCache")
@Service
@Slf4j
public class PostServiceImpl extends BaseServiceImpl<Post> implements PostService {
@Autowired
private PostMapper postMapper;
@Autowired
private CategoryService categoryService;
@Autowired
private TagService tagService;
@Autowired
private PostTagService postTagService;
@Autowired
private ConfigService configService;
@Autowired
private BaiDuPushService baiDuPushService;
@Autowired
private PostTaskService postTaskService;
@Autowired
@Lazy
private EventPublisher eventPublisher;
private static final Integer COVER_NUM = 20;
@Override
public BaseMapper<Post> getBaseMapper() {
return this.postMapper;
}
@Override
protected Example getExample(BaseRequest request) {
// 获取查询参数
PostRequest postRequest = (PostRequest) request;
Example example = Example.builder(Post.class)
.notSelect("content", "contentHtml").orderByDesc("createTime").build();
Example.Criteria criteria = example.createCriteria();
Boolean publish = postRequest.getPublish();
if (publish != null) {
criteria.andEqualTo("publish", publish);
}
Boolean top = postRequest.getTop();
if (top != null) {
criteria.andEqualTo("top", top);
}
String title = postRequest.getTitle();
if (StringUtils.isNotBlank(title)) {
criteria.andLike("title", "%" + title.trim() + "%");
}
Integer categoryId = postRequest.getCategoryId();
if (categoryId != null) {
criteria.andEqualTo("categoryId", categoryId);
}
String publishDate = postRequest.getPublishDate();
if (StringUtils.isNotBlank(publishDate)) {
criteria.andEqualTo("publishDate", publishDate.trim());
}
String authCode = postRequest.getAuthCode();
if (StringUtils.isNotBlank(authCode)) {
if ("1".equals(authCode)) {
criteria.andNotEqualTo("authCode", "");
} else {
criteria.andEqualTo("authCode", "");
}
}
return example;
}
@Override
public PageInfo<Post> findPage(BaseRequest<Post> request) throws GlobalException {
PageInfo<Post> pageInfo = super.findPage(request);
List<Post> list = pageInfo.getList();
if (CollectionUtils.isEmpty(list)) {
return new PageInfo<>();
}
List<Category> categoryList = this.categoryService.findAll();
Map<Integer, Category> categoryMap = categoryList.stream().collect(Collectors.toMap(Category::getId, Function.identity(), (k1, k2)->k1));
for (Post post : list) {
Category category = categoryMap.get(post.getCategoryId());
post.setCategoryName(category == null ? "默认" : category.getName());
if (StringUtils.isNotBlank(post.getCustomLink())) {
post.setLink(post.getCustomLink() + ".html");
}
}
return pageInfo;
}
@Override
public void removePostBatch(List<String> idStrList) throws GlobalException {
List<Long> idList = idStrList.stream().map(Long::valueOf).collect(Collectors.toList());
Example example = new Example(Post.class);
example.createCriteria().andIn("id", idList);
int num = this.getBaseMapper().deleteByExample(example);
if (num > 0) {
this.eventPublisher.emit(new PostEvent(this,null, PostEvent.Type.POST_NUM));
}
}
@Override
@Transactional(rollbackFor = Exception.class)
public void savePost(Post post) throws GlobalException {
Category category = this.categoryService.findById(post.getCategoryId());
if (category == null || !category.getState()) {
ExceptionUtil.throwEx(HexoExceptionEnum.ERROR_CATEGORY_NOT_EXIST);
}
// 前端上传的参数可能带空格,影响条件查询
post.setTitle(post.getTitle().trim());
Example example = new Example(Post.class);
example.createCriteria().andEqualTo("title", post.getTitle());
int titleCount = this.getBaseMapper().selectCountByExample(example);
if (titleCount > 0) {
ExceptionUtil.throwEx(HexoExceptionEnum.ERROR_POST_TITLE_REPEAT);
}
if (StringUtils.isBlank(post.getAuthor())) {
post.setAuthor(this.configService.getConfigValue(ConfigEnum.BLOG_AUTHOR.getName()));
}
String coverUrl = post.getCoverUrl();
if(StringUtils.isBlank(coverUrl)) {
int num = new Random().nextInt(COVER_NUM);
coverUrl = HexoConstant.DEFAULT_IMG_DIR + "/post/post_cover_" + num + ".jpg";
post.setCoverUrl(coverUrl);
}
boolean isMarkdown = this.configService.getConfigValue(ConfigEnum.EDITOR_TYPE.getName()).equals("markdown");
String content = post.getContent();
// 摘要
post.setSummary(this.interceptContent(content, isMarkdown))
.setSummaryHtml(this.interceptContentHtml(content, isMarkdown));
if (isMarkdown) {
post.setContentHtml(MarkdownUtil.md2html(post.getContent()));
} else {
post.setContentHtml(this.transformHtml(post.getContent()));
}
LocalDateTime now = LocalDateTime.now();
LocalDateTime jobTime = post.getJ
没有合适的资源?快使用搜索试试~ 我知道了~
个人博客系统-前后端,基于hexo搭建
共1922个文件
js:434个
gif:366个
java:300个
需积分: 0 5 下载量 113 浏览量
2023-10-19
13:16:03
上传
评论
收藏 20.63MB ZIP 举报
温馨提示
个人博客系统,可自定义模版
资源推荐
资源详情
资源评论
收起资源包目录
个人博客系统-前后端,基于hexo搭建 (1922个子文件)
mvnw.cmd 6KB
codebase.min.css 351KB
bootstrap.css 143KB
bootstrap.min.css 118KB
bootstrap.min.css 118KB
bootstrap.min.css 118KB
iziModal.min.css 84KB
iziModal.min.css 84KB
editormd.css 76KB
animate.css 76KB
material-design-iconic-font.min.css 69KB
editormd.min.css 60KB
editormd.preview.css 55KB
app.css 46KB
editormd.preview.min.css 44KB
font-awesome.min.css 30KB
sweetalert2.min.css 26KB
ambiance.css 26KB
ambiance.css 26KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
bootstrap-theme.min.css 23KB
bootstrap-datepicker3.min.css 21KB
summernote.min.css 19KB
flatpickr.min.css 16KB
layer.css 14KB
layer.css 14KB
jquery.fancybox.min.css 12KB
jquery.fancybox.min.css 12KB
APlayer.min.css 12KB
dropzone.min.css 9KB
hb-comment.css 9KB
hb-comment.css 9KB
codemirror.css 9KB
awesome.css 8KB
codemirror.css 8KB
magnific-popup.css 7KB
metroStyle.css 6KB
zTreeStyle.css 6KB
fonts.googleapis.css 6KB
datepicker.css 6KB
datepicker.css 5KB
solarized.css 5KB
codemirror.min.css 5KB
emoji.css 5KB
layer.css 5KB
layer.css 5KB
mdn-like.css 5KB
mdn-like.css 5KB
datepicker.css 5KB
solarized.css 5KB
datepicker.css 5KB
share.min.css 4KB
liquibyte.css 4KB
waves.min.css 4KB
datepicker.css 4KB
merge.css 3KB
merge.css 3KB
yonce.css 3KB
xq-dark.css 3KB
lint.css 3KB
lint.css 3KB
jquery.search.css 3KB
xq-dark.css 3KB
material-palenight.css 3KB
duotone-light.css 3KB
darcula.css 3KB
lesser-dark.css 3KB
duotone-dark.css 3KB
material-darker.css 3KB
material-ocean.css 3KB
icecoder.css 2KB
pastel-on-dark.css 2KB
ttcn.css 2KB
shadowfox.css 2KB
tomorrow-night-eighties.css 2KB
moxer.css 2KB
ayu-mirage.css 2KB
lesser-dark.css 2KB
material.css 2KB
erlang-dark.css 2KB
oceanic-next.css 2KB
xq-light.css 2KB
ayu-dark.css 2KB
pastel-on-dark.css 2KB
xq-light.css 2KB
monokai.css 2KB
twilight.css 2KB
vibrant-ink.css 2KB
tomorrow-night-eighties.css 2KB
base16-light.css 2KB
base16-dark.css 2KB
mbo.css 2KB
nord.css 2KB
paraiso-light.css 2KB
paraiso-dark.css 2KB
3024-night.css 2KB
editormd.logo.css 2KB
dracula.css 2KB
erlang-dark.css 2KB
共 1922 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
轩*
- 粉丝: 18
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功