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

Skip to content

Commit db0a113

Browse files
committed
浏览记录保存
1 parent b781d93 commit db0a113

26 files changed

+356
-79
lines changed

config/db.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,3 +566,13 @@ CREATE TABLE IF NOT EXISTS `feed` (
566566
INDEX `idx_updated_at` (`updated_at`)
567567
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='网站关键资源动态表';
568568

569+
CREATE TABLE `view_record` (
570+
`id` int unsigned NOT NULL AUTO_INCREMENT,
571+
`objid` int(10) unsigned NOT NULL COMMENT '对象id,属主',
572+
`objtype` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '类型,0-帖子;1-博客;2-资源;3-wiki;4-项目;5-图书',
573+
`uid` int unsigned NOT NULL DEFAULT 0 COMMENT '浏览人UID',
574+
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
575+
PRIMARY KEY (`id`),
576+
UNIQUE KEY `uniq_obj_uid` (`objid`,`objtype`,`uid`),
577+
INDEX `idx_uid` (`uid`)
578+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '用户浏览记录表';

src/http/controller/article.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,22 +120,35 @@ func (ArticleController) Detail(ctx echo.Context) error {
120120
return ctx.Redirect(http.StatusSeeOther, "/articles")
121121
}
122122

123-
likeFlag := 0
124-
hadCollect := 0
123+
data := map[string]interface{}{
124+
"activeArticles": "active",
125+
"article": article,
126+
"prev": prevNext[0],
127+
"next": prevNext[1],
128+
}
129+
125130
me, ok := ctx.Get("user").(*model.Me)
126131
if ok {
127-
likeFlag = logic.DefaultLike.HadLike(ctx, me.Uid, article.Id, model.TypeArticle)
128-
hadCollect = logic.DefaultFavorite.HadFavorite(ctx, me.Uid, article.Id, model.TypeArticle)
132+
data["likeflag"] = logic.DefaultLike.HadLike(ctx, me.Uid, article.Id, model.TypeArticle)
133+
data["hadcollect"] = logic.DefaultFavorite.HadFavorite(ctx, me.Uid, article.Id, model.TypeArticle)
129134

130135
logic.Views.Incr(Request(ctx), model.TypeArticle, article.Id, me.Uid)
136+
137+
if article.IsSelf && me.Uid != article.User.Uid {
138+
go logic.DefaultViewRecord.Record(article.Id, model.TypeArticle, me.Uid)
139+
}
140+
141+
if me.IsRoot || (article.IsSelf && me.Uid == article.User.Uid) {
142+
data["view_user_num"] = logic.DefaultViewRecord.FindUserNum(ctx, article.Id, model.TypeArticle)
143+
}
131144
} else {
132145
logic.Views.Incr(Request(ctx), model.TypeArticle, article.Id)
133146
}
134147

135148
// 为了阅读数即时看到
136149
article.Viewnum++
137150

138-
return render(ctx, "articles/detail.html,common/comment.html", map[string]interface{}{"activeArticles": "active", "article": article, "prev": prevNext[0], "next": prevNext[1], "likeflag": likeFlag, "hadcollect": hadCollect})
151+
return render(ctx, "articles/detail.html,common/comment.html", data)
139152
}
140153

141154
// Create 发布新文章

src/http/controller/comment.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ func (CommentController) Create(ctx echo.Context) error {
4040

4141
// 入库
4242
objid := goutils.MustInt(ctx.Param("objid"))
43+
if objid == 0 {
44+
return fail(ctx, 1, "参数有误,请刷新后重试!")
45+
}
4346
comment, err := logic.DefaultComment.Publish(ctx, user.Uid, objid, ctx.FormParams())
4447
if err != nil {
45-
return fail(ctx, 1, "服务器内部错误")
48+
return fail(ctx, 2, "服务器内部错误")
4649
}
4750

4851
return success(ctx, comment)

src/http/controller/project.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,33 @@ func (ProjectController) Detail(ctx echo.Context) error {
141141
return ctx.Redirect(http.StatusSeeOther, "/projects")
142142
}
143143

144-
likeFlag := 0
145-
hadCollect := 0
144+
data := map[string]interface{}{
145+
"activeProjects": "active",
146+
"project": project,
147+
}
148+
146149
me, ok := ctx.Get("user").(*model.Me)
147150
if ok {
148-
likeFlag = logic.DefaultLike.HadLike(ctx, me.Uid, project.Id, model.TypeProject)
149-
hadCollect = logic.DefaultFavorite.HadFavorite(ctx, me.Uid, project.Id, model.TypeProject)
151+
data["likeflag"] = logic.DefaultLike.HadLike(ctx, me.Uid, project.Id, model.TypeProject)
152+
data["hadcollect"] = logic.DefaultFavorite.HadFavorite(ctx, me.Uid, project.Id, model.TypeProject)
150153

151154
logic.Views.Incr(Request(ctx), model.TypeProject, project.Id, me.Uid)
155+
156+
if me.Uid != project.User.Uid {
157+
go logic.DefaultViewRecord.Record(project.Id, model.TypeProject, me.Uid)
158+
}
159+
160+
if me.IsRoot || me.Uid == project.User.Uid {
161+
data["view_user_num"] = logic.DefaultViewRecord.FindUserNum(ctx, project.Id, model.TypeProject)
162+
}
152163
} else {
153164
logic.Views.Incr(Request(ctx), model.TypeProject, project.Id)
154165
}
155166

156167
// 为了阅读数即时看到
157168
project.Viewnum++
158169

159-
return render(ctx, "projects/detail.html,common/comment.html", map[string]interface{}{"activeProjects": "active", "project": project, "likeflag": likeFlag, "hadcollect": hadCollect})
170+
return render(ctx, "projects/detail.html,common/comment.html", data)
160171
}
161172

162173
// CheckExist 检测 uri 对应的项目是否存在(验证,true表示不存在;false表示存在)

src/http/controller/resource.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,30 @@ func (ResourceController) Detail(ctx echo.Context) error {
6565
return ctx.Redirect(http.StatusSeeOther, "/resources/cat/1")
6666
}
6767

68-
likeFlag := 0
69-
hadCollect := 0
68+
data := map[string]interface{}{
69+
"activeResources": "active",
70+
"resource": resource,
71+
"comments": comments,
72+
}
73+
7074
me, ok := ctx.Get("user").(*model.Me)
7175
if ok {
7276
id := resource["id"].(int)
73-
likeFlag = logic.DefaultLike.HadLike(ctx, me.Uid, id, model.TypeResource)
74-
hadCollect = logic.DefaultFavorite.HadFavorite(ctx, me.Uid, id, model.TypeResource)
77+
data["likeflag"] = logic.DefaultLike.HadLike(ctx, me.Uid, id, model.TypeResource)
78+
data["hadcollect"] = logic.DefaultFavorite.HadFavorite(ctx, me.Uid, id, model.TypeResource)
7579

7680
logic.Views.Incr(Request(ctx), model.TypeResource, id, me.Uid)
81+
82+
if me.Uid != resource["uid"].(int) {
83+
go logic.DefaultViewRecord.Record(id, model.TypeResource, me.Uid)
84+
} else {
85+
data["view_user_num"] = logic.DefaultViewRecord.FindUserNum(ctx, id, model.TypeResource)
86+
}
7787
} else {
7888
logic.Views.Incr(Request(ctx), model.TypeResource, id)
7989
}
8090

81-
return render(ctx, "resources/detail.html,common/comment.html", map[string]interface{}{"activeResources": "active", "resource": resource, "comments": comments, "likeflag": likeFlag, "hadcollect": hadCollect})
91+
return render(ctx, "resources/detail.html,common/comment.html", data)
8292
}
8393

8494
// Create 发布新资源

src/http/controller/topic.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,20 +142,30 @@ func (TopicController) Detail(ctx echo.Context) error {
142142
return render(ctx, "notfound.html", nil)
143143
}
144144

145-
likeFlag := 0
146-
hadCollect := 0
145+
data := map[string]interface{}{
146+
"activeTopics": "active",
147+
"topic": topic,
148+
"replies": replies,
149+
}
150+
147151
me, ok := ctx.Get("user").(*model.Me)
148152
if ok {
149153
tid := topic["tid"].(int)
150-
likeFlag = logic.DefaultLike.HadLike(ctx, me.Uid, tid, model.TypeTopic)
151-
hadCollect = logic.DefaultFavorite.HadFavorite(ctx, me.Uid, tid, model.TypeTopic)
154+
data["likeflag"] = logic.DefaultLike.HadLike(ctx, me.Uid, tid, model.TypeTopic)
155+
data["hadcollect"] = logic.DefaultFavorite.HadFavorite(ctx, me.Uid, tid, model.TypeTopic)
152156

153157
logic.Views.Incr(Request(ctx), model.TypeTopic, tid, me.Uid)
158+
159+
if me.Uid != topic["uid"].(int) {
160+
go logic.DefaultViewRecord.Record(tid, model.TypeTopic, me.Uid)
161+
} else {
162+
data["view_user_num"] = logic.DefaultViewRecord.FindUserNum(ctx, tid, model.TypeTopic)
163+
}
154164
} else {
155165
logic.Views.Incr(Request(ctx), model.TypeTopic, tid)
156166
}
157167

