Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 14bffb9

Browse files
committed
首页文章排行榜
1 parent ad692b0 commit 14bffb9

File tree

2 files changed

+85
-6
lines changed

2 files changed

+85
-6
lines changed

src/logic/rank.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"fmt"
1212
"model"
1313
"time"
14+
"util"
1415

1516
"github.com/garyburd/redigo/redis"
1617
"github.com/polaris1119/logger"
@@ -24,6 +25,7 @@ var DefaultRank = RankLogic{}
2425

2526
func (self RankLogic) GenDayRank(objtype, objid, num int) {
2627
redisClient := nosql.NewRedisClient()
28+
defer redisClient.Close()
2729
key := self.getDayRankKey(objtype, times.Format("ymd"))
2830
err := redisClient.ZINCRBY(key, num, objid)
2931
if err != nil {
@@ -35,6 +37,7 @@ func (self RankLogic) GenDayRank(objtype, objid, num int) {
3537
// GenWeekRank 过去 7 天排行榜
3638
func (self RankLogic) GenWeekRank(objtype int) {
3739
redisClient := nosql.NewRedisClient()
40+
defer redisClient.Close()
3841
dest := self.getWeekRankKey(objtype)
3942

4043
keys := self.getMultiKey(objtype, 7)
@@ -48,6 +51,7 @@ func (self RankLogic) GenWeekRank(objtype int) {
4851
// GenMonthRank 过去 30 天排行榜
4952
func (self RankLogic) GenMonthRank(objtype int) {
5053
redisClient := nosql.NewRedisClient()
54+
defer redisClient.Close()
5155
dest := self.getMonthRankKey(objtype)
5256

5357
keys := self.getMultiKey(objtype, 30)
@@ -64,6 +68,7 @@ func (self RankLogic) FindDayRank(ctx context.Context, objtype int, ymd string,
6468
redisClient := nosql.NewRedisClient()
6569
key := self.getDayRankKey(objtype, ymd)
6670
resultSlice, err := redisClient.ZREVRANGE(key, 0, num, true)
71+
redisClient.Close()
6772
if err != nil {
6873
objLog.Errorln("FindDayRank ZREVRANGE error:", err)
6974
return nil
@@ -78,6 +83,7 @@ func (self RankLogic) FindWeekRank(ctx context.Context, objtype, num int) (resul
7883
redisClient := nosql.NewRedisClient()
7984
key := self.getWeekRankKey(objtype)
8085
resultSlice, err := redisClient.ZREVRANGE(key, 0, num, true)
86+
redisClient.Close()
8187
if err != nil {
8288
objLog.Errorln("FindWeekRank ZREVRANGE error:", err)
8389
return nil
@@ -92,6 +98,7 @@ func (self RankLogic) FindMonthRank(ctx context.Context, objtype, num int) (resu
9298
redisClient := nosql.NewRedisClient()
9399
key := self.getMonthRankKey(objtype)
94100
resultSlice, err := redisClient.ZREVRANGE(key, 0, num, true)
101+
redisClient.Close()
95102
if err != nil {
96103
objLog.Errorln("FindMonthRank ZREVRANGE error:", err)
97104
return nil
@@ -136,6 +143,8 @@ func (RankLogic) findModelsByRank(resultSlice []interface{}, objtype, num int) (
136143
articles := DefaultArticle.FindByIds(objids)
137144
for i, article := range articles {
138145
article.RankView = viewNums[i]
146+
article.Txt = util.Substring(article.Txt, 220, "...")
147+
article.AuthorTxt = util.Substring(article.AuthorTxt, 15, " 等")
139148
}
140149
result = articles
141150
case model.TypeProject:

template/index.html

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ <h3><a href="/resources">最新资源</a></h3>
5050

5151
<div class="row box_white article-list">
5252
<ul class="nav nav-tabs" role="tablist" id="article-nav">
53-
<li role="presentation" class="active"><a href="/">最新博文</a></li>
54-
<li role="presentation" class="rank" data-type="today"><a href="#">今日热门</a></li>
55-
<li role="presentation" class="rank" data-type="yesterday"><a href="#">昨日热门</a></li>
56-
<li role="presentation" class="rank" data-type="week"><a href="#">一周热门</a></li>
53+
<li role="presentation" class="active" data-type="newest"><a href="javascript:">最新博文</a></li>
54+
<li role="presentation" data-type="today"><a href="javascript:">今日热门</a></li>
55+
<li role="presentation" data-type="yesterday"><a href="javascript:">昨日热门</a></li>
56+
<li role="presentation" data-type="week"><a href="javascript:">一周热门</a></li>
5757
</ul>
58+
<div id="article-list">
5859
{{range .articles}}
5960
<article class="article box_white">
6061
<div class="row">
@@ -123,10 +124,13 @@ <h2><a href="/articles/{{.Id}}" target="_blank" title="{{.Title}}">
123124
<div class="row">暂无博文,<a class="btn btn-default btn-sm" href="/articles/new">撰写文章</a></div>
124125
</article>
125126
{{end}}
127+
126128
{{if (len .articles) gt 0}}
127129
{{$article := index .articles 0}}
128130
<div class="pull-right article-bottom"><a href="/articles?lastid={{add $article.Id -10}}" title="查看更多">查看更多&gt;&gt;</a></div>
129131
{{end}}
132+
133+
</div>
130134
</div>
131135
<div class="row box_white book-list">
132136
<div class="title">
@@ -321,12 +325,58 @@ <h3 class="title"><i class="glyphicon glyphicon-link"></i>&nbsp;友情链接</h3
321325

322326
</div>
323327
</div>
328+
329+
<script type="text/x-jsrender" id="article-tpl">
330+
<article class="article box_white">
331+
<div class="row">
332+
[%if cover%]
333+
<div class="pull-left">
334+
<a href="/articles/[%:id%]" target="_blank" title="[%:title%]">
335+
<img class="lazy" src="[%:cover%]" width="200" alt="[%:title%]" style="display: inline;">
336+
</a>
337+
</div>
338+
<div style="margin-left:220px;">
339+
[%else%]
340+
<div>
341+
[%/if%]
342+
<h2><a href="/articles/[%:id%]" target="_blank" title="[%:title%]">
343+
[%:title%]
344+
</a></h2>
345+
<p class="text">[%:txt%]<a href="/articles/[%:id%]" target="_blank" title="阅读全文">阅读全文</a></p>
346+
</div>
347+
</div>
348+
<div class="row">
349+
<div class="col-md-8 metatag">
350+
<i class="glyphicon glyphicon-th"></i>
351+
<span class="source" title="来源">[%:name%]</span>
352+
<i class="glyphicon glyphicon-calendar"></i>
353+
<span class="date" title="发布日期">[%:pub_date%]</span>
354+
<i class="glyphicon glyphicon-user"></i>
355+
<span class="author" title="作者">[%:author_txt%]</span>
356+
</div>
357+
<div class="col-md-4 metatag text-right">
358+
<span class="view" title="阅读数">
359+
<i class="glyphicon glyphicon-eye-open"></i>
360+
阅读:<span>[%:rank_view%]</span>
361+
</span>
362+
</div>
363+
</div>
364+
</article>
365+
</script>
366+
367+
<script type="text/x-jsrender" id="no-article-tpl">
368+
<article class="article box_white">
369+
<div class="row">该排行榜暂无博文</div>
370+
</article>
371+
</script>
372+
324373
{{end}}
325374
{{define "css"}}
326375
<link href="/static/css/index.css?v=0.4" rel="stylesheet"/>
327376
{{end}}
328377
{{define "js"}}
329378
<script type="text/javascript" src="/static/js/index.js?v=0.5"></script>
379+
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jsrender/0.9.84/jsrender.min.js"></script>
330380
<script type="text/javascript">
331381
// 需要加载的侧边栏
332382
SG.SIDE_BARS = [
@@ -339,20 +389,40 @@ <h3 class="title"><i class="glyphicon glyphicon-link"></i>&nbsp;友情链接</h3
339389
"/friend/links"
340390
];
341391

392+
$.views.settings.delimiters("[%", "%]");
393+
// $.views.settings.debugMode(true);
394+
342395
$(function() {
343-
$('#article-nav .rank').on('click', function(evt) {
396+
397+
var newestArticleHtml = '';
398+
399+
$('#article-nav li').on('click', function(evt) {
344400
evt.preventDefault();
345401

346402
$(this).parent().children().removeClass('active');
347403
$(this).addClass('active');
348404

349405
var type = $(this).data('type');
350406

407+
if (type != "newest" && newestArticleHtml == '') {
408+
newestArticleHtml = $('#article-list').html();
409+
} else {
410+
if (type == "newest") {
411+
$('#article-list').html(newestArticleHtml);
412+
return;
413+
}
414+
}
415+
351416
var params = {objtype: 1, limit: 10, rank_type: type}
352417
$.getJSON('/rank/view', params, function(result) {
353418
if (result.ok) {
354419
var data = result.data;
355-
console.log(data);
420+
if (data) {
421+
var rankArticlesHtml = $.templates('#article-tpl').render(data);
422+
$('#article-list').html(rankArticlesHtml);
423+
} else {
424+
$('#article-list').html($('#no-article-tpl').html());
425+
}
356426
}
357427
});
358428
});

0 commit comments

Comments
 (0)