Skip to content

Commit

Permalink
🦄 添加最新评论,最新文章函数
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Feb 24, 2017
1 parent f6b1ed1 commit c48985b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/tale/controller/InstallController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.blade.mvc.view.RestResponse;
import com.tale.dto.JdbcConf;
import com.tale.exception.TipException;
import com.tale.ext.Commons;
import com.tale.init.TaleConst;
import com.tale.init.TaleJdbc;
import com.tale.model.Users;
Expand Down Expand Up @@ -103,8 +104,9 @@ public RestResponse doInstall(@QueryParam String site_title, @QueryParam String
Config config = new Config();
config.addAll(optionsService.getOptions());
TaleConst.OPTIONS = config;

TaleConst.INSTALL = true;
Commons.setSiteService(siteService);

} catch (Exception e) {
String msg = "安装失败";
if (e instanceof TipException) {
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/tale/ext/Commons.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import com.blade.kit.Tools;
import com.tale.controller.BaseController;
import com.tale.init.TaleConst;
import com.tale.model.Comments;
import com.tale.model.Contents;
import com.tale.service.SiteService;
import com.tale.utils.TaleUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -22,6 +26,14 @@
*/
public final class Commons {

private static SiteService siteService;

private static final List EMPTY = new ArrayList(0);

public static void setSiteService(SiteService ss) {
siteService = ss;
}

/**
* 判断分页中是否有数据
*
Expand Down Expand Up @@ -266,6 +278,32 @@ public static String show_thumb(Contents contents) {
return "/static/user/img/rand/" + cid % 12 + ".jpg";
}

/**
* 最新文章
*
* @param limit
* @return
*/
public static List<Contents> recent_articles(int limit) {
if (null == siteService) {
return EMPTY;
}
return siteService.recentContents(limit);
}

/**
* 最新评论
*
* @param limit
* @return
*/
public static List<Comments> recent_comments(int limit) {
if (null == siteService) {
return EMPTY;
}
return siteService.recentComments(limit);
}

/**
* 获取文章第一张图片
*
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/tale/init/WebContext.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tale.init;

import com.blade.Blade;
import com.blade.config.BConfig;
import com.blade.context.WebContextListener;
import com.blade.ioc.BeanProcessor;
Expand All @@ -11,6 +12,7 @@
import com.tale.ext.Commons;
import com.tale.ext.JetTag;
import com.tale.service.OptionsService;
import com.tale.service.SiteService;
import jetbrick.template.resolver.GlobalResolver;

import javax.servlet.ServletContext;
Expand Down Expand Up @@ -40,6 +42,7 @@ public void init(BConfig bConfig, ServletContext sec) {
if (dbIsOk) {
TaleConst.OPTIONS.addAll(optionsService.getOptions());
TaleConst.INSTALL = true;
Commons.setSiteService(Blade.$().ioc().getBean(SiteService.class));
}
TaleConst.BCONF = bConfig.config();
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ <h4 class="panel-title">最新文章</h4>
<li class="list-group-item">
<span class="badge badge-primary"
title="${article.comments_num}条评论">${article.comments_num}</span>
<a href="">${article.title}</a>
<a target="_blank" href="${site_url('/article/')}${article.cid}">${article.title}</a>
</li>
#end
</ul>
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/templates/themes/default/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@

<div class="meta-item meta-posts">
<h3 class="meta-title">最新文章</h3>
#for(post : recent_articles(8))
<li>
<a href="${permalink(post)}">${post.title}</a>
</li>
#end
</div>

<div class="meta-item meta-comments">
<h3 class="meta-title">最新评论</h3>
#for(comm : recent_comments(8))
<li>
<a href="${site_url('/article/')}${comm.cid}#comment-${comm.coid}">${comm.author}:${comm.content}</a>
</li>
#end
</div>
</div>
</div>
Expand Down

0 comments on commit c48985b

Please sign in to comment.