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

Skip to content

Commit 5e76c72

Browse files
committed
喜欢改进
1 parent 3bad3b3 commit 5e76c72

File tree

14 files changed

+95
-21
lines changed

14 files changed

+95
-21
lines changed

config/db.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ CREATE TABLE IF NOT EXISTS `comments` (
9494
`content` text NOT NULL,
9595
`uid` int unsigned NOT NULL COMMENT '回复者',
9696
`floor` int unsigned NOT NULL COMMENT '第几楼',
97+
`likenum` int unsigned NOT NULL COMMENT '喜欢数',
9798
`flag` tinyint NOT NULL DEFAULT 0 COMMENT '审核标识,0-未审核;1-已审核;2-审核删除;3-用户自己删除',
9899
`ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
99100
PRIMARY KEY (`cid`),
@@ -614,6 +615,7 @@ CREATE TABLE IF NOT EXISTS `feed` (
614615
`lastreplytime` timestamp NOT NULL DEFAULT '2013-03-15 14:38:09' COMMENT '最后回复时间',
615616
`tags` varchar(63) NOT NULL DEFAULT '' COMMENT 'tag,逗号分隔',
616617
`cmtnum` int unsigned NOT NULL DEFAULT 0 COMMENT '评论数',
618+
`likenum` int unsigned NOT NULL DEFAULT 0 COMMENT '喜欢(赞)数',
617619
`top` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '置顶,0否,1置顶',
618620
`seq` int NOT NULL DEFAULT 0 COMMENT '排序用,越大越靠前',
619621
`state` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '状态:0-正常;1-下线',

config/init.sql

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ VALUES
6666

6767
INSERT INTO `user_setting` (`id`, `key`, `value`, `remark`, `created_at`)
6868
VALUES
69-
(1, 'new_user_wait', 0, '新用户注册多久能发布帖子,单位秒,0表示没限制', '2017-05-30 18:11:31'),
70-
(2, 'can_edit_time', 300, '发布后多久内能够编辑,单位秒', '2017-05-30 18:12:53');
69+
(1, 'new_user_wait', 0, '新用户注册多久才能发布帖子,单位秒,0表示没限制', '2017-05-30 10:11:31'),
70+
(2, 'can_edit_time', 172800, '发布后多久内能够编辑,单位秒', '2017-05-30 10:12:53'),
71+
(3, 'publish_times', 3, '一天发布次数大于该值,需要验证码', '2018-10-01 10:47:23'),
72+
(4, 'publish_interval', 60, '发布时间间隔在该值内,需要验证码,单位秒', '2018-10-01 10:56:18');
73+
7174

7275
INSERT INTO `mission` (`id`, `name`, `type`, `fixed`, `min`, `max`, `incr`, `state`, `created_at`)
7376
VALUES

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,6 @@ github.com/polaris1119/config v0.0.0-20160609095218-06a751e884f3 h1:ql/tT34m2dfi
156156
github.com/polaris1119/config v0.0.0-20160609095218-06a751e884f3/go.mod h1:up6EO6QVWJMpgNMhqqZx2ImgXyfH28qf84Jf66SIm88=
157157
github.com/polaris1119/email v0.0.0-20171030115359-9c57dd3e3e7d h1:5Tx8BuKqRGhit6t1VHqOLbSLhxTFZF6DypyyA7nn/lI=
158158
github.com/polaris1119/email v0.0.0-20171030115359-9c57dd3e3e7d/go.mod h1:3S7Km+d2GftzOJJgrvIGHDtRxm5eUZamHaoUzLZNJX0=
159-
github.com/polaris1119/goutils v0.0.0-20160602032428-582c98b3184b h1:THcO90yP3NaMbkQmcnKyu8wJBS1iSO3Z7eAo5axqVbA=
160-
github.com/polaris1119/goutils v0.0.0-20160602032428-582c98b3184b/go.mod h1:yHb+G3YG7G0Hf0EIXCjbOe/Aus8TXZtYa/ZPEqyMxXU=
161159
github.com/polaris1119/goutils v0.0.0-20190815094239-73c47df9b896 h1:BYTbNGq/6Y28744ZVeOcD9s0qkeuTrDka8PCbaXPs4g=
162160
github.com/polaris1119/goutils v0.0.0-20190815094239-73c47df9b896/go.mod h1:yHb+G3YG7G0Hf0EIXCjbOe/Aus8TXZtYa/ZPEqyMxXU=
163161
github.com/polaris1119/keyword v0.0.0-20170608075927-96ae6735f2f2 h1:63TyJE7nj3eUsJ/1jbzrnH8gxw1z3oV/su2RIyVHlRQ=

http/controller/comment.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ import (
2323
"github.com/studygolang/studygolang/echoutils"
2424
)
2525

26+
// 在需要喜欢且要回调的地方注册喜欢对象
27+
func init() {
28+
// 注册喜欢对象
29+
logic.RegisterLikeObject(model.TypeComment, logic.CommentLike{})
30+
}
31+
2632
type CommentController struct{}
2733

2834
func (self CommentController) RegisterRoute(g *echo.Group) {

http/middleware/login.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ func NeedLogin() echo.MiddlewareFunc {
106106
}
107107
}
108108
}
109+
} else {
110+
newUserWait := time.Duration(logic.UserSetting[model.KeyNewUserWait]) * time.Second
111+
if newUserWait > 0 {
112+
elapse := time.Now().Sub(user.CreatedAt)
113+
if elapse <= newUserWait {
114+
return echo.NewHTTPError(http.StatusForbidden, `您需要再等待`+(newUserWait-elapse).String()+"才能进行此操作")
115+
}
116+
}
109117
}
110118

111119
if err := next(ctx); err != nil {

logic/comment.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,20 @@ func (CommentLogic) filterDelObjectCmt(comments []*model.Comment) []*model.Comme
451451
}
452452
return resultCmts
453453
}
454+
455+
// 回复赞(喜欢)
456+
type CommentLike struct{}
457+
458+
// 更新该回复的赞
459+
// objid:被喜欢对象id;num: 喜欢数(负数表示取消喜欢)
460+
func (self CommentLike) UpdateLike(objid, num int) {
461+
// 更新喜欢数(TODO:暂时每次都更新表)
462+
_, err := MasterDB.Where("id=?", objid).Incr("likenum", num).Update(new(model.Comment))
463+
if err != nil {
464+
logger.Errorln("更新回复喜欢数失败:", err)
465+
}
466+
}
467+
468+
func (self CommentLike) String() string {
469+
return "comment"
470+
}

logic/feed.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,14 @@ func (self FeedLogic) updateComment(objid, objtype, uid int, cmttime time.Time)
314314
}()
315315
}
316316

317-
// updateLike 更新动态赞数据(暂时没存)
318-
func (self FeedLogic) updateLike(objid, objtype, uid int, liketime time.Time) {
319-
self.updateSeq(objid, objtype, 0, 1, 0)
317+
// updateLike 更新动态赞数据
318+
func (self FeedLogic) updateLike(objid, objtype, uid, num int) {
319+
go func() {
320+
MasterDB.Where("objid=? AND objtype=?", objid, objtype).
321+
Incr("likenum", num).SetExpr("updated_at", "updated_at").
322+
Update(new(model.Feed))
323+
}()
324+
self.updateSeq(objid, objtype, 0, num, 0)
320325
}
321326

322327
func (self FeedLogic) modifyTopicNode(tid, nid int) {

logic/like.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package logic
99
import (
1010
"errors"
1111
"fmt"
12-
"time"
1312

1413
. "github.com/studygolang/studygolang/db"
1514

@@ -72,9 +71,6 @@ func (LikeLogic) FindUserLikeObjects(ctx context.Context, uid, objtype int, obji
7271
func (LikeLogic) LikeObject(ctx context.Context, uid, objid, objtype, likeFlag int) error {
7372
objLog := GetLogger(ctx)
7473

75-
// 点喜欢,活跃度+3
76-
go DefaultUser.IncrUserWeight("uid", uid, 3)
77-
7874
like := &model.Like{}
7975
_, err := MasterDB.Where("uid=? AND objid=? AND objtype=?", uid, objid, objtype).Get(like)
8076
if err != nil {
@@ -101,6 +97,8 @@ func (LikeLogic) LikeObject(ctx context.Context, uid, objid, objtype, likeFlag i
10197
// 取消喜欢成功,更新对象的喜欢数
10298
if liker, ok := likers[objtype]; ok {
10399
go liker.UpdateLike(objid, -1)
100+
101+
DefaultFeed.updateLike(objid, objtype, uid, -1)
104102
}
105103

106104
return nil
@@ -125,8 +123,10 @@ func (LikeLogic) LikeObject(ctx context.Context, uid, objid, objtype, likeFlag i
125123
if liker, ok := likers[objtype]; ok {
126124
go liker.UpdateLike(objid, 1)
127125

128-
DefaultFeed.updateLike(objid, objtype, uid, time.Now())
126+
DefaultFeed.updateLike(objid, objtype, uid, 1)
129127
}
128+
129+
go likeObservable.NotifyObservers(uid, objtype, objid)
130130
}
131131

132132
// TODO: 给被喜欢对象所有者发系统消息

logic/observer.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var (
2020
ViewObservable Observable
2121
appendObservable Observable
2222
topObservable Observable
23+
likeObservable Observable
2324
)
2425

2526
func init() {
@@ -52,6 +53,11 @@ func init() {
5253
topObservable.AddObserver(&UserWeightObserver{})
5354
topObservable.AddObserver(&TodayActiveObserver{})
5455
topObservable.AddObserver(&UserRichObserver{})
56+
57+
likeObservable = NewConcreteObservable(actionLike)
58+
likeObservable.AddObserver(&UserWeightObserver{})
59+
likeObservable.AddObserver(&TodayActiveObserver{})
60+
likeObservable.AddObserver(&UserRichObserver{})
5561
}
5662

5763
type Observer interface {
@@ -73,7 +79,8 @@ const (
7379
actionComment = "comment"
7480
actionView = "view"
7581
actionAppend = "append"
76-
actionTop = "top" // 置顶
82+
actionTop = "top" // 置顶
83+
actionLike = "like" // 喜欢(赞)
7784
)
7885

7986
type ConcreteObservable struct {
@@ -138,6 +145,8 @@ func (this *UserWeightObserver) Update(action string, uid, objtype, objid int) {
138145
weight = 15
139146
case actionTop:
140147
weight = 5
148+
case actionLike:
149+
weight = 3
141150
}
142151

143152
DefaultUser.IncrUserWeight("uid", uid, weight)
@@ -168,6 +177,8 @@ func (*TodayActiveObserver) Update(action string, uid, objtype, objid int) {
168177
weight = 15
169178
case actionTop:
170179
weight = 5
180+
case actionLike:
181+
weight = 5
171182
}
172183

173184
DefaultRank.GenDAURank(uid, weight)
@@ -395,6 +406,9 @@ func (UserRichObserver) Update(action string, uid, objtype, objid int) {
395406
article.Id,
396407
article.Title)
397408
}
409+
} else if action == actionLike {
410+
// TODO: 暂时不处理
411+
return
398412
}
399413

400414
DefaultUserRich.IncrUserRich(user, typ, award, desc)

logic/topic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ func (self TopicComment) UpdateComment(cid, objid, uid int, cmttime time.Time) {
684684
session.Begin()
685685

686686
// 更新最后回复信息
687-
_, err := session.Table(new(model.Topic)).Id(objid).Update(map[string]interface{}{
687+
_, err := session.Table(new(model.Topic)).ID(objid).Update(map[string]interface{}{
688688
"lastreplyuid": uid,
689689
"lastreplytime": cmttime,
690690
})

model/comment.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ const (
1616
TypeBook // 图书
1717
)
1818

19+
const (
20+
TypeComment = 100
21+
// 置顶
22+
TypeTop = 101
23+
)
24+
1925
const (
2026
TopicURI = "topics"
2127
ArticleURI = "articles"

model/user.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ const (
6262
DauAuthTop // 置顶
6363
)
6464

65-
// 置顶
66-
const (
67-
TypeComment = 100
68-
TypeTop = 101
69-
)
70-
7165
const DefaultAuth = DauAuthTopic | DauAuthArticle | DauAuthResource | DauAuthProject | DauAuthComment
7266

7367
// 用户基本信息

model/user_setting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package model
99
import "time"
1010

1111
const (
12-
KeyNewUserWait = "new_user_wait" // 新用户注册多久内发布帖子需要验证码,单位秒,0表示没限制
12+
KeyNewUserWait = "new_user_wait" // 新用户注册多久才能发布帖子,单位秒,0表示没限制
1313
KeyCanEditTime = "can_edit_time" // 发布后多久内能够编辑,单位秒
1414
KeyPublishTimes = "publish_times" // 一天发布次数大于该值,需要验证码
1515
KeyPublishInterval = "publish_interval" // 发布时间间隔在该值内,需要验证码,单位秒

template/index.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
{{define "seo"}}<meta name="keywords" content="{{.setting.Name}},{{.setting.SeoKeywords}},主题,资源,文章,图书,开源项目">
33
<meta name="description" content="{{.setting.SeoDescription}}">{{end}}
44
{{define "content"}}
5+
6+
<style type="text/css">
7+
.zan-wrap {
8+
background-color: rgba(1,126,102,0.08);
9+
color: #db6d4c;
10+
padding: 0;
11+
display: inline-block;
12+
height: 24px;
13+
width: 24px;
14+
line-height: 24px;
15+
text-align: center;
16+
margin-right: 5px;
17+
border-radius: 12px;
18+
margin-bottom: 1px;
19+
}
20+
</style>
521
<div class="row">
622
<div class="col-md-9 col-sm-6">
723
<div class="sep20"></div>
@@ -45,6 +61,11 @@
4561
{{if .Top}}
4662
<span style="color: #ff7700; border: 1px solid #ff7700;">置顶</span>
4763
{{end}}
64+
65+
<span class="zan-wrap">
66+
<i class="fa fa-thumbs-up "></i>
67+
</span>
68+
4869
{{if eq .Objtype 0}}
4970
<a class="node" href="/go/{{.Node.ename}}">{{.Node.name}}</a>
5071
{{else if eq .Objtype 1}}

0 commit comments

Comments
 (0)