增加文章阅读次数

pull/674/head
wangbin05 2021-03-31 19:32:18 +08:00
parent bae6826a97
commit 5b274ce836
5 changed files with 54 additions and 0 deletions

View File

@ -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{})

View File

@ -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)
}

View File

@ -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
}

View File

@ -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);

View File

@ -146,6 +146,7 @@
</div>
<div class="m-copyright">
<p>
<div id="view_count">阅读次数:{{.ViewCount}}</div>
本文档使用 <a href="https://www.iminho.me" target="_blank">MinDoc</a> 发布
</p>
</div>