Merge pull request #1 from rymcu/dev

缺陷代码修复
This commit is contained in:
ronger 2020-02-15 15:59:39 +08:00 committed by GitHub
commit 7704cf0a1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 177 additions and 57 deletions

View File

@ -8,12 +8,9 @@ import com.rymcu.vertical.service.ArticleService;
import com.rymcu.vertical.service.VisitService; import com.rymcu.vertical.service.VisitService;
import com.rymcu.vertical.util.UserUtils; import com.rymcu.vertical.util.UserUtils;
import com.rymcu.vertical.util.Utils; import com.rymcu.vertical.util.Utils;
import com.rymcu.vertical.web.api.exception.BaseApiException;
import com.rymcu.vertical.web.api.exception.ErrorCode;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@ -42,4 +42,6 @@ public class Tag implements Serializable,Cloneable {
private Date createdTime; private Date createdTime;
/** 更新时间 */ /** 更新时间 */
private Date updatedTime; private Date updatedTime;
/** 保留标签 */
private String tagReservation;
} }

View File

@ -15,5 +15,5 @@ public interface TagMapper extends Mapper<Tag> {
Integer deleteUnusedTag(); Integer deleteUnusedTag();
Integer update(@Param("idTag") Integer idTag, @Param("tagUri") String tagUri, @Param("tagIconPath") String tagIconPath, @Param("tagStatus") String tagStatus, @Param("tagDescription") String tagDescription); Integer update(@Param("idTag") Integer idTag, @Param("tagUri") String tagUri, @Param("tagIconPath") String tagIconPath, @Param("tagStatus") String tagStatus, @Param("tagDescription") String tagDescription, @Param("tagReservation") String tagReservation);
} }

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

@ -11,12 +11,14 @@ import com.rymcu.vertical.web.api.exception.BaseApiException;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
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 java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -87,15 +89,35 @@ public class TagServiceImpl extends AbstractService<Tag> implements TagService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Map saveTag(Tag tag) { public Map saveTag(Tag tag) {
Integer result = 0; Integer result = 0;
Map map = new HashMap(1);
if (tag.getIdTag() == null) { if (tag.getIdTag() == null) {
tag.setCreatedTime(new Date()); if (StringUtils.isBlank(tag.getTagTitle())) {
tag.setUpdatedTime(tag.getCreatedTime()); map.put("message","标签名不能为空!");
result = tagMapper.insertSelective(tag); return map;
} else {
Condition tagCondition = new Condition(Tag.class);
tagCondition.createCriteria().andCondition("tag_title =", tag.getTagTitle());
List<Tag> tags = tagMapper.selectByCondition(tagCondition);
if (!tags.isEmpty()) {
map.put("message","标签 '" + tag.getTagTitle() + "' 已存在!");
return map;
}
}
Tag newTag = new Tag();
newTag.setTagTitle(tag.getTagTitle());
newTag.setTagUri(tag.getTagUri());
newTag.setTagIconPath(tag.getTagIconPath());
newTag.setTagStatus(tag.getTagStatus());
newTag.setTagDescription(tag.getTagDescription());
newTag.setTagReservation(tag.getTagReservation());
newTag.setCreatedTime(new Date());
newTag.setUpdatedTime(tag.getCreatedTime());
result = tagMapper.insertSelective(newTag);
} else { } else {
tag.setUpdatedTime(new Date()); tag.setUpdatedTime(new Date());
result = tagMapper.update(tag.getIdTag(),tag.getTagUri(),tag.getTagIconPath(),tag.getTagStatus(),tag.getTagDescription()); result = tagMapper.update(tag.getIdTag(),tag.getTagUri(),tag.getTagIconPath(),tag.getTagStatus(),tag.getTagDescription(),tag.getTagReservation());
} }
Map map = new HashMap(1);
if (result == 0) { if (result == 0) {
map.put("message","操作失败!"); map.put("message","操作失败!");
} else { } else {

View File

@ -13,6 +13,7 @@ import com.rymcu.vertical.service.TopicService;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
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 java.util.Date; import java.util.Date;
@ -59,17 +60,37 @@ public class TopicServiceImpl extends AbstractService<Topic> implements TopicSer
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Map saveTopic(Topic topic) { public Map saveTopic(Topic topic) {
Integer result = 0; Integer result = 0;
Map map = new HashMap(1);
if (topic.getIdTopic() == null) { if (topic.getIdTopic() == null) {
topic.setCreatedTime(new Date()); if (StringUtils.isBlank(topic.getTopicTitle())) {
topic.setUpdatedTime(topic.getCreatedTime()); map.put("message","标签名不能为空!");
result = topicMapper.insertSelective(topic); return map;
} else {
Condition topicCondition = new Condition(Topic.class);
topicCondition.createCriteria().andCondition("topic_title =", topic.getTopicTitle());
List<Topic> topics = topicMapper.selectByCondition(topicCondition);
if (!topics.isEmpty()) {
map.put("message","专题 '" + topic.getTopicTitle() + "' 已存在!");
return map;
}
}
Topic newTopic = new Topic();
newTopic.setTopicTitle(topic.getTopicTitle());
newTopic.setTopicUri(topic.getTopicUri());
newTopic.setTopicIconPath(topic.getTopicIconPath());
newTopic.setTopicNva(topic.getTopicNva());
newTopic.setTopicStatus(topic.getTopicStatus());
newTopic.setTopicSort(topic.getTopicSort());
newTopic.setTopicDescription(topic.getTopicDescription());
newTopic.setCreatedTime(new Date());
newTopic.setUpdatedTime(topic.getCreatedTime());
result = topicMapper.insertSelective(newTopic);
} else { } else {
topic.setCreatedTime(new Date()); topic.setCreatedTime(new Date());
result = topicMapper.update(topic.getIdTopic(),topic.getTopicTitle(),topic.getTopicUri() result = topicMapper.update(topic.getIdTopic(),topic.getTopicTitle(),topic.getTopicUri()
,topic.getTopicIconPath(),topic.getTopicNva(),topic.getTopicStatus() ,topic.getTopicIconPath(),topic.getTopicNva(),topic.getTopicStatus()
,topic.getTopicSort(),topic.getTopicDescription()); ,topic.getTopicSort(),topic.getTopicDescription());
} }
Map map = new HashMap(1);
if (result == 0) { if (result == 0) {
map.put("message","操作失败!"); map.put("message","操作失败!");
} else { } else {

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");
} }

View File

@ -13,6 +13,7 @@
<id column="tag_view_count" property="tagViewCount"/> <id column="tag_view_count" property="tagViewCount"/>
<id column="tag_article_count" property="tagArticleCount"/> <id column="tag_article_count" property="tagArticleCount"/>
<id column="tag_ad" property="tagAd"/> <id column="tag_ad" property="tagAd"/>
<id column="tag_reservation" property="tagReservation"/>
<id column="tag_show_side_ad" property="tagShowSideAd"/> <id column="tag_show_side_ad" property="tagShowSideAd"/>
<id column="created_time" property="createdTime"/> <id column="created_time" property="createdTime"/>
<id column="updated_time" property="updatedTime"/> <id column="updated_time" property="updatedTime"/>
@ -24,10 +25,10 @@
insert into vertical_user_tag (id_tag,id_user,type,created_time,updated_time) values (#{idTag},#{idUser},#{type},sysdate(),sysdate()) insert into vertical_user_tag (id_tag,id_user,type,created_time,updated_time) values (#{idTag},#{idUser},#{type},sysdate(),sysdate())
</insert> </insert>
<update id="update"> <update id="update">
update vertical_tag set tag_uri = #{tagUri}, tag_icon_path = #{tagIconPath}, tag_status = #{tagStatus}, tag_description = #{tagDescription} where id = #{idTag} update vertical_tag set tag_uri = #{tagUri}, tag_icon_path = #{tagIconPath}, tag_status = #{tagStatus}, tag_description = #{tagDescription}, tag_reservation = #{tagReservation} where id = #{idTag}
</update> </update>
<delete id="deleteUnusedTag"> <delete id="deleteUnusedTag">
delete from vertical_tag where id not in (select * from (select id_tag from vertical_tag_article vta left join vertical_article va on vta.id_article = va.id where va.id is not null) tmp); delete from vertical_tag where tag_reservation = 0 and id not in (select * from (select id_tag from vertical_tag_article vta left join vertical_article va on vta.id_article = va.id where va.id is not null) tmp);
</delete> </delete>
<select id="selectCountTagArticleById" resultType="java.lang.Integer"> <select id="selectCountTagArticleById" resultType="java.lang.Integer">
select count(*) from vertical_tag_article where id_tag = #{idTag} and id_article = #{idArticle} select count(*) from vertical_tag_article where id_tag = #{idTag} and id_article = #{idArticle}