Skip to content

Commit 24c79c9

Browse files
committed
支持限定游客可阅读的章节数量
1 parent d3ac739 commit 24c79c9

File tree

8 files changed

+90
-11
lines changed

8 files changed

+90
-11
lines changed

controllers/DocumentController.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ func (this *DocumentController) indexWithPassword() {
228228

229229
//阅读文档.
230230
func (this *DocumentController) Read() {
231+
231232
identify := this.Ctx.Input.Param(":key")
232233
token := this.GetString("token")
233234
id := this.GetString(":id")
@@ -272,6 +273,13 @@ func (this *DocumentController) Read() {
272273
this.Abort404(bookName, bookLink)
273274
}
274275

276+
// 是否允许阅读
277+
isAllowRead := true
278+
percent := 100
279+
if this.Member.MemberId == 0 {
280+
isAllowRead, percent = doc.IsAllowReadChapter(doc.BookId, doc.DocumentId)
281+
}
282+
275283
bodyText := ""
276284
authHTTPS := strings.ToLower(models.GetOptionValue("AUTO_HTTPS", "false")) == "true"
277285
if doc.Release != "" {
@@ -387,16 +395,22 @@ func (this *DocumentController) Read() {
387395
models.NewBookCounter().Increase(bookResult.BookId, true)
388396
comments, _ := models.NewComments().Comments(1, 1000, models.CommentOpt{DocId: doc.DocumentId, Status: []int{1}})
389397

398+
if !isAllowRead {
399+
doc.Release = ""
400+
}
401+
390402
if this.IsAjax() {
391403
var data struct {
392-
Id int `json:"doc_id"`
393-
DocTitle string `json:"doc_title"`
394-
Body string `json:"body"`
395-
Title string `json:"title"`
396-
Bookmark bool `json:"bookmark"` //是否已经添加了书签
397-
View int `json:"view"`
398-
UpdatedAt string `json:"updated_at"`
399-
Comments []models.BookCommentsResult `json:"comments"`
404+
Id int `json:"doc_id"`
405+
DocTitle string `json:"doc_title"`
406+
Body string `json:"body"`
407+
Title string `json:"title"`
408+
Bookmark bool `json:"bookmark"` //是否已经添加了书签
409+
View int `json:"view"`
410+
UpdatedAt string `json:"updated_at"`
411+
Comments []models.BookCommentsResult `json:"comments"`
412+
IsAllowRead bool `json:"is_allow_read"`
413+
Percent int `json:"percent"`
400414
}
401415
data.DocTitle = doc.DocumentName
402416
data.Body = doc.Release
@@ -405,9 +419,9 @@ func (this *DocumentController) Read() {
405419
data.Bookmark = existBookmark
406420
data.View = doc.Vcnt
407421
data.UpdatedAt = doc.ModifyTime.Format("2006-01-02 15:04:05")
408-
//data.Body = doc.Markdown
409422
data.Comments = comments
410-
423+
data.IsAllowRead = isAllowRead
424+
data.Percent = percent
411425
this.JsonResult(0, "ok", data)
412426
}
413427

@@ -474,6 +488,8 @@ func (this *DocumentController) Read() {
474488
this.Data["UpdatedAt"] = doc.ModifyTime.Format("2006-01-02 15:04:05")
475489
this.Data["ExistWeCode"] = strings.TrimSpace(models.GetOptionValue("DOWNLOAD_WECODE", "")) != ""
476490
this.Data["Comments"] = comments
491+
this.Data["IsAllowRead"] = isAllowRead
492+
this.Data["Percent"] = percent
477493
}
478494

479495
//编辑文档.

models/document.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package models
22

33
import (
44
"os"
5+
"strconv"
56
"strings"
67
"time"
78

@@ -737,3 +738,39 @@ func (m *Document) SplitMarkdownAndStore(seg string, markdown string, docId int)
737738
}
738739
return
739740
}
741+
742+
// IsAllowReadChapter 书籍章节是否允许阅读
743+
func (m *Document) IsAllowReadChapter(bookId int, docId int) (yes bool, percent int) {
744+
percent = 100
745+
if bookId == 0 || docId == 0 {
746+
return
747+
}
748+
749+
percent, _ = strconv.Atoi(GetOptionValue("VISITOR_ALLOW_READED_PERCENT", "0"))
750+
if percent <= 0 {
751+
return false, percent
752+
}
753+
754+
if percent >= 100 {
755+
return true, percent
756+
}
757+
758+
var docs []Document
759+
760+
orm.NewOrm().QueryTable(m).Filter("book_id", bookId).OrderBy("parent_id", "order_sort").All(&docs, "document_id", "identify")
761+
length := len(docs)
762+
if length == 0 {
763+
return true, percent
764+
}
765+
766+
index := int(float64(length*percent) / 100)
767+
if index <= 0 {
768+
index = 1
769+
}
770+
for _, doc := range docs[:index] {
771+
if doc.DocumentId == docId {
772+
return true, percent
773+
}
774+
}
775+
return false, percent
776+
}

models/options.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ func (m *Option) Init() error {
321321
OptionName: "DEFAULT_HOME_PAGE",
322322
OptionTitle: "默认首页",
323323
},
324+
{
325+
OptionValue: "100",
326+
OptionName: "VISITOR_ALLOW_READED_PERCENT",
327+
OptionTitle: "游客允许阅读百分比", // 游客未登录情况下允许阅读的章节百分比
328+
},
324329
}
325330

326331
for _, op := range options {

static/css/bookstack.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,4 +1497,10 @@ form .btn-row .col-md-10{padding-left: 0;}
14971497
left: 18px;
14981498
top: 6px;
14991499
position: absolute;
1500+
}
1501+
1502+
.locked-reading{padding-bottom: 100px;}
1503+
.locked-reading .locked-icon{
1504+
width: 128px;height: 128px;margin: 100px auto 15px;
1505+
background: url("/static/images/lock.png") no-repeat center center;
15001506
}

static/images/lock.png

3.72 KB
Loading

static/js/bookstack.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ function load_doc(url,wd,without_history) {
4848
var doc_title = res.data.doc_title;
4949
var title = res.data.title;
5050
var $body = body;
51+
try {
52+
if(res.data.is_allow_read==false){$body='<div class="locked-reading text-center"><div class="locked-icon"></div><div class="help-block">未登录游客仅可阅读 <span style="color:red">'+res.data.percent+'%</span> 章节,请您 <a href="/login" title="登录">登录</a> 再阅读</div>' }
53+
} catch (error) {}
5154
$("#page-content").html($body);
5255
// RenderByMarkdown($body);
5356
$("title").text(title);

views/document/default_read.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,14 @@ <h1 id="article-title">{{.Title}}</h1>
129129
</div>
130130
{{end}}
131131
<article class="article-body {{if eq .Model.Editor "markdown"}}markdown-body editormd-preview-container{{else}}editor-content{{end}}" id="page-content">
132-
{{.Content}}
132+
{{if .IsAllowRead}}
133+
{{.Content}}
134+
{{else}}
135+
<div class="locked-reading text-center">
136+
<div class="locked-icon"></div>
137+
<div class="help-block">未登录游客仅可阅读 <span style="color:red">{{.Percent}}%</span> 章节,请您 <a href="/login" title="登录">登录</a> 再阅读</div>
138+
</div>
139+
{{end}}
133140
</article>
134141
{{if (ads "content-bottom" $.IsMobile)}}
135142
<div class="ap ap-content-bottom mgt-15px">

views/manager/setting.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@
380380
{{end}}
381381

382382
{{if eq .Tab "operation"}}
383+
<div class="form-group">
384+
<label>游客允许阅读章节百分比</label>
385+
<input type="number" min="0" max="100" name="VISITOR_ALLOW_READED_PERCENT" class="form-control" value="{{.VISITOR_ALLOW_READED_PERCENT.OptionValue}}">
386+
<div class="help-block">未登录用户,允许阅读的书籍章节百分比。0表示不允许,100表示所有章节游客都可以阅读</div>
387+
</div>
383388
<div class="form-group">
384389
<label>是否需要登录才能下载电子书</label>
385390
<input type="text" name="DOWNLOAD_LIMIT" class="form-control" value="{{.DOWNLOAD_LIMIT.OptionValue}}">

0 commit comments

Comments
 (0)