diff --git a/commands/command.go b/commands/command.go index 34a35ca2..a420d85d 100644 --- a/commands/command.go +++ b/commands/command.go @@ -109,6 +109,7 @@ func RegisterModel() { new(models.TeamMember), new(models.TeamRelationship), new(models.Itemsets), + new(models.DocumentViewCount), ) gob.Register(models.Blog{}) gob.Register(models.Document{}) diff --git a/controllers/DocumentController.go b/controllers/DocumentController.go index dbbba7c8..126972bf 100644 --- a/controllers/DocumentController.go +++ b/controllers/DocumentController.go @@ -143,17 +143,22 @@ func (c *DocumentController) Read() { doc.AttachList = attach } + view_count := models.NewDocumentViewCount().IncrViewCount(doc.DocumentId) + c.Data["ViewCount"] = view_count + if c.IsAjax() { var data struct { DocTitle string `json:"doc_title"` Body string `json:"body"` Title string `json:"title"` Version int64 `json:"version"` + ViewCount int `json:"view_count"` } data.DocTitle = doc.DocumentName data.Body = doc.Release data.Title = doc.DocumentName + " - Powered by MinDoc" data.Version = doc.Version + data.ViewCount = view_count c.JsonResult(0, "ok", data) } diff --git a/models/DocumentViewCount.go b/models/DocumentViewCount.go new file mode 100644 index 00000000..5e926e41 --- /dev/null +++ b/models/DocumentViewCount.go @@ -0,0 +1,44 @@ +package models + +import ( + "github.com/astaxie/beego/orm" + "github.com/mindoc-org/mindoc/conf" +) + +type DocumentViewCount struct { + DocumentId int `orm:"pk;column(document_id);type(int)" json:"doc_id"` + ViewCount int `orm:"column(view_count);type(int)" json:"view_count"` +} + +// TableName 获取对应数据库表名. +func (v *DocumentViewCount) TableName() string { + return "document_viewcount" +} + +// TableEngine 获取数据使用的引擎. +func (v *DocumentViewCount) TableEngine() string { + return "INNODB" +} + +func (v *DocumentViewCount) TableNameWithPrefix() string { + return conf.GetDatabasePrefix() + v.TableName() +} + +func NewDocumentViewCount() *DocumentViewCount { + return &DocumentViewCount{} +} + +func (v *DocumentViewCount) IncrViewCount(id int) int { + o := orm.NewOrm() + num, _ := o.QueryTable(v.TableNameWithPrefix()).Filter("document_id", id).Update(orm.Params{ + "view_count": orm.ColValue(orm.ColAdd, 1), + }) + if 0 == num { + v.DocumentId = id + v.ViewCount = 1 + num, _ = o.Insert(v) + } else { + o.QueryTable(v.TableNameWithPrefix()).Filter("document_id", id).One(v) + } + return v.ViewCount +} diff --git a/static/js/kancloud.js b/static/js/kancloud.js index 47b72847..3363b395 100644 --- a/static/js/kancloud.js +++ b/static/js/kancloud.js @@ -65,6 +65,7 @@ function loadDocument($url, $id, $callback) { $("title").text(data.title); $("#article-title").text(data.doc_title); $("#article-info").text(data.doc_info); + $("#view_count").text("阅读次数:" + data.view_count); events.trigger('article.open', {$url: $url, $id: $id}); @@ -80,6 +81,7 @@ function loadDocument($url, $id, $callback) { var doc_title = res.data.doc_title; var title = res.data.title; var doc_info = res.data.doc_info; + var view_count = res.data.view_count; $body = body; if (typeof $callback === "function" ) { @@ -90,6 +92,7 @@ function loadDocument($url, $id, $callback) { $("title").text(title); $("#article-title").text(doc_title); $("#article-info").text(doc_info); + $("#view_count").text("阅读次数:" + view_count); events.data($id, res.data); diff --git a/views/document/default_read.tpl b/views/document/default_read.tpl index 383dec41..27a47d42 100644 --- a/views/document/default_read.tpl +++ b/views/document/default_read.tpl @@ -146,6 +146,7 @@