From e56210165fb29d6ff2a7af22d858d7993a3c2d1a Mon Sep 17 00:00:00 2001 From: Minho Date: Fri, 23 Mar 2018 10:00:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=AE=BE=E7=BD=AE=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E7=AF=87=E6=96=87=E7=AB=A0=E4=B8=BA=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E6=89=93=E5=BC=80=E7=9A=84=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/command.go | 3 + controllers/book.go | 6 + controllers/document.go | 174 ++++++++++++++------------ models/book.go | 2 + models/book_result.go | 19 ++- models/document.go | 5 +- views/account/find_password_setp1.tpl | 2 +- views/account/find_password_setp2.tpl | 2 +- views/account/login.tpl | 2 +- views/account/register.tpl | 2 +- views/book/dashboard.tpl | 4 +- views/book/setting.tpl | 14 ++- views/document/compare.tpl | 2 +- views/document/history.tpl | 4 +- views/errors/403.tpl | 2 +- views/errors/404.tpl | 2 +- views/errors/error.tpl | 2 +- views/manager/attach_detailed.tpl | 4 +- views/manager/books.tpl | 2 +- views/search/index.tpl | 4 +- 20 files changed, 151 insertions(+), 106 deletions(-) diff --git a/commands/command.go b/commands/command.go index 1723e110..be133d2f 100644 --- a/commands/command.go +++ b/commands/command.go @@ -173,6 +173,9 @@ func RegisterFunction() { beego.AddFuncMap("cdnimg", conf.URLForWithCdnImage) //重写url生成,支持配置域名以及域名前缀 beego.AddFuncMap("urlfor", conf.URLFor) + beego.AddFuncMap("date_format", func(t time.Time,format string) string { + return t.Local().Format(format) + }) } //解析命令 diff --git a/controllers/book.go b/controllers/book.go index 25329dea..a5637c23 100644 --- a/controllers/book.go +++ b/controllers/book.go @@ -143,6 +143,7 @@ func (c *BookController) SaveBook() { historyCount,_ := c.GetInt("history_count",0) isDownload := strings.TrimSpace(c.GetString("is_download")) == "on" enableShare := strings.TrimSpace(c.GetString("enable_share")) == "on" + isUseFirstDocument := strings.TrimSpace(c.GetString("is_use_first_document")) == "on" if strings.Count(description, "") > 500 { c.JsonResult(6004, "项目描述不能大于500字") @@ -184,6 +185,11 @@ func (c *BookController) SaveBook() { }else{ book.IsEnableShare = 1 } + if isUseFirstDocument { + book.IsUseFirstDocument = 1 + }else{ + book.IsUseFirstDocument = 0 + } if err := book.Update(); err != nil { c.JsonResult(6006, "保存失败") } diff --git a/controllers/document.go b/controllers/document.go index 116d4582..07f97cc6 100644 --- a/controllers/document.go +++ b/controllers/document.go @@ -34,81 +34,6 @@ type DocumentController struct { BaseController } -// 判断用户是否可以阅读文档 -func isReadable(identify, token string, c *DocumentController) *models.BookResult { - book, err := models.NewBook().FindByFieldFirst("identify", identify) - - if err != nil { - beego.Error(err) - c.Abort("500") - } - - // 如果文档是私有的 - if book.PrivatelyOwned == 1 && !c.Member.IsAdministrator() { - is_ok := false - - if c.Member != nil { - _, err := models.NewRelationship().FindForRoleId(book.BookId, c.Member.MemberId) - if err == nil { - is_ok = true - } - } - - if book.PrivateToken != "" && !is_ok { - // 如果有访问的 Token,并且该项目设置了访问 Token,并且和用户提供的相匹配,则记录到 Session 中。 - // 如果用户未提供 Token 且用户登录了,则判断用户是否参与了该项目。 - // 如果用户未登录,则从 Session 中读取 Token。 - if token != "" && strings.EqualFold(token, book.PrivateToken) { - c.SetSession(identify, token) - } else if token, ok := c.GetSession(identify).(string); !ok || !strings.EqualFold(token, book.PrivateToken) { - c.Abort("403") - } - } else if !is_ok { - c.Abort("403") - } - } - - bookResult := models.NewBookResult().ToBookResult(*book) - - if c.Member != nil { - rel, err := models.NewRelationship().FindByBookIdAndMemberId(bookResult.BookId, c.Member.MemberId) - - if err == nil { - bookResult.MemberId = rel.MemberId - bookResult.RoleId = rel.RoleId - bookResult.RelationshipId = rel.RelationshipId - } - } - - // 判断是否需要显示评论框 - if bookResult.CommentStatus == "closed" { - bookResult.IsDisplayComment = false - } else if bookResult.CommentStatus == "open" { - bookResult.IsDisplayComment = true - } else if bookResult.CommentStatus == "group_only" { - bookResult.IsDisplayComment = bookResult.RelationshipId > 0 - } else if bookResult.CommentStatus == "registered_only" { - bookResult.IsDisplayComment = true - } - - return bookResult -} - -func isUserLoggedIn(c *DocumentController) bool { - return c.Member != nil && c.Member.MemberId > 0 -} - -func promptUserToLogIn(c *DocumentController) { - beego.Info("Access " + c.Ctx.Request.URL.RequestURI() + " not permitted.") - beego.Info(" Access will be redirected to login page(SessionId: " + c.CruSession.SessionID() + ").") - - if c.IsAjax() { - c.JsonResult(6000, "请重新登录。") - } else { - c.Redirect(conf.URLFor("AccountController.Login")+ "?url=" + url.PathEscape(conf.BaseUrl+ c.Ctx.Request.URL.RequestURI()), 302) - } -} - // 文档首页 func (c *DocumentController) Index() { c.Prepare() @@ -130,7 +55,25 @@ func (c *DocumentController) Index() { c.TplName = "document/" + bookResult.Theme + "_read.tpl" - tree, err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId, 0) + selected := 0 + + if bookResult.IsUseFirstDocument { + doc,err := bookResult.FindFirstDocumentByBookId(bookResult.BookId) + if err == nil { + if strings.TrimSpace(doc.Release) != "" { + doc.Release += "
文档更新时间: " + doc.ModifyTime.Local().Format("2006-01-02 15:04") + "
"; + } + selected = doc.DocumentId + c.Data["Title"] = doc.DocumentName + c.Data["Content"] = template.HTML(doc.Release) + + } + }else { + c.Data["Title"] = "概要" + c.Data["Content"] = template.HTML(blackfriday.Run([]byte(bookResult.Description))) + } + + tree, err := models.NewDocument().CreateDocumentTreeForHtml(bookResult.BookId, selected) if err != nil { beego.Error(err) @@ -139,8 +82,7 @@ func (c *DocumentController) Index() { c.Data["Model"] = bookResult c.Data["Result"] = template.HTML(tree) - c.Data["Title"] = "概要" - c.Data["Content"] = template.HTML(blackfriday.Run([]byte(bookResult.Description))) + } // 阅读文档 @@ -1321,3 +1263,79 @@ func EachFun(prefix, dpath string, c *DocumentController, book *models.BookResul f.WriteString(html) f.Close() } + + +// 判断用户是否可以阅读文档 +func isReadable(identify, token string, c *DocumentController) *models.BookResult { + book, err := models.NewBook().FindByFieldFirst("identify", identify) + + if err != nil { + beego.Error(err) + c.Abort("500") + } + + // 如果文档是私有的 + if book.PrivatelyOwned == 1 && !c.Member.IsAdministrator() { + is_ok := false + + if c.Member != nil { + _, err := models.NewRelationship().FindForRoleId(book.BookId, c.Member.MemberId) + if err == nil { + is_ok = true + } + } + + if book.PrivateToken != "" && !is_ok { + // 如果有访问的 Token,并且该项目设置了访问 Token,并且和用户提供的相匹配,则记录到 Session 中。 + // 如果用户未提供 Token 且用户登录了,则判断用户是否参与了该项目。 + // 如果用户未登录,则从 Session 中读取 Token。 + if token != "" && strings.EqualFold(token, book.PrivateToken) { + c.SetSession(identify, token) + } else if token, ok := c.GetSession(identify).(string); !ok || !strings.EqualFold(token, book.PrivateToken) { + c.Abort("403") + } + } else if !is_ok { + c.Abort("403") + } + } + + bookResult := models.NewBookResult().ToBookResult(*book) + + if c.Member != nil { + rel, err := models.NewRelationship().FindByBookIdAndMemberId(bookResult.BookId, c.Member.MemberId) + + if err == nil { + bookResult.MemberId = rel.MemberId + bookResult.RoleId = rel.RoleId + bookResult.RelationshipId = rel.RelationshipId + } + } + + // 判断是否需要显示评论框 + if bookResult.CommentStatus == "closed" { + bookResult.IsDisplayComment = false + } else if bookResult.CommentStatus == "open" { + bookResult.IsDisplayComment = true + } else if bookResult.CommentStatus == "group_only" { + bookResult.IsDisplayComment = bookResult.RelationshipId > 0 + } else if bookResult.CommentStatus == "registered_only" { + bookResult.IsDisplayComment = true + } + + return bookResult +} + +func isUserLoggedIn(c *DocumentController) bool { + return c.Member != nil && c.Member.MemberId > 0 +} + +func promptUserToLogIn(c *DocumentController) { + beego.Info("Access " + c.Ctx.Request.URL.RequestURI() + " not permitted.") + beego.Info(" Access will be redirected to login page(SessionId: " + c.CruSession.SessionID() + ").") + + if c.IsAjax() { + c.JsonResult(6000, "请重新登录。") + } else { + c.Redirect(conf.URLFor("AccountController.Login")+ "?url=" + url.PathEscape(conf.BaseUrl+ c.Ctx.Request.URL.RequestURI()), 302) + } +} diff --git a/models/book.go b/models/book.go index ad5fa3d1..e93ed336 100644 --- a/models/book.go +++ b/models/book.go @@ -56,6 +56,8 @@ type Book struct { MemberId int `orm:"column(member_id);size(100)" json:"member_id"` ModifyTime time.Time `orm:"type(datetime);column(modify_time);null;auto_now" json:"modify_time"` Version int64 `orm:"type(bigint);column(version)" json:"version"` + //是否使用第一篇文章项目为默认首页,0 否/1 是 + IsUseFirstDocument int `orm:"column(is_use_first_document);type(int);default(0)" json:"is_use_first_document"` } // TableName 获取对应数据库表名. diff --git a/models/book_result.go b/models/book_result.go index 96a87c07..3feceb93 100644 --- a/models/book_result.go +++ b/models/book_result.go @@ -51,6 +51,7 @@ type BookResult struct { RoleName string `json:"role_name"` Status int `json:"status"` IsEnableShare bool `json:"is_enable_share"` + IsUseFirstDocument bool `json:"is_use_first_document"` LastModifyText string `json:"last_modify_text"` IsDisplayComment bool `json:"is_display_comment"` @@ -168,8 +169,8 @@ func (m *BookResult) ToBookResult(book Book) *BookResult { m.DocCount = book.DocCount m.CommentStatus = book.CommentStatus m.CommentCount = book.CommentCount - m.CreateTime = book.CreateTime.Local() - m.ModifyTime = book.ModifyTime.Local() + m.CreateTime = book.CreateTime + m.ModifyTime = book.ModifyTime m.Cover = book.Cover m.Label = book.Label m.Status = book.Status @@ -177,6 +178,7 @@ func (m *BookResult) ToBookResult(book Book) *BookResult { m.Theme = book.Theme m.AutoRelease = book.AutoRelease == 1 m.IsEnableShare = book.IsEnableShare == 0 + m.IsUseFirstDocument = book.IsUseFirstDocument == 1 m.Publisher = book.Publisher m.HistoryCount = book.HistoryCount m.IsDownload = book.IsDownload == 0 @@ -465,8 +467,17 @@ func exportMarkdown(p string,parentId int,bookId int) (error){ return nil } - - +//查询项目的第一篇文档 +func (m *BookResult) FindFirstDocumentByBookId(bookId int) (*Document,error) { + + o := orm.NewOrm() + + doc := NewDocument() + + err := o.QueryTable(doc.TableNameWithPrefix()).Filter("book_id", bookId).Filter("parent_id",0).OrderBy("order_sort").One(doc) + + return doc,err +} diff --git a/models/document.go b/models/document.go index dd1bff3b..420eac58 100644 --- a/models/document.go +++ b/models/document.go @@ -33,7 +33,7 @@ type Document struct { Content string `orm:"column(content);type(text);null" json:"content"` CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"` MemberId int `orm:"column(member_id);type(int)" json:"member_id"` - ModifyTime time.Time `orm:"column(modify_time);type(datetime)" json:"modify_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"` AttachList []*Attachment `orm:"-" json:"attach"` @@ -81,11 +81,8 @@ func (m *Document) InsertOrUpdate(cols ...string) error { o := orm.NewOrm() var err error if m.DocumentId > 0 { - m.ModifyTime = time.Now().Local() _, err = o.Update(m) } else { - m.ModifyTime = time.Now().Local() - m.CreateTime = time.Now().Local() _, err = o.Insert(m) NewBook().ResetDocumentNumber(m.BookId) } diff --git a/views/account/find_password_setp1.tpl b/views/account/find_password_setp1.tpl index 78c733de..a469e869 100644 --- a/views/account/find_password_setp1.tpl +++ b/views/account/find_password_setp1.tpl @@ -2,7 +2,7 @@ - + diff --git a/views/account/find_password_setp2.tpl b/views/account/find_password_setp2.tpl index bf777ee1..6cca864c 100644 --- a/views/account/find_password_setp2.tpl +++ b/views/account/find_password_setp2.tpl @@ -2,7 +2,7 @@ - + diff --git a/views/account/login.tpl b/views/account/login.tpl index 55714c3b..671c70df 100644 --- a/views/account/login.tpl +++ b/views/account/login.tpl @@ -2,7 +2,7 @@ - + diff --git a/views/account/register.tpl b/views/account/register.tpl index c3923a15..cc2ca908 100644 --- a/views/account/register.tpl +++ b/views/account/register.tpl @@ -2,7 +2,7 @@ - + diff --git a/views/book/dashboard.tpl b/views/book/dashboard.tpl index 3ef80545..2fa850be 100644 --- a/views/book/dashboard.tpl +++ b/views/book/dashboard.tpl @@ -73,11 +73,11 @@
创建时间: - {{dateformat .Model.CreateTime "2006-01-02 15:04:05"}} + {{date_format .Model.CreateTime "2006-01-02 15:04:05"}}
修改时间: - {{dateformat .Model.CreateTime "2006-01-02 15:04:05"}} + {{date_format .Model.CreateTime "2006-01-02 15:04:05"}}
担任角色: diff --git a/views/book/setting.tpl b/views/book/setting.tpl index 5e7e06e3..162977b2 100644 --- a/views/book/setting.tpl +++ b/views/book/setting.tpl @@ -142,7 +142,7 @@
- +
@@ -150,7 +150,15 @@
- + +
+
+ +
+ +
+
+
@@ -317,7 +325,7 @@ }).on("show.bs.modal",function () { window.modalHtml = $("#upload-logo-panel").find(".modal-body").html(); }); - $("#autoRelease,#enableShare,#isDownload").bootstrapSwitch(); + $("#autoRelease,#enableShare,#isDownload,#is_use_first_document").bootstrapSwitch(); $('input[name="label"]').tagsinput({ confirmKeys: [13,44], diff --git a/views/document/compare.tpl b/views/document/compare.tpl index 7ff458c2..2c0a08e2 100644 --- a/views/document/compare.tpl +++ b/views/document/compare.tpl @@ -4,7 +4,7 @@ 文档比较 - Powered by MinDoc - + diff --git a/views/document/history.tpl b/views/document/history.tpl index 6606db0a..d059951a 100644 --- a/views/document/history.tpl +++ b/views/document/history.tpl @@ -3,7 +3,7 @@ - + @@ -42,7 +42,7 @@ {{range $index,$item := .List}} {{$item.HistoryId}} - {{date $item.ModifyTime "Y-m-d H:i:s"}} + {{date_format $item.ModifyTime "2006-01-02 15:04:05"}} {{$item.ModifyName}} {{$item.Version}} diff --git a/views/errors/403.tpl b/views/errors/403.tpl index a600df45..1eaec9eb 100644 --- a/views/errors/403.tpl +++ b/views/errors/403.tpl @@ -3,7 +3,7 @@ - + diff --git a/views/errors/404.tpl b/views/errors/404.tpl index 3ecbf1f5..0fd193bd 100644 --- a/views/errors/404.tpl +++ b/views/errors/404.tpl @@ -3,7 +3,7 @@ - + diff --git a/views/errors/error.tpl b/views/errors/error.tpl index 65aa3de5..6819cb0d 100644 --- a/views/errors/error.tpl +++ b/views/errors/error.tpl @@ -3,7 +3,7 @@ - + diff --git a/views/manager/attach_detailed.tpl b/views/manager/attach_detailed.tpl index 1276291d..204974de 100644 --- a/views/manager/attach_detailed.tpl +++ b/views/manager/attach_detailed.tpl @@ -76,7 +76,7 @@
- +
@@ -100,7 +100,7 @@ - + \ No newline at end of file diff --git a/views/manager/books.tpl b/views/manager/books.tpl index 14266749..9d717d11 100644 --- a/views/manager/books.tpl +++ b/views/manager/books.tpl @@ -87,7 +87,7 @@
- {{dateformat $item.CreateTime "2006-01-02 15:04:05"}} + {{date_format $item.CreateTime "2006-01-02 15:04:05"}} {{if eq $item.RealName "" }}{{$item.CreateName}}{{else}}{{$item.RealName}}{{end}} diff --git a/views/search/index.tpl b/views/search/index.tpl index f44020db..bcbac2f0 100644 --- a/views/search/index.tpl +++ b/views/search/index.tpl @@ -38,12 +38,12 @@
来自:{{$item.BookName}} 作者:{{$item.Author}} - 更新时间:{{date $item.ModifyTime "Y-m-d H:i:s"}} + 更新时间:{{date_format $item.ModifyTime "2006-01-02 15:04:05"}}
{{else}}
- + 暂无相关搜索结果
{{end}}