From 96ba414dd8865c8e95b579489921cfab31579d9d Mon Sep 17 00:00:00 2001 From: lifei6671 Date: Fri, 13 Jul 2018 19:04:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=8D=95=E9=A1=B5=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/command.go | 1 + controllers/BlogController.go | 75 ++++- models/{Blogs.go => Blog.go} | 26 +- models/BlogResult.go | 6 + models/DocumentModel.go | 6 + routers/router.go | 9 + views/blog/index.tpl | 1 + views/blog/list.tpl | 0 views/blog/manage_edit.tpl | 0 views/blog/manage_list.tpl | 510 ++++++++++++++++++++++++++++++++++ views/blog/manage_setting.tpl | 0 views/book/index.tpl | 3 +- views/widgets/header.tpl | 9 + 13 files changed, 642 insertions(+), 4 deletions(-) rename models/{Blogs.go => Blog.go} (83%) create mode 100644 models/BlogResult.go create mode 100644 views/blog/index.tpl create mode 100644 views/blog/list.tpl create mode 100644 views/blog/manage_edit.tpl create mode 100644 views/blog/manage_list.tpl create mode 100644 views/blog/manage_setting.tpl diff --git a/commands/command.go b/commands/command.go index 987de8aa..fb699997 100644 --- a/commands/command.go +++ b/commands/command.go @@ -89,6 +89,7 @@ func RegisterModel() { new(models.DocumentHistory), new(models.Migration), new(models.Label), + new(models.Blog), ) //migrate.RegisterMigration() } diff --git a/controllers/BlogController.go b/controllers/BlogController.go index 21182808..08ac13cb 100644 --- a/controllers/BlogController.go +++ b/controllers/BlogController.go @@ -1,5 +1,9 @@ package controllers +import ( + "strings" + "github.com/lifei6671/mindoc/models" +) type BlogController struct{ BaseController @@ -8,24 +12,93 @@ type BlogController struct{ func (c *BlogController) Index() { c.Prepare() - + c.TplName = "blog/index.tpl" } func (c *BlogController) List() { c.Prepare() + c.TplName = "blog/list.tpl" + } //管理后台 func (c *BlogController) ManageList() { c.Prepare() + c.TplName = "blog/manage_list.tpl" } //文章设置 func (c *BlogController) ManageSetting() { c.Prepare() + c.TplName = "blog/manage_setting.tpl" + //如果是post请求 + if c.Ctx.Input.IsPost() { + blogId,_ := c.GetInt("id",0) + blogTitle := c.GetString("title") + blogIdentify := c.GetString("identify") + orderIndex,_ := c.GetInt("order_index",0) + blogType,_ := c.GetInt("blog_type",0) + documentId,_ := c.GetInt("document_id",0) + + if blogTitle == "" { + c.JsonResult(6001,"文章标题不能为空") + } + if blogType != 0 && blogType != 1 { + c.JsonResult(6005,"未知的文章类型") + }else if documentId <= 0 && blogType == 1 { + c.JsonResult(6006,"请选择链接的文章") + }else if blogType == 1 && documentId > 0 && !models.NewDocument().IsExist(documentId){ + c.JsonResult(6007,"链接的文章不存在") + } + if strings.Count(blogTitle,"") > 200 { + c.JsonResult(6002,"文章标题不能大于200个字符") + } + var blog *models.Blog + var err error + //如果文章ID存在,则从数据库中查询文章 + if blogId > 0 { + if c.Member.IsAdministrator() { + blog, err = models.NewBlog().Find(blogId) + } else { + blog, err = models.NewBlog().FindByIdAndMemberId(blogId, c.Member.MemberId) + } + + if err != nil { + c.JsonResult(6003, "文章不存在") + } + //如果设置了文章标识 + if blogIdentify != "" { + //如果查询到的文章标识存在并且不是当前文章的id + if b,err := models.NewBlog().FindByIdentify(blogIdentify); err == nil && b.BlogId != blogId { + c.JsonResult(6004,"文章标识已存在") + } + } + }else{ + //如果设置了文章标识 + if blogIdentify != "" { + if models.NewBlog().IsExist(blogIdentify) { + c.JsonResult(6004,"文章标识已存在") + } + } + + blog = models.NewBlog() + blog.MemberId = c.Member.MemberId + } + + + blog.BlogTitle = blogTitle + blog.BlogIdentify = blogIdentify + blog.OrderIndex = orderIndex + blog.BlogType = blogType + if blogType == 1 { + blog.DocumentId = documentId + } + + } } //文章创建或编辑 func (c *BlogController) ManageEdit() { c.Prepare() + c.TplName = "blog/manage_edit.tpl" } diff --git a/models/Blogs.go b/models/Blog.go similarity index 83% rename from models/Blogs.go rename to models/Blog.go index 0c9e1703..405ea52c 100644 --- a/models/Blogs.go +++ b/models/Blog.go @@ -17,7 +17,7 @@ type Blog struct { //排序序号 OrderIndex int `orm:"column(order_index);type(int);default(0)" json:"order_index"` //所属用户 - MemberId int `orm:"column(member_id);type(int);default(0)" json:"member_id"` + MemberId int `orm:"column(member_id);type(int);default(0):index" json:"member_id"` //文章类型:0 普通文章/1 链接文章 BlogType int `orm:"column(blog_type);type(int);default(0)" json:"blog_type"` //链接到的项目中的文档ID @@ -80,6 +80,18 @@ func (b *Blog) Find(blogId int) (*Blog,error) { return b,nil } +//查找指定用户的指定文章 +func (b *Blog) FindByIdAndMemberId(blogId,memberId int) (*Blog,error) { + o := orm.NewOrm() + + err := o.QueryTable(b.TableNameWithPrefix()).Filter("blog_id",blogId).Filter("member_id",memberId).One(b) + if err != nil { + beego.Error("查询文章时失败 -> ",err) + return nil,err + } + + return b,nil +} //根据文章标识查询文章 func (b *Blog) FindByIdentify(identify string) (*Blog,error) { o := orm.NewOrm() @@ -91,7 +103,7 @@ func (b *Blog) FindByIdentify(identify string) (*Blog,error) { } return b,nil } - +//获取指定文章的链接内容 func (b *Blog)Link() (*Blog,error) { o := orm.NewOrm() //如果是链接文章,则需要从链接的项目中查找文章内容 @@ -109,3 +121,13 @@ func (b *Blog)Link() (*Blog,error) { return b,nil } +//判断指定的文章标识是否存在 +func (b *Blog) IsExist(identify string) bool { + o := orm.NewOrm() + + return o.QueryTable(b.TableNameWithPrefix()).Filter("blog_identify",identify).Exist() +} + +func (b *Blog) FindToPager() { + +} diff --git a/models/BlogResult.go b/models/BlogResult.go new file mode 100644 index 00000000..b7be9fe9 --- /dev/null +++ b/models/BlogResult.go @@ -0,0 +1,6 @@ +package models + + +type BlogResult struct{ + +} \ No newline at end of file diff --git a/models/DocumentModel.go b/models/DocumentModel.go index fa8ac688..6768c7ff 100644 --- a/models/DocumentModel.go +++ b/models/DocumentModel.go @@ -220,3 +220,9 @@ func (m *Document) FindListByBookId(bookId int) (docs []*Document, err error) { return } +//判断文章是否存在 +func (m *Document) IsExist(documentId int) bool { + o := orm.NewOrm() + + return o.QueryTable(m.TableNameWithPrefix()).Filter("document_id",documentId).Exist() +} \ No newline at end of file diff --git a/routers/router.go b/routers/router.go index 1450d652..5f20c2ee 100644 --- a/routers/router.go +++ b/routers/router.go @@ -63,6 +63,15 @@ func init() { beego.Router("/book/setting/token", &controllers.BookController{}, "post:CreateToken") beego.Router("/book/setting/delete", &controllers.BookController{}, "post:Delete") + //管理文章的路由 + beego.Router("/blogs", &controllers.BlogController{},"*:ManageList") + beego.Router("/blogs/setting/:id", &controllers.BlogController{}, "*:ManageSetting") + beego.Router("/blogs/edit/:id",&controllers.BlogController{}, "*:ManageEdit") + + //读文章的路由 + beego.Router("/blog", &controllers.BlogController{}, "*:List") + beego.Router("/blog/:id",&controllers.BlogController{}, "*:Index") + beego.Router("/api/attach/remove/", &controllers.DocumentController{}, "post:RemoveAttachment") beego.Router("/api/:key/edit/?:id", &controllers.DocumentController{}, "*:Edit") beego.Router("/api/upload", &controllers.DocumentController{}, "post:Upload") diff --git a/views/blog/index.tpl b/views/blog/index.tpl new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/views/blog/index.tpl @@ -0,0 +1 @@ + diff --git a/views/blog/list.tpl b/views/blog/list.tpl new file mode 100644 index 00000000..e69de29b diff --git a/views/blog/manage_edit.tpl b/views/blog/manage_edit.tpl new file mode 100644 index 00000000..e69de29b diff --git a/views/blog/manage_list.tpl b/views/blog/manage_list.tpl new file mode 100644 index 00000000..cd678dea --- /dev/null +++ b/views/blog/manage_list.tpl @@ -0,0 +1,510 @@ + + + + + + + + 我的文章 - Powered by MinDoc + + + + + + + + + + + + +
+{{template "widgets/header.tpl" .}} +
+
+ +
+
+
+ 文章列表 +   + +
+
+
+
+ + +
+ +
+
+
+
+{{template "widgets/footer.tpl" .}} +
+ + + + + + + + + + + + + + + + + + diff --git a/views/blog/manage_setting.tpl b/views/blog/manage_setting.tpl new file mode 100644 index 00000000..e69de29b diff --git a/views/book/index.tpl b/views/book/index.tpl index b477e8ab..184fd14f 100644 --- a/views/book/index.tpl +++ b/views/book/index.tpl @@ -27,7 +27,8 @@
diff --git a/views/widgets/header.tpl b/views/widgets/header.tpl index dcb17313..a77b6d74 100644 --- a/views/widgets/header.tpl +++ b/views/widgets/header.tpl @@ -13,6 +13,9 @@
  • 首页
  • +
  • + 文章 +
  • 标签
  • @@ -37,6 +40,9 @@
  • 我的项目
  • +
  • + 我的文章 +
  • {{if eq .Member.Role 0 }}
  • 管理后台 @@ -72,6 +78,9 @@
  • 我的项目
  • +
  • + 我的文章 +
  • {{if eq .Member.Role 0 1}}
  • 管理后台