1. 定时推送主页更新(百度搜索)
2. 主动推送文章更新(百度搜索)
3. 对象名修正
This commit is contained in:
ronger 2020-02-15 15:07:26 +08:00
parent 60346e8f96
commit 164e0376ed
3 changed files with 119 additions and 42 deletions

View File

@ -7,6 +7,7 @@ import com.rymcu.vertical.dto.ArticleTagDTO;
import com.rymcu.vertical.dto.Author;
import com.rymcu.vertical.entity.Article;
import com.rymcu.vertical.entity.ArticleContent;
import com.rymcu.vertical.entity.Tag;
import com.rymcu.vertical.entity.User;
import com.rymcu.vertical.mapper.ArticleMapper;
import com.rymcu.vertical.service.ArticleService;
@ -19,6 +20,7 @@ import org.apache.commons.text.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@ -40,10 +42,9 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
private TagService tagService;
@Resource
private UserService userService;
@Value("${reserved-words}")
private String reservedWords;
private static final String DOMAIN = "https://rymcu.com";
@Value("${resource.domain}")
private static String domain;
private static final int MAX_PREVIEW = 200;
@ -101,82 +102,92 @@ public class ArticleServiceImpl extends AbstractService<Article> implements Arti
String articleContent = article.getArticleContent();
String articleContentHtml = article.getArticleContentHtml();
User user = UserUtils.getWxCurrentUser();
boolean checkTags = checkTags(articleTags);
String reservedTag = checkTags(articleTags);
boolean notification = false;
if (checkTags) {
if (StringUtils.isNotBlank(reservedTag)) {
Integer roleWeights = userService.findRoleWeightsByUser(user.getIdUser());
if (roleWeights > 2) {
map.put("message", StringEscapeUtils.unescapeJava(reservedWords) + "标签为系统保留标签!");
map.put("message", StringEscapeUtils.unescapeJava(reservedTag) + "标签为系统保留标签!");
return map;
} else {
notification = true;
}
}
Article article1;
Article newArticle;
if(article.getIdArticle() == null || article.getIdArticle() == 0){
article1 = new Article();
article1.setArticleTitle(articleTitle);
article1.setArticleAuthorId(user.getIdUser());
article1.setArticleTags(articleTags);
article1.setCreatedTime(new Date());
article1.setUpdatedTime(article1.getCreatedTime());
articleMapper.insertSelective(article1);
article1.setArticlePermalink(DOMAIN + "/article/"+article1.getIdArticle());
article1.setArticleLink("/article/"+article1.getIdArticle());
articleMapper.insertArticleContent(article1.getIdArticle(),articleContent,articleContentHtml);
BaiDuUtils.sendSEOData(article1.getArticlePermalink());
newArticle = new Article();
newArticle.setArticleTitle(articleTitle);
newArticle.setArticleAuthorId(user.getIdUser());
newArticle.setArticleTags(articleTags);
newArticle.setCreatedTime(new Date());
newArticle.setUpdatedTime(newArticle.getCreatedTime());
articleMapper.insertSelective(newArticle);
newArticle.setArticlePermalink(domain + "/article/"+newArticle.getIdArticle());
newArticle.setArticleLink("/article/"+newArticle.getIdArticle());
articleMapper.insertArticleContent(newArticle.getIdArticle(),articleContent,articleContentHtml);
BaiDuUtils.sendSEOData(newArticle.getArticlePermalink());
} else {
article1 = articleMapper.selectByPrimaryKey(article.getIdArticle());
if(!user.getIdUser().equals(article1.getArticleAuthorId())){
newArticle = articleMapper.selectByPrimaryKey(article.getIdArticle());
if(!user.getIdUser().equals(newArticle.getArticleAuthorId())){
map.put("message","非法访问!");
return map;
}
article1.setArticleTitle(articleTitle);
article1.setArticleTags(articleTags);
newArticle.setArticleTitle(articleTitle);
newArticle.setArticleTags(articleTags);
if(StringUtils.isNotBlank(articleContentHtml)){
Integer length = articleContentHtml.length();
if(length > MAX_PREVIEW){
length = 200;
}
String articlePreviewContent = articleContentHtml.substring(0,length);
article1.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent));
newArticle.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent));
}
article1.setUpdatedTime(new Date());
articleMapper.updateArticleContent(article1.getIdArticle(),articleContent,articleContentHtml);
newArticle.setUpdatedTime(new Date());
articleMapper.updateArticleContent(newArticle.getIdArticle(),articleContent,articleContentHtml);
BaiDuUtils.updateSEOData(newArticle.getArticlePermalink());
}
if (notification) {
NotificationUtils.sendAnnouncement(article1.getIdArticle(), NotificationConstant.Article, article1.getArticleTitle());
NotificationUtils.sendAnnouncement(newArticle.getIdArticle(), NotificationConstant.Article, newArticle.getArticleTitle());
}
tagService.saveTagArticle(article1);
articleMapper.updateByPrimaryKeySelective(article1);
tagService.saveTagArticle(newArticle);
articleMapper.updateByPrimaryKeySelective(newArticle);
map.put("id", article1.getIdArticle());
map.put("id", newArticle.getIdArticle());
return map;
}
private boolean checkTags(String articleTags) {
if (StringUtils.isNotBlank(reservedWords) && StringUtils.isNotBlank(articleTags)) {
String[] words = StringEscapeUtils.unescapeJava(reservedWords).split(",");
String[] tags = articleTags.split(",");
for(String word: words) {
if (StringUtils.isBlank(word)) {
continue;
}
for (String tag: tags) {
if (StringUtils.isBlank(tag)) {
continue;
}
if (tag.equals(word)) {
return true;
}
}
private String checkTags(String articleTags) {
// 判断文章是否有标签
if(StringUtils.isBlank(articleTags)){
return "";
}
// 判断是否存在系统配置的保留标签词
Condition condition = new Condition(Tag.class);
condition.createCriteria().andEqualTo("tagReservation", "1");
List<Tag> tags = tagService.findByCondition(condition);
if (tags.isEmpty()) {
return "";
} else {
return false;
String[] articleTagArr = articleTags.split(",");
for (Tag tag : tags) {
if (StringUtils.isBlank(tag.getTagTitle())) {
continue;
}
return false;
for (String articleTag: articleTagArr) {
if (StringUtils.isBlank(articleTag)) {
continue;
}
if (articleTag.equals(tag.getTagTitle())) {
return tag.getTagTitle();
}
}
}
}
return "";
}
@Override

View File

@ -0,0 +1,24 @@
package com.rymcu.vertical.task;
import com.rymcu.vertical.util.BaiDuUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class BaiduCronTask {
@Value("${resource.domain}")
private String domain;
/**
* 定时推送首页更新
* */
@Scheduled(cron = "0 0 10,14,18 * * ?")
public void pushHome() {
BaiDuUtils.updateSEOData(domain);
}
}

View File

@ -35,6 +35,48 @@ public class BaiDuUtils {
},executor);
}
public static void updateSEOData(String permalink) {
if (StringUtils.isBlank(permalink) || StringUtils.isBlank(token)) {
return;
}
ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
CompletableFuture.supplyAsync(()-> {
try {
HttpResponse response = HttpRequest.post("http://data.zz.baidu.com/update?site=" + site + "&token=" + token).
header("User-Agent", "curl/7.12.1").
header("Host", "data.zz.baidu.com").
header("Content-Type", "text/plain").
header("Connection", "close").body(permalink.getBytes(), "text/plain").timeout(30000).send();
response.charset("UTF-8");
System.out.println(response.bodyText());
} catch (Exception e){
e.printStackTrace();
}
return 0;
},executor);
}
public static void deleteSEOData(String permalink) {
if (StringUtils.isBlank(permalink) || StringUtils.isBlank(token)) {
return;
}
ExecutorService executor= new ThreadPoolExecutor(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
CompletableFuture.supplyAsync(()-> {
try {
HttpResponse response = HttpRequest.post("http://data.zz.baidu.com/del?site=" + site + "&token=" + token).
header("User-Agent", "curl/7.12.1").
header("Host", "data.zz.baidu.com").
header("Content-Type", "text/plain").
header("Connection", "close").body(permalink.getBytes(), "text/plain").timeout(30000).send();
response.charset("UTF-8");
System.out.println(response.bodyText());
} catch (Exception e){
e.printStackTrace();
}
return 0;
},executor);
}
public static void main(String agrs[]){
sendSEOData("https://rymcu.com/article/31");
}