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

Skip to content

Commit ad3d521

Browse files
committed
Merge branch 'feature_comment'
2 parents c74b352 + bdea25a commit ad3d521

File tree

19 files changed

+1301
-374
lines changed

19 files changed

+1301
-374
lines changed

src/db/conn.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ func init() {
3535
if err = initEngine(); err != nil {
3636
panic(err)
3737
}
38+
39+
// 测试数据库连接是否 OK
40+
if err = MasterDB.Ping(); err != nil {
41+
panic(err)
42+
}
3843
}
3944

4045
var (
@@ -83,9 +88,7 @@ func TestDB() error {
8388
}
8489

8590
// 初始化 MasterDB
86-
Init()
87-
88-
return nil
91+
return Init()
8992
}
9093

9194
func Init() error {

src/global/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
)
3737

3838
const (
39-
DefaultCDNHttp = "http://studygolang.qiniudn.com/"
39+
DefaultCDNHttp = "http://test.static.studygolang.com/"
4040
DefaultCDNHttps = "https://static.studygolang.com/"
4141
)
4242

src/http/controller/comment.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,36 @@ func (CommentController) Modify(ctx echo.Context) error {
9191
func (CommentController) CommentList(ctx echo.Context) error {
9292
objid := goutils.MustInt(ctx.QueryParam("objid"))
9393
objtype := goutils.MustInt(ctx.QueryParam("objtype"))
94+
p := goutils.MustInt(ctx.QueryParam("p"))
9495

95-
commentList, err := logic.DefaultComment.FindObjectComments(ctx, objid, objtype)
96+
commentList, replyComments, pageNum, err := logic.DefaultComment.FindObjectComments(ctx, objid, objtype, p)
9697
if err != nil {
9798
return fail(ctx, 1, "服务器内部错误")
9899
}
99100

100101
uids := slices.StructsIntSlice(commentList, "Uid")
102+
if len(replyComments) > 0 {
103+
replyUids := slices.StructsIntSlice(replyComments, "Uid")
104+
uids = append(uids, replyUids...)
105+
}
101106
users := logic.DefaultUser.FindUserInfos(ctx, uids)
102107

103108
result := map[string]interface{}{
104109
"comments": commentList,
110+
"page_num": pageNum,
105111
}
106112

107113
// json encode 不支持 map[int]...
108114
for uid, user := range users {
109115
result[strconv.Itoa(uid)] = user
110116
}
111117

118+
replyMap := make(map[string]interface{})
119+
for _, comment := range replyComments {
120+
replyMap[strconv.Itoa(comment.Floor)] = comment
121+
}
122+
result["reply_comments"] = replyMap
123+
112124
return success(ctx, result)
113125
}
114126

src/http/http.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"global"
1313
"html/template"
1414
"logic"
15+
"math"
1516
"math/rand"
1617
"model"
1718
"net/http"
@@ -128,6 +129,9 @@ var funcMap = template.FuncMap{
128129

129130
return num1 % num2
130131
},
132+
"divide": func(num1, num2 int) int {
133+
return int(math.Ceil(float64(num1) / float64(num2)))
134+
},
131135
"explode": func(s, sep string) []string {
132136
return strings.Split(s, sep)
133137
},
@@ -175,6 +179,19 @@ var funcMap = template.FuncMap{
175179

176180
return uri
177181
},
182+
"genList": func(n int, steps ...int) []int {
183+
step := 1
184+
if len(steps) > 0 {
185+
step = steps[0]
186+
}
187+
num := int(math.Ceil(float64(n) / float64(step)))
188+
nums := make([]int, num)
189+
for i := 0; i < num; i++ {
190+
nums[i] = i + 1
191+
}
192+
193+
return nums
194+
},
178195
}
179196

180197
func tplInclude(file string, dot map[string]interface{}) template.HTML {
@@ -380,6 +397,9 @@ func executeTpl(ctx echo.Context, tpl *template.Template, data map[string]interf
380397

381398
data["setting"] = logic.WebsiteSetting
382399

400+
// 评论每页显示多少个
401+
data["cmt_per_num"] = logic.CommentPerNum
402+
383403
// 记录处理时间
384404
data["resp_time"] = time.Since(ctx.Get("req_start_time").(time.Time))
385405

src/logic/comment.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package logic
99
import (
1010
"fmt"
1111
"html/template"
12+
"math"
1213
"model"
1314
"net/url"
1415
"regexp"
@@ -63,19 +64,43 @@ func (self CommentLogic) FindObjComments(ctx context.Context, objid, objtype int
6364
return
6465
}
6566

67+
const CommentPerNum = 50
68+
6669
// FindObjectComments 获得某个对象的所有评论(新版)
67-
// TODO:分页暂不做
68-
func (self CommentLogic) FindObjectComments(ctx context.Context, objid, objtype int) (commentList []*model.Comment, err error) {
70+
func (self CommentLogic) FindObjectComments(ctx context.Context, objid, objtype, p int) (commentList []*model.Comment, replyComments []*model.Comment, pageNum int, err error) {
6971
objLog := GetLogger(ctx)
7072

73+
total, err := MasterDB.Where("objid=? AND objtype=?", objid, objtype).Count(new(model.Comment))
74+
if err != nil {
75+
objLog.Errorln("comment logic FindObjectComments count Error:", err)
76+
return
77+
}
78+
79+
pageNum = int(math.Ceil(float64(total) / CommentPerNum))
80+
if p == 0 {
81+
p = pageNum
82+
}
83+
7184
commentList = make([]*model.Comment, 0)
72-
err = MasterDB.Where("objid=? AND objtype=?", objid, objtype).Asc("cid").Find(&commentList)
85+
err = MasterDB.Where("objid=? AND objtype=?", objid, objtype).Asc("cid").
86+
Limit(CommentPerNum, (p-1)*CommentPerNum).
87+
Find(&commentList)
7388
if err != nil {
7489
objLog.Errorln("comment logic FindObjectComments Error:", err)
7590
}
7691

92+
floors := make([]interface{}, 0, len(commentList))
7793
for _, comment := range commentList {
7894
self.decodeCmtContentForShow(ctx, comment, true)
95+
96+
if comment.ReplyFloor > 0 {
97+
floors = append(floors, comment.ReplyFloor)
98+
}
99+
}
100+
101+
if len(floors) > 0 {
102+
replyComments = make([]*model.Comment, 0)
103+
err = MasterDB.Where("objid=? AND objtype=?", objid, objtype).In("floor", floors...).Find(&replyComments)
79104
}
80105

81106
return

0 commit comments

Comments
 (0)