🎨 适配主题

pull/73/head
ruibaby 2018-12-31 12:43:46 +08:00
parent 0b4f614073
commit 0f8759048e
6 changed files with 106 additions and 0 deletions

View File

@ -1,5 +1,7 @@
package cc.ryanc.halo.config;
import cc.ryanc.halo.model.method.RandomMethod;
import cc.ryanc.halo.model.method.RecentPostsMethod;
import cc.ryanc.halo.model.tag.ArticleTagDirective;
import cc.ryanc.halo.model.tag.CommonTagDirective;
import cc.ryanc.halo.service.OptionsService;
@ -38,6 +40,12 @@ public class FreeMarkerAutoConfiguration {
@Autowired
private ArticleTagDirective articleTagDirective;
@Autowired
private RandomMethod randomMethod;
@Autowired
private RecentPostsMethod recentPostsMethod;
@PostConstruct
public void setSharedVariable() {
try {
@ -46,6 +54,8 @@ public class FreeMarkerAutoConfiguration {
configuration.setSharedVariable("articleTag", articleTagDirective);
configuration.setSharedVariable("options", optionsService.findAllOptions());
configuration.setSharedVariable("user", userService.findUser());
configuration.setSharedVariable("randomMethod", randomMethod);
configuration.setSharedVariable("recentPostsMethod", recentPostsMethod);
} catch (TemplateModelException e) {
log.error("Custom tags failed to load{}", e.getMessage());
}

View File

@ -0,0 +1,33 @@
package cc.ryanc.halo.model.method;
import cn.hutool.core.util.RandomUtil;
import freemarker.template.SimpleNumber;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author : RYAN0UP
* @date : 2018/12/21
*/
@Component
public class RandomMethod implements TemplateMethodModelEx {
/**
*
*
* @param arguments
* @return Object
* @throws TemplateModelException TemplateModelException
*/
@Override
public Object exec(List arguments) throws TemplateModelException {
SimpleNumber argOne = (SimpleNumber) arguments.get(0);
SimpleNumber argTwo = (SimpleNumber) arguments.get(1);
int start = argOne.getAsNumber().intValue();
int end = argTwo.getAsNumber().intValue();
return RandomUtil.randomInt(start, end);
}
}

View File

@ -0,0 +1,35 @@
package cc.ryanc.halo.model.method;
import cc.ryanc.halo.service.PostService;
import freemarker.template.SimpleNumber;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author : RYAN0UP
* @date : 2018/12/31
*/
@Component
public class RecentPostsMethod implements TemplateMethodModelEx {
@Autowired
private PostService postService;
/**
*
*
* @param arguments
* @return Object
* @throws TemplateModelException TemplateModelException
*/
@Override
public Object exec(List arguments) throws TemplateModelException {
SimpleNumber argOne = (SimpleNumber) arguments.get(0);
int limit = argOne.getAsNumber().intValue();
return postService.getRecentPosts(limit);
}
}

View File

@ -222,4 +222,13 @@ public interface PostRepository extends JpaRepository<Post, Long> {
* @return
*/
Integer countAllByPostStatusAndPostType(Integer status, String postType);
/**
*
*
* @param limit
* @return List
*/
@Query(value = "SELECT * FROM HALO_POST WHERE POST_STATUS = 0 AND POST_TYPE = 'post' ORDER BY POST_DATE DESC LIMIT :limit",nativeQuery = true)
List<Post> getPostsByLimit(@Param(value = "limit") int limit);
}

View File

@ -277,4 +277,12 @@ public interface PostService {
* @return Post Post
*/
Post buildCategoriesAndTags(Post post, List<String> cateList, @RequestParam("tagList") String tagList);
/**
*
*
* @param limit
* @return List
*/
List<Post> getRecentPosts(int limit);
}

View File

@ -483,4 +483,15 @@ public class PostServiceImpl implements PostService {
}
return post;
}
/**
*
*
* @param limit
* @return List
*/
@Override
public List<Post> getRecentPosts(int limit) {
return postRepository.getPostsByLimit(limit);
}
}