IsAllowReadChapter 书籍章节是否允许阅读
(bookId int, docId int)
| 766 | |
| 767 | // IsAllowReadChapter 书籍章节是否允许阅读 |
| 768 | func (m *Document) IsAllowReadChapter(bookId int, docId int) (yes bool, percent int) { |
| 769 | percent = 100 |
| 770 | if bookId == 0 || docId == 0 { |
| 771 | return |
| 772 | } |
| 773 | |
| 774 | percent, _ = strconv.Atoi(GetOptionValue("VISITOR_ALLOW_READED_PERCENT", "0")) |
| 775 | if percent <= 0 { |
| 776 | return false, percent |
| 777 | } |
| 778 | |
| 779 | if percent >= 100 { |
| 780 | return true, percent |
| 781 | } |
| 782 | |
| 783 | var docs []Document |
| 784 | |
| 785 | orm.NewOrm().QueryTable(m).Filter("book_id", bookId).OrderBy("parent_id", "order_sort").All(&docs, "document_id", "identify") |
| 786 | length := len(docs) |
| 787 | if length == 0 { |
| 788 | return true, percent |
| 789 | } |
| 790 | |
| 791 | index := int(float64(length*percent) / 100) |
| 792 | if index <= 0 { |
| 793 | index = 1 |
| 794 | } |
| 795 | for _, doc := range docs[:index] { |
| 796 | if doc.DocumentId == docId { |
| 797 | return true, percent |
| 798 | } |
| 799 | } |
| 800 | return false, percent |
| 801 | } |
no test coverage detected