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