158-
return render(ctx, "topics/detail.html,common/comment.html", map[string]interface{}{"activeTopics": "active", "topic": topic, "replies": replies, "likeflag": likeFlag, "hadcollect": hadCollect})
168+
return render(ctx, "topics/detail.html,common/comment.html", data)
159169
}
160170

161171
// Create 新建主题

src/http/controller/user.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ func (UserController) Home(ctx echo.Context) error {
4343
projects := logic.DefaultProject.FindRecent(ctx, user.Username)
4444
comments := logic.DefaultComment.FindRecent(ctx, user.Uid, -1, 5)
4545

46-
return render(ctx, "user/profile.html", map[string]interface{}{"activeUsers": "active", "topics": topics, "resources": resources, "projects": projects, "comments": comments, "user": user})
46+
user.IsOnline = logic.Book.RegUserIsOnline(user.Uid)
47+
48+
return render(ctx, "user/profile.html", map[string]interface{}{
49+
"activeUsers": "active",
50+
"topics": topics,
51+
"resources": resources,
52+
"projects": projects,
53+
"comments": comments,
54+
"user": user,
55+
})
4756
}
4857

4958
// ReadList 会员列表

src/http/controller/websocket.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ func (this *WebsocketController) Ws(wsConn *websocket.Conn) {
3737
this.ServerId++
3838
serverId := this.ServerId
3939
this.mutex.Unlock()
40+
41+
isUid := true
4042
req := wsConn.Request()
4143
user := goutils.MustInt(req.FormValue("uid"))
4244
if user == 0 {
4345
user = int(goutils.Ip2long(goutils.RemoteIp(req)))
46+
isUid = false
4447
}
45-
userData := logic.Book.AddUser(user, serverId)
48+
userData := logic.Book.AddUser(user, serverId, isUid)
4649
// 给自己发送消息,告诉当前在线用户数、历史最高在线人数
4750
onlineInfo := map[string]int{"online": logic.Book.Len(), "maxonline": logic.MaxOnlineNum()}
4851
message := logic.NewMessage(logic.WsMsgOnline, onlineInfo)
@@ -64,7 +67,7 @@ func (this *WebsocketController) Ws(wsConn *websocket.Conn) {
6467
}
6568
}
6669
if clientClosed {
67-
logic.Book.DelUser(user, serverId)
70+
logic.Book.DelUser(user, serverId, isUid)
6871
logger.Infoln("user:", user, "client close")
6972
break
7073
}

src/logic/article.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,22 @@ func (ArticleLogic) FindById(ctx context.Context, id interface{}) (*model.Articl
561561
return article, err
562562
}
563563

564+
// getOwner 通过objid获得 article 的所有者
565+
func (ArticleLogic) getOwner(id int) int {
566+
article := &model.Article{}
567+
_, err := MasterDB.Id(id).Get(article)
568+
if err != nil {
569+
logger.Errorln("article logic getOwner Error:", err)
570+
return 0
571+
}
572+
573+
if article.IsSelf {
574+
user := DefaultUser.FindOne(nil, "username", article.Author)
575+
return user.Uid
576+
}
577+
return 0
578+
}
579+
564580
// 博文评论
565581
type ArticleComment struct{}
566582

src/logic/book.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,18 @@ func (this *UserData) SendMessage(message *Message) {
8686
}
8787
}
8888

89-
var Book = &book{users: make(map[int]*UserData)}
89+
var Book = &book{users: make(map[int]*UserData), uids: make(map[int]struct{})}
9090

9191
type book struct {
92-
users map[int]*UserData
92+
users map[int]*UserData
93+
// 登录用户
94+
uids map[int]struct{}
9395
rwMutex sync.RWMutex
9496
}
9597

9698
// 增加一个用户到book中(有可能是用户的另一个请求)
9799
// user为UID或IP地址的int表示
98-
func (this *book) AddUser(user, serverId int) *UserData {
100+
func (this *book) AddUser(user, serverId int, isUid bool) *UserData {
99101
var userData *UserData
100102
var ok bool
101103
this.rwMutex.Lock()
@@ -111,6 +113,9 @@ func (this *book) AddUser(user, serverId int) *UserData {
111113
lastAccessTime: time.Now(),
112114
}
113115
this.users[user] = userData
116+
if isUid {
117+
this.uids[user] = struct{}{}
118+
}
114119
length := len(this.users)
115120

116121
this.rwMutex.Unlock()
@@ -133,19 +138,22 @@ func (this *book) AddUser(user, serverId int) *UserData {
133138
}
134139

135140
// 删除用户
136-
func (this *book) DelUser(user, serverId int) {
141+
func (this *book) DelUser(user, serverId int, isUid bool) {
137142
this.rwMutex.Lock()
138143
defer this.rwMutex.Unlock()
139144

140145
// 自己只有一个页面建立websocket连接
141146
if this.users[user].Len() == 1 {
142147
delete(this.users, user)
148+
if isUid {
149+
delete(this.uids, user)
150+
}
143151
} else {
144152
this.users[user].Remove(serverId)
145153
}
146154
}
147155

148-
// 判断用户是否还在线
156+
// 判断用户是否还在线(user 有可能是IP)
149157
func (this *book) UserIsOnline(user int) bool {
150158
this.rwMutex.RLock()
151159
defer this.rwMutex.RUnlock()
@@ -155,13 +163,30 @@ func (this *book) UserIsOnline(user int) bool {
155163
return false
156164
}
157165

166+
// 判断注册用户是否还在线(user 有可能是IP)
167+
func (this *book) RegUserIsOnline(uid int) bool {
168+
this.rwMutex.RLock()
169+
defer this.rwMutex.RUnlock()
170+
if _, ok := this.uids[uid]; ok {
171+
return true
172+
}
173+
return false
174+
}
175+
158176
// 在线用户数
159177
func (this *book) Len() int {
160178
this.rwMutex.RLock()
161179
defer this.rwMutex.RUnlock()
162180
return len(this.users)
163181
}
164182

183+
// 在线注册会员数
184+
func (this *book) LoginLen() int {
185+
this.rwMutex.RLock()
186+
defer this.rwMutex.RUnlock()
187+
return len(this.uids)
188+
}
189+
165190
// 给某个用户发送一条消息
166191
func (this *book) PostMessage(uid int, message *Message) {
167192
this.rwMutex.RLock()

src/logic/comment.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ func (CommentLogic) sendSystemMsg(ctx context.Context, uid, objid, objtype, cid
208208
case model.TypeTopic:
209209
to = DefaultTopic.getOwner(objid)
210210
case model.TypeArticle:
211+
to = DefaultArticle.getOwner(objid)
211212
case model.TypeResource:
212213
to = DefaultResource.getOwner(objid)
213214
case model.TypeWiki:

0 commit comments

Comments
 (0)