mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-10-25 10:59:13 +08:00
feat:实现自定义模板功能
This commit is contained in:
@@ -197,6 +197,9 @@ func (c *BookController) SaveBook() {
|
||||
bookResult.Description = description
|
||||
bookResult.CommentStatus = commentStatus
|
||||
bookResult.Label = tag
|
||||
|
||||
beego.Info("用户 [",c.Member.Account ,"] 修改了项目 ->",book)
|
||||
|
||||
c.JsonResult(0, "ok", bookResult)
|
||||
}
|
||||
|
||||
@@ -238,6 +241,7 @@ func (c *BookController) PrivatelyOwned() {
|
||||
logs.Error("PrivatelyOwned => ", err)
|
||||
c.JsonResult(6004, "保存失败")
|
||||
}
|
||||
beego.Info("用户 【",c.Member.Account,"]修改了项目权限 ->", state)
|
||||
c.JsonResult(0, "ok")
|
||||
}
|
||||
|
||||
@@ -344,7 +348,7 @@ func (c *BookController) UploadCover() {
|
||||
filePath = filepath.Join(conf.WorkingDirectory, "uploads", time.Now().Format("200601"), fileName+"_small"+ext)
|
||||
|
||||
//生成缩略图并保存到磁盘
|
||||
err = graphics.ImageResizeSaveFile(subImg, 175, 230, filePath)
|
||||
err = graphics.ImageResizeSaveFile(subImg, 350, 460, filePath)
|
||||
|
||||
if err != nil {
|
||||
logs.Error("ImageResizeSaveFile => ", err.Error())
|
||||
@@ -368,6 +372,7 @@ func (c *BookController) UploadCover() {
|
||||
if oldCover != conf.GetDefaultCover() {
|
||||
os.Remove("." + oldCover)
|
||||
}
|
||||
beego.Info("用户[",c.Member.Account,"]上传了项目封面 ->",book.BookName,book.BookId,book.Cover)
|
||||
|
||||
c.JsonResult(0, "ok", url)
|
||||
}
|
||||
@@ -473,7 +478,7 @@ func (c *BookController) Create() {
|
||||
}
|
||||
}
|
||||
|
||||
if books, _ := book.FindByField("identify", identify,"book_id"); len(books) > 0 {
|
||||
if books, _ := book.FindByField("identify", identify, "book_id"); len(books) > 0 {
|
||||
c.JsonResult(6006, "项目标识已存在")
|
||||
}
|
||||
|
||||
@@ -505,32 +510,34 @@ func (c *BookController) Create() {
|
||||
beego.Error(err)
|
||||
}
|
||||
|
||||
beego.Info("用户[",c.Member.Account,"]创建了项目 ->",book)
|
||||
c.JsonResult(0, "ok", bookResult)
|
||||
}
|
||||
c.JsonResult(6001, "error")
|
||||
}
|
||||
|
||||
//复制项目
|
||||
func (c *BookController) Copy(){
|
||||
func (c *BookController) Copy() {
|
||||
if c.Ctx.Input.IsPost() {
|
||||
//检查是否有复制项目的权限
|
||||
if _,err := c.IsPermission(); err != nil{
|
||||
c.JsonResult(500,err.Error())
|
||||
if _, err := c.IsPermission(); err != nil {
|
||||
c.JsonResult(500, err.Error())
|
||||
}
|
||||
|
||||
identify := strings.TrimSpace(c.GetString("identify", ""))
|
||||
if identify == "" {
|
||||
c.JsonResult(6001,"参数错误")
|
||||
c.JsonResult(6001, "参数错误")
|
||||
}
|
||||
book := models.NewBook()
|
||||
err := book.Copy(identify)
|
||||
if err != nil {
|
||||
c.JsonResult(6002,"复制项目出错")
|
||||
}else{
|
||||
c.JsonResult(6002, "复制项目出错")
|
||||
} else {
|
||||
bookResult, err := models.NewBookResult().FindByIdentify(book.Identify, c.Member.MemberId)
|
||||
if err != nil {
|
||||
beego.Error("查询失败")
|
||||
}
|
||||
c.JsonResult(0,"ok",bookResult)
|
||||
c.JsonResult(0, "ok", bookResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -572,7 +579,7 @@ func (c *BookController) Import() {
|
||||
c.JsonResult(6004, "不支持的文件类型")
|
||||
}
|
||||
|
||||
if books, _ := models.NewBook().FindByField("identify", identify,"book_id"); len(books) > 0 {
|
||||
if books, _ := models.NewBook().FindByField("identify", identify, "book_id"); len(books) > 0 {
|
||||
c.JsonResult(6006, "项目标识已存在")
|
||||
}
|
||||
|
||||
@@ -602,9 +609,10 @@ func (c *BookController) Import() {
|
||||
book.Editor = "markdown"
|
||||
book.Theme = "default"
|
||||
|
||||
|
||||
go book.ImportBook(tempPath)
|
||||
|
||||
beego.Info("用户[",c.Member.Account,"]导入了项目 ->",book)
|
||||
|
||||
c.JsonResult(0, "项目正在后台转换中,请稍后查看")
|
||||
}
|
||||
|
||||
@@ -640,6 +648,7 @@ func (c *BookController) CreateToken() {
|
||||
logs.Error("生成阅读令牌失败 => ", err)
|
||||
c.JsonResult(6003, "生成阅读令牌失败")
|
||||
}
|
||||
beego.Info("用户[",c.Member.Account,"]创建项目令牌 ->",book.PrivateToken)
|
||||
c.JsonResult(0, "ok", conf.URLFor("DocumentController.Index", ":key", book.Identify, "token", book.PrivateToken))
|
||||
} else {
|
||||
book.PrivateToken = ""
|
||||
@@ -647,6 +656,7 @@ func (c *BookController) CreateToken() {
|
||||
logs.Error("CreateToken => ", err)
|
||||
c.JsonResult(6004, "删除令牌失败")
|
||||
}
|
||||
beego.Info("用户[",c.Member.Account,"]创建项目令牌 ->",book.PrivateToken)
|
||||
c.JsonResult(0, "ok", "")
|
||||
}
|
||||
}
|
||||
@@ -673,6 +683,7 @@ func (c *BookController) Delete() {
|
||||
logs.Error("删除项目 => ", err)
|
||||
c.JsonResult(6003, "删除失败")
|
||||
}
|
||||
beego.Info("用户[",c.Member.Account,"]删除了项目 ->",bookResult)
|
||||
c.JsonResult(0, "ok")
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/lifei6671/mindoc/utils/filetil"
|
||||
"github.com/lifei6671/mindoc/utils/gopool"
|
||||
"github.com/astaxie/beego/logs"
|
||||
)
|
||||
|
||||
// DocumentController struct
|
||||
@@ -797,10 +798,12 @@ func (c *DocumentController) Content() {
|
||||
|
||||
//如果启用了自动发布
|
||||
if autoRelease {
|
||||
go func(identify string) {
|
||||
models.NewBook().ReleaseContent(bookId)
|
||||
|
||||
}(identify)
|
||||
go func() {
|
||||
err := doc.ReleaseContent()
|
||||
if err == nil {
|
||||
logs.Informational("文档自动发布成功 -> document_id=%d;document_name=%s",doc.DocumentId, doc.DocumentName)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
c.JsonResult(0, "ok", doc)
|
||||
@@ -1021,7 +1024,7 @@ func (c *DocumentController) History() {
|
||||
if c.Member.IsAdministrator() {
|
||||
book, err := models.NewBook().FindByFieldFirst("identify", identify)
|
||||
if err != nil {
|
||||
beego.Error("FindByIdentify => ", err)
|
||||
beego.Error("查找项目失败 ->", err)
|
||||
c.Data["ErrorMessage"] = "项目不存在或权限不足"
|
||||
return
|
||||
}
|
||||
@@ -1031,7 +1034,7 @@ func (c *DocumentController) History() {
|
||||
} else {
|
||||
bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
|
||||
if err != nil || bookResult.RoleId == conf.BookObserver {
|
||||
beego.Error("FindByIdentify => ", err)
|
||||
beego.Error("查找项目失败 ->", err)
|
||||
c.Data["ErrorMessage"] = "项目不存在或权限不足"
|
||||
return
|
||||
}
|
||||
@@ -1060,7 +1063,7 @@ func (c *DocumentController) History() {
|
||||
|
||||
historis, totalCount, err := models.NewDocumentHistory().FindToPager(docId, pageIndex, conf.PageSize)
|
||||
if err != nil {
|
||||
beego.Error("FindToPager => ", err)
|
||||
beego.Error("分页查找文档历史失败 ->", err)
|
||||
c.Data["ErrorMessage"] = "获取历史失败"
|
||||
return
|
||||
}
|
||||
@@ -1094,7 +1097,7 @@ func (c *DocumentController) DeleteHistory() {
|
||||
if c.Member.IsAdministrator() {
|
||||
book, err := models.NewBook().FindByFieldFirst("identify", identify)
|
||||
if err != nil {
|
||||
beego.Error("FindByIdentify => ", err)
|
||||
beego.Error("查找项目失败 ->", err)
|
||||
c.JsonResult(6002, "项目不存在或权限不足")
|
||||
}
|
||||
|
||||
@@ -1102,7 +1105,7 @@ func (c *DocumentController) DeleteHistory() {
|
||||
} else {
|
||||
bookResult, err := models.NewBookResult().FindByIdentify(identify, c.Member.MemberId)
|
||||
if err != nil || bookResult.RoleId == conf.BookObserver {
|
||||
beego.Error("FindByIdentify => ", err)
|
||||
beego.Error("查找项目失败 ->", err)
|
||||
c.JsonResult(6002, "项目不存在或权限不足")
|
||||
}
|
||||
|
||||
|
||||
125
controllers/TemplateController.go
Normal file
125
controllers/TemplateController.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/lifei6671/mindoc/models"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/qiniu/x/errors.v7"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type TemplateController struct {
|
||||
BaseController
|
||||
BookId int
|
||||
}
|
||||
|
||||
func (c *TemplateController) isPermission() (error) {
|
||||
c.Prepare()
|
||||
|
||||
if c.IsAjax() {
|
||||
bookIdentify := c.GetString("identify", "")
|
||||
|
||||
if bookIdentify == "" {
|
||||
return errors.New("参数错误")
|
||||
}
|
||||
|
||||
if !c.Member.IsAdministrator() {
|
||||
book, err := models.NewBookResult().FindByIdentify(bookIdentify, c.Member.MemberId,"book_id")
|
||||
if err != nil {
|
||||
if err == orm.ErrNoRows {
|
||||
return errors.New("项目不存在或没有权限")
|
||||
}
|
||||
return errors.New("查询项目模板失败")
|
||||
}
|
||||
c.BookId = book.BookId
|
||||
}else{
|
||||
book,err := models.NewBook().FindByIdentify(bookIdentify,"book_id")
|
||||
if err != nil {
|
||||
if err == orm.ErrNoRows {
|
||||
return errors.New("项目不存在或没有权限")
|
||||
}
|
||||
return errors.New("查询项目模板失败")
|
||||
}
|
||||
c.BookId = book.BookId
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.New("请求方法不支持")
|
||||
}
|
||||
|
||||
//获取模板列表
|
||||
func (c *TemplateController) List() {
|
||||
if err := c.isPermission() ; err != nil {
|
||||
c.JsonResult(500,err.Error())
|
||||
}
|
||||
|
||||
templateList,err := models.NewTemplate().FindAllByBookId(c.BookId)
|
||||
|
||||
if err != nil {
|
||||
if err == orm.ErrNoRows {
|
||||
c.JsonResult(404,"没有模板")
|
||||
}
|
||||
c.JsonResult(500,"查询项目模板失败")
|
||||
}
|
||||
c.JsonResult(0,"OK",templateList)
|
||||
}
|
||||
|
||||
func (c *TemplateController) Add() {
|
||||
if err := c.isPermission() ; err != nil {
|
||||
c.JsonResult(500,err.Error())
|
||||
}
|
||||
|
||||
templateId, _ := c.GetInt("template_id",0)
|
||||
content := c.GetString("content")
|
||||
isGlobal,_ := c.GetInt("is_global",0)
|
||||
templateName := c.GetString("template_name","")
|
||||
|
||||
if templateName == "" || strings.Count(templateName,"") > 300 {
|
||||
c.JsonResult(500,"模板名称不能为空且必须小于300字")
|
||||
}
|
||||
template := models.NewTemplate()
|
||||
template.TemplateId = templateId
|
||||
template.BookId = c.BookId
|
||||
template.TemplateContent = content
|
||||
template.MemberId = c.Member.MemberId
|
||||
|
||||
if templateId > 0 {
|
||||
template.ModifyAt = c.Member.MemberId
|
||||
}
|
||||
//只有管理员才能设置全局模板
|
||||
if c.Member.IsAdministrator() {
|
||||
template.IsGlobal = isGlobal
|
||||
}else{
|
||||
template.IsGlobal = 0
|
||||
}
|
||||
|
||||
var cols []string
|
||||
|
||||
if templateId > 0 {
|
||||
cols = []string{ "template_content", "modify_time","modify_at","version" }
|
||||
}
|
||||
|
||||
if err := template.Save(cols...); err != nil {
|
||||
c.JsonResult(500,"报错模板失败")
|
||||
}
|
||||
c.JsonResult(0,"OK",template)
|
||||
}
|
||||
|
||||
func (c *TemplateController) Delete() {
|
||||
if err := c.isPermission() ; err != nil {
|
||||
c.JsonResult(500,err.Error())
|
||||
}
|
||||
templateId, _ := c.GetInt("template_id",0)
|
||||
|
||||
if c.Member.IsAdministrator() {
|
||||
err := models.NewTemplate().Delete(templateId,0)
|
||||
if err != nil {
|
||||
c.JsonResult(500,"删除失败")
|
||||
}
|
||||
}else{
|
||||
err := models.NewTemplate().Delete(templateId,c.Member.MemberId)
|
||||
if err != nil {
|
||||
c.JsonResult(500,"删除失败")
|
||||
}
|
||||
}
|
||||
c.JsonResult(0,"OK")
|
||||
}
|
||||
Reference in New Issue
Block a user