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

Skip to content

Commit 6355c96

Browse files
committed
排行榜
1 parent 3ec42a3 commit 6355c96

File tree

15 files changed

+162
-10
lines changed

15 files changed

+162
-10
lines changed

src/global/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type app struct {
6565
locker sync.Mutex
6666
}
6767

68-
var App = app{}
68+
var App = &app{}
6969

7070
var showVersion = flag.Bool("version", false, "Print version of this binary")
7171

src/http/controller/sidebar.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import (
1010
"logic"
1111
"model"
1212
"strconv"
13+
"time"
1314

1415
"github.com/labstack/echo"
1516
"github.com/polaris1119/goutils"
1617
"github.com/polaris1119/slices"
18+
"github.com/polaris1119/times"
1719
)
1820

1921
// 侧边栏的内容通过异步请求获取
@@ -33,6 +35,7 @@ func (self SidebarController) RegisterRoute(g *echo.Group) {
3335
g.GET("/users/active", self.ActiveUser)
3436
g.GET("/users/newest", self.NewestUser)
3537
g.GET("/friend/links", self.FriendLinks)
38+
g.GET("/rank/view", self.ViewRank)
3639
}
3740

3841
// RecentReading 技术晨读
@@ -147,3 +150,25 @@ func (SidebarController) FriendLinks(ctx echo.Context) error {
147150
friendLinks := logic.DefaultFriendLink.FindAll(ctx)
148151
return success(ctx, friendLinks)
149152
}
153+
154+
// ViewRank 阅读排行榜
155+
func (SidebarController) ViewRank(ctx echo.Context) error {
156+
objtype := goutils.MustInt(ctx.QueryParam("objtype"))
157+
rankType := ctx.QueryParam("rank_type")
158+
limit := goutils.MustInt(ctx.QueryParam("limit"), 10)
159+
160+
var result interface{}
161+
switch rankType {
162+
case "today":
163+
result = logic.DefaultRank.FindDayRank(ctx, objtype, times.Format("ymd"), limit)
164+
case "yesterday":
165+
yesterday := time.Now().Add(-1 * 24 * time.Hour)
166+
result = logic.DefaultRank.FindDayRank(ctx, objtype, times.Format("ymd", yesterday), limit)
167+
case "week":
168+
result = logic.DefaultRank.FindWeekRank(ctx, objtype, limit)
169+
case "month":
170+
result = logic.DefaultRank.FindMonthRank(ctx, objtype, limit)
171+
}
172+
173+
return success(ctx, result)
174+
}

src/logic/ad.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ func (AdLogic) FindAll(ctx context.Context, path string) map[string]*model.Adver
3434
adIdSet.Add(pageAd.AdId)
3535
}
3636

37+
if adIdSet.IsEmpty() {
38+
return nil
39+
}
40+
3741
adMap := make(map[int]*model.Advertisement)
3842
err = MasterDB.In("id", set.IntSlice(adIdSet)).Find(&adMap)
3943
if err != nil {

src/logic/rank.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
package logic
88

99
import (
10+
"context"
1011
"fmt"
12+
"model"
1113
"time"
1214

1315
"github.com/polaris1119/logger"
@@ -55,6 +57,92 @@ func (self RankLogic) GenMonthRank(objtype int) {
5557
}
5658
}
5759

60+
func (self RankLogic) FindDayRank(ctx context.Context, objtype int, ymd string, num int) (result interface{}) {
61+
objLog := GetLogger(ctx)
62+
63+
redisClient := nosql.NewRedisClient()
64+
key := self.getDayRankKey(objtype, ymd)
65+
resultSlice, err := redisClient.ZREVRANGE(key, 0, num, true)
66+
if err != nil {
67+
objLog.Errorln("FindDayRank ZREVRANGE error:", err)
68+
return nil
69+
}
70+
71+
return self.findModelsByRank(resultSlice, objtype, num)
72+
}
73+
74+
func (self RankLogic) FindWeekRank(ctx context.Context, objtype, num int) (result interface{}) {
75+
objLog := GetLogger(ctx)
76+
77+
redisClient := nosql.NewRedisClient()
78+
key := self.getWeekRankKey(objtype)
79+
resultSlice, err := redisClient.ZREVRANGE(key, 0, num, true)
80+
if err != nil {
81+
objLog.Errorln("FindWeekRank ZREVRANGE error:", err)
82+
return nil
83+
}
84+
85+
return self.findModelsByRank(resultSlice, objtype, num)
86+
}
87+
88+
func (self RankLogic) FindMonthRank(ctx context.Context, objtype, num int) (result interface{}) {
89+
objLog := GetLogger(ctx)
90+
91+
redisClient := nosql.NewRedisClient()
92+
key := self.getMonthRankKey(objtype)
93+
resultSlice, err := redisClient.ZREVRANGE(key, 0, num, true)
94+
if err != nil {
95+
objLog.Errorln("FindMonthRank ZREVRANGE error:", err)
96+
return nil
97+
}
98+
99+
return self.findModelsByRank(resultSlice, objtype, num)
100+
}
101+
102+
func (RankLogic) findModelsByRank(resultSlice []interface{}, objtype, num int) (result interface{}) {
103+
objids := make([]int, 0, num)
104+
viewNums := make([]int, 0, num)
105+
for i, length := 0, len(resultSlice); i < length; i += 2 {
106+
objids = append(objids, resultSlice[i].(int))
107+
viewNums = append(viewNums, resultSlice[i+1].(int))
108+
}
109+
110+
switch objtype {
111+
case model.TypeTopic:
112+
topics := DefaultTopic.FindByTids(objids)
113+
for i, topic := range topics {
114+
topic.RankView = viewNums[i]
115+
}
116+
result = topics
117+
case model.TypeResource:
118+
resources := DefaultResource.FindByIds(objids)
119+
for i, resource := range resources {
120+
resource.RankView = viewNums[i]
121+
}
122+
result = resources
123+
case model.TypeArticle:
124+
articles := DefaultArticle.FindByIds(objids)
125+
for i, article := range articles {
126+
article.RankView = viewNums[i]
127+
}
128+
result = articles
129+
case model.TypeProject:
130+
projects := DefaultProject.FindByIds(objids)
131+
for i, project := range projects {
132+
project.RankView = viewNums[i]
133+
}
134+
result = projects
135+
case model.TypeBook:
136+
books := DefaultGoBook.FindByIds(objids)
137+
for i, book := range books {
138+
book.RankView = viewNums[i]
139+
}
140+
result = books
141+
}
142+
143+
return
144+
}
145+
58146
func (self RankLogic) getMultiKey(objtype, num int) []string {
59147
today := time.Now()
60148

src/logic/sitemap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func GenSitemap() {
195195
for {
196196
sitemapFile := "sitemap_book_" + strconv.Itoa(large) + ".xml"
197197

198-
err = MasterDB.Where("id BETWEEN ? AND ?", little, large).Select("id,mtime").Find(&books)
198+
err = MasterDB.Where("id BETWEEN ? AND ?", little, large).Select("id,updated_at").Find(&books)
199199
little, large = large+1, little+step
200200

201201
if err != nil {

src/model/article.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ type Article struct {
4949
Mtime OftenTime `json:"mtime" xorm:"<-"`
5050

5151
IsSelf bool `json:"is_self" xorm:"-"`
52+
// 排行榜阅读量
53+
RankView int `json:"rank_view" xorm:"-"`
5254
}
5355

5456
func (this *Article) AfterSet(name string, cell xorm.Cell) {

src/model/book.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ type Book struct {
3232
Likenum int `json:"likenum"`
3333
CreatedAt OftenTime `json:"created_at" xorm:"created"`
3434
UpdatedAt OftenTime `json:"updated_at" xorm:"<-"`
35+
36+
// 排行榜阅读量
37+
RankView int `json:"rank_view" xorm:"-"`
3538
}

src/model/openproject.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ type OpenProject struct {
4343
Status int `json:"status"`
4444
Ctime OftenTime `json:"ctime,omitempty" xorm:"created"`
4545
Mtime time.Time `json:"mtime,omitempty" xorm:"<-"`
46+
47+
// 排行榜阅读量
48+
RankView int `json:"rank_view" xorm:"-"`
4649
}
4750

4851
func (this *OpenProject) AfterSet(name string, cell xorm.Cell) {

src/model/resource.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ type Resource struct {
2525
CatName string `json:"-" xorm:"-"`
2626
Ctime OftenTime `json:"ctime" xorm:"created"`
2727
Mtime time.Time `json:"mtime" xorm:"<-"`
28+
29+
// 排行榜阅读量
30+
RankView int `json:"rank_view" xorm:"-"`
2831
}
2932

3033
// 资源扩展(计数)信息

src/model/topic.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type Topic struct {
3232

3333
// 为了方便,加上Node(节点名称,数据表没有)
3434
Node string `xorm:"-"`
35+
// 排行榜阅读量
36+
RankView int `json:"rank_view" xorm:"-"`
3537
}
3638

3739
func (*Topic) TableName() string {

0 commit comments

Comments
 (0)