mirror of https://github.com/halo-dev/halo
🎨 适配主题
parent
0b4f614073
commit
0f8759048e
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue