mirror of
https://github.com/mindoc-org/mindoc.git
synced 2026-02-27 17:03:57 +08:00
feat:实现自定义模板功能
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -14,7 +13,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/astaxie/beego/orm"
|
||||
@@ -24,6 +22,7 @@ import (
|
||||
"github.com/lifei6671/mindoc/utils/requests"
|
||||
"github.com/lifei6671/mindoc/utils/ziptil"
|
||||
"gopkg.in/russross/blackfriday.v2"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
// Book struct .
|
||||
@@ -73,6 +72,15 @@ type Book struct {
|
||||
IsUseFirstDocument int `orm:"column(is_use_first_document);type(int);default(0)" json:"is_use_first_document"`
|
||||
}
|
||||
|
||||
func (b *Book) String() string {
|
||||
ret, err := json.Marshal(*b)
|
||||
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(ret)
|
||||
}
|
||||
|
||||
// TableName 获取对应数据库表名.
|
||||
func (book *Book) TableName() string {
|
||||
return "books"
|
||||
@@ -286,10 +294,10 @@ func (book *Book) FindByFieldFirst(field string, value interface{}) (*Book, erro
|
||||
}
|
||||
|
||||
//根据项目标识查询项目
|
||||
func (book *Book) FindByIdentify(identify string) (*Book, error) {
|
||||
func (book *Book) FindByIdentify(identify string,cols ...string) (*Book, error) {
|
||||
o := orm.NewOrm()
|
||||
|
||||
err := o.QueryTable(book.TableNameWithPrefix()).Filter("identify", identify).One(book)
|
||||
err := o.QueryTable(book.TableNameWithPrefix()).Filter("identify", identify).One(book,cols...)
|
||||
|
||||
return book, err
|
||||
}
|
||||
@@ -500,59 +508,8 @@ func (book *Book) ReleaseContent(bookId int) {
|
||||
return
|
||||
}
|
||||
for _, item := range docs {
|
||||
if item.Content != "" {
|
||||
item.Release = item.Content
|
||||
bufio := bytes.NewReader([]byte(item.Content))
|
||||
//解析文档中非本站的链接,并设置为新窗口打开
|
||||
if content, err := goquery.NewDocumentFromReader(bufio); err == nil {
|
||||
|
||||
content.Find("a").Each(func(i int, contentSelection *goquery.Selection) {
|
||||
if src, ok := contentSelection.Attr("href"); ok {
|
||||
if strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://") {
|
||||
//beego.Info(src,conf.BaseUrl,strings.HasPrefix(src,conf.BaseUrl))
|
||||
if conf.BaseUrl != "" && !strings.HasPrefix(src, conf.BaseUrl) {
|
||||
contentSelection.SetAttr("target", "_blank")
|
||||
if html, err := content.Html(); err == nil {
|
||||
item.Release = html
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
attachList, err := NewAttachment().FindListByDocumentId(item.DocumentId)
|
||||
if err == nil && len(attachList) > 0 {
|
||||
content := bytes.NewBufferString("<div class=\"attach-list\"><strong>附件</strong><ul>")
|
||||
for _, attach := range attachList {
|
||||
if strings.HasPrefix(attach.HttpPath, "/") {
|
||||
attach.HttpPath = strings.TrimSuffix(conf.BaseUrl, "/") + attach.HttpPath
|
||||
}
|
||||
li := fmt.Sprintf("<li><a href=\"%s\" target=\"_blank\" title=\"%s\">%s</a></li>", attach.HttpPath, attach.FileName, attach.FileName)
|
||||
|
||||
content.WriteString(li)
|
||||
}
|
||||
content.WriteString("</ul></div>")
|
||||
item.Release += content.String()
|
||||
}
|
||||
_, err = o.Update(item, "release")
|
||||
if err != nil {
|
||||
beego.Error(fmt.Sprintf("发布失败 => %+v", item), err)
|
||||
} else {
|
||||
//当文档发布后,需要清除已缓存的转换文档和文档缓存
|
||||
if doc, err := NewDocument().Find(item.DocumentId); err == nil {
|
||||
doc.PutToCache()
|
||||
} else {
|
||||
doc.RemoveCache()
|
||||
}
|
||||
|
||||
if err := os.RemoveAll(filepath.Join(conf.WorkingDirectory, "uploads", "books", strconv.Itoa(bookId))); err != nil {
|
||||
beego.Error("删除已缓存的文档目录失败 => ",filepath.Join(conf.WorkingDirectory, "uploads", "books", strconv.Itoa(bookId)))
|
||||
}
|
||||
|
||||
}
|
||||
item.BookId = bookId
|
||||
item.ReleaseContent()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"github.com/lifei6671/mindoc/utils/requests"
|
||||
"github.com/lifei6671/mindoc/utils/gopool"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
var(
|
||||
@@ -69,8 +70,17 @@ func NewBookResult() *BookResult {
|
||||
return &BookResult{}
|
||||
}
|
||||
|
||||
func (b *BookResult) String() string {
|
||||
ret, err := json.Marshal(*b)
|
||||
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(ret)
|
||||
}
|
||||
|
||||
// 根据项目标识查询项目以及指定用户权限的信息.
|
||||
func (m *BookResult) FindByIdentify(identify string, memberId int) (*BookResult, error) {
|
||||
func (m *BookResult) FindByIdentify(identify string, memberId int,cols ...string) (*BookResult, error) {
|
||||
if identify == "" || memberId <= 0 {
|
||||
return m, ErrInvalidParameter
|
||||
}
|
||||
@@ -78,7 +88,7 @@ func (m *BookResult) FindByIdentify(identify string, memberId int) (*BookResult,
|
||||
|
||||
book := NewBook()
|
||||
|
||||
err := o.QueryTable(book.TableNameWithPrefix()).Filter("identify", identify).One(book)
|
||||
err := o.QueryTable(book.TableNameWithPrefix()).Filter("identify", identify).One(book,cols...)
|
||||
|
||||
if err != nil {
|
||||
return m, err
|
||||
|
||||
@@ -10,6 +10,11 @@ import (
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/lifei6671/mindoc/cache"
|
||||
"github.com/lifei6671/mindoc/conf"
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
"strings"
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Document struct.
|
||||
@@ -228,3 +233,67 @@ func (m *Document) IsExist(documentId int) bool {
|
||||
|
||||
return o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", documentId).Exist()
|
||||
}
|
||||
|
||||
//发布单篇文档
|
||||
func (item *Document) ReleaseContent() error {
|
||||
|
||||
o := orm.NewOrm()
|
||||
|
||||
bookId := item.BookId
|
||||
|
||||
if item.Content != "" {
|
||||
item.Release = item.Content
|
||||
bufio := bytes.NewReader([]byte(item.Content))
|
||||
//解析文档中非本站的链接,并设置为新窗口打开
|
||||
if content, err := goquery.NewDocumentFromReader(bufio); err == nil {
|
||||
|
||||
content.Find("a").Each(func(i int, contentSelection *goquery.Selection) {
|
||||
if src, ok := contentSelection.Attr("href"); ok {
|
||||
if strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://") {
|
||||
//beego.Info(src,conf.BaseUrl,strings.HasPrefix(src,conf.BaseUrl))
|
||||
if conf.BaseUrl != "" && !strings.HasPrefix(src, conf.BaseUrl) {
|
||||
contentSelection.SetAttr("target", "_blank")
|
||||
if html, err := content.Html(); err == nil {
|
||||
item.Release = html
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
attachList, err := NewAttachment().FindListByDocumentId(item.DocumentId)
|
||||
if err == nil && len(attachList) > 0 {
|
||||
content := bytes.NewBufferString("<div class=\"attach-list\"><strong>附件</strong><ul>")
|
||||
for _, attach := range attachList {
|
||||
if strings.HasPrefix(attach.HttpPath, "/") {
|
||||
attach.HttpPath = strings.TrimSuffix(conf.BaseUrl, "/") + attach.HttpPath
|
||||
}
|
||||
li := fmt.Sprintf("<li><a href=\"%s\" target=\"_blank\" title=\"%s\">%s</a></li>", attach.HttpPath, attach.FileName, attach.FileName)
|
||||
|
||||
content.WriteString(li)
|
||||
}
|
||||
content.WriteString("</ul></div>")
|
||||
item.Release += content.String()
|
||||
}
|
||||
_, err = o.Update(item, "release")
|
||||
if err != nil {
|
||||
beego.Error(fmt.Sprintf("发布失败 => %+v", item), err)
|
||||
return err
|
||||
} else {
|
||||
//当文档发布后,需要清除已缓存的转换文档和文档缓存
|
||||
if doc, err := NewDocument().Find(item.DocumentId); err == nil {
|
||||
doc.PutToCache()
|
||||
} else {
|
||||
doc.RemoveCache()
|
||||
}
|
||||
|
||||
if err := os.RemoveAll(filepath.Join(conf.WorkingDirectory, "uploads", "books", strconv.Itoa(bookId))); err != nil {
|
||||
beego.Error("删除已缓存的文档目录失败 => ",filepath.Join(conf.WorkingDirectory, "uploads", "books", strconv.Itoa(bookId)))
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
152
models/Template.go
Normal file
152
models/Template.go
Normal file
@@ -0,0 +1,152 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
"github.com/lifei6671/mindoc/conf"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/astaxie/beego"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Template struct {
|
||||
TemplateId int `orm:"column(template_id);pk;auto;unique;" json:"template_id"`
|
||||
TemplateName string `orm:"column(template_name);size(500);" json:"template_name"`
|
||||
MemberId int `orm:"column(member_id);index" json:"member_id"`
|
||||
BookId int `orm:"column(book_id);index" json:"book_id"`
|
||||
//是否是全局模板:0 否/1 是; 全局模板在所有项目中都可以使用;否则只能在创建模板的项目中使用
|
||||
IsGlobal int `orm:"column(is_global);default(0)" json:"is_global"`
|
||||
TemplateContent string `orm:"column(template_content);type(text);null" json:"template_content"`
|
||||
CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"`
|
||||
ModifyTime time.Time `orm:"column(modify_time);type(datetime);auto_now" json:"modify_time"`
|
||||
ModifyAt int `orm:"column(modify_at);type(int)" json:"-"`
|
||||
Version int64 `orm:"type(bigint);column(version)" json:"version"`
|
||||
}
|
||||
|
||||
// TableName 获取对应数据库表名.
|
||||
func (m *Template) TableName() string {
|
||||
return "templates"
|
||||
}
|
||||
|
||||
// TableEngine 获取数据使用的引擎.
|
||||
func (m *Template) TableEngine() string {
|
||||
return "INNODB"
|
||||
}
|
||||
|
||||
func (m *Template) TableNameWithPrefix() string {
|
||||
return conf.GetDatabasePrefix() + m.TableName()
|
||||
}
|
||||
|
||||
func NewTemplate() *Template {
|
||||
return &Template{}
|
||||
}
|
||||
|
||||
//查询指定ID的模板
|
||||
func (t *Template) Find(templateId int) (*Template,error) {
|
||||
if templateId <= 0 {
|
||||
return t, ErrInvalidParameter
|
||||
}
|
||||
|
||||
o := orm.NewOrm()
|
||||
|
||||
err := o.QueryTable(t.TableNameWithPrefix()).Filter("template_id",templateId).One(t)
|
||||
|
||||
if err != nil {
|
||||
logs.Error("查询模板时失败 ->%s",err)
|
||||
}
|
||||
return t,err
|
||||
}
|
||||
|
||||
//查询属于指定项目的模板.
|
||||
func (t *Template) FindByBookId(bookId int) ([]*Template,error) {
|
||||
if bookId <= 0 {
|
||||
return nil,ErrInvalidParameter
|
||||
}
|
||||
o := orm.NewOrm()
|
||||
|
||||
var templateList []*Template
|
||||
|
||||
_,err := o.QueryTable(t.TableNameWithPrefix()).Filter("book_id",bookId).OrderBy("-template_id").All(&templateList)
|
||||
|
||||
if err != nil {
|
||||
beego.Error("查询模板列表失败 ->",err)
|
||||
}
|
||||
return templateList,err
|
||||
}
|
||||
|
||||
//查询指定项目所有可用模板列表.
|
||||
func (t *Template) FindAllByBookId(bookId int) ([]*Template,error) {
|
||||
if bookId <= 0 {
|
||||
return nil,ErrInvalidParameter
|
||||
}
|
||||
o := orm.NewOrm()
|
||||
|
||||
cond := orm.NewCondition()
|
||||
|
||||
cond1 := cond.And("book_id",bookId).Or("is_global",1)
|
||||
|
||||
qs := o.QueryTable(t.TableNameWithPrefix())
|
||||
|
||||
var templateList []*Template
|
||||
|
||||
_,err := qs.SetCond(cond1).OrderBy("-template_id").All(&templateList)
|
||||
|
||||
if err != nil {
|
||||
beego.Error("查询模板列表失败 ->",err)
|
||||
}
|
||||
return templateList,err
|
||||
}
|
||||
//删除一个模板
|
||||
func (t *Template) Delete(templateId int,memberId int) error {
|
||||
if templateId <= 0 {
|
||||
return ErrInvalidParameter
|
||||
}
|
||||
|
||||
o := orm.NewOrm()
|
||||
|
||||
qs := o.QueryTable(t.TableNameWithPrefix()).Filter("template_id",templateId)
|
||||
|
||||
if memberId > 0 {
|
||||
qs = qs.Filter("member_id",memberId)
|
||||
}
|
||||
_,err := qs.Delete()
|
||||
|
||||
if err != nil {
|
||||
beego.Error("删除模板失败 ->",err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
//添加或更新模板
|
||||
func (t *Template) Save(cols ...string) (err error) {
|
||||
|
||||
if t.BookId <= 0 {
|
||||
return ErrInvalidParameter
|
||||
}
|
||||
o := orm.NewOrm()
|
||||
|
||||
if !o.QueryTable(NewBook()).Filter("book_id",t.BookId).Exist() {
|
||||
return errors.New("项目不存在")
|
||||
}
|
||||
if !o.QueryTable(NewMember()).Filter("member_id",t.MemberId).Filter("status",0).Exist() {
|
||||
return errors.New("用户已被禁用")
|
||||
}
|
||||
if t.TemplateId > 0 {
|
||||
t.Version = time.Now().Unix()
|
||||
t.ModifyTime = time.Now()
|
||||
_,err = o.Update(t,cols...)
|
||||
}else{
|
||||
t.CreateTime = time.Now()
|
||||
_,err = o.Insert(t)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user