Selaa lähdekoodia

实现设置第一篇文章为项目默认打开的页面

Minho 7 vuotta sitten
vanhempi
sitoutus
e56210165f

+ 3 - 0
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)
+	})
 }
 
 //解析命令

+ 6 - 0
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, "保存失败")
 	}

+ 96 - 78
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 += "<div class=\"wiki-bottom\">文档更新时间: " + doc.ModifyTime.Local().Format("2006-01-02 15:04") + "</div>";
+			}
+			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)
+	}
+}

+ 2 - 0
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 获取对应数据库表名.

+ 13 - 2
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
+}
 
 
 

+ 1 - 4
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)
 	}

+ 1 - 1
views/account/find_password_setp1.tpl

@@ -2,7 +2,7 @@
 <html lang="zh-cn">
 <head>
     <meta charset="utf-8">
-    <link rel="shortcut icon" href="/favicon.ico">
+    <link rel="shortcut icon" href="{{cdnimg "/favicon.ico"}}">
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
     <meta name="renderer" content="webkit" />
     <meta name="viewport" content="width=device-width, initial-scale=1">

+ 1 - 1
views/account/find_password_setp2.tpl

@@ -2,7 +2,7 @@
 <html lang="zh-cn">
 <head>
     <meta charset="utf-8">
-    <link rel="shortcut icon" href="/favicon.ico">
+    <link rel="shortcut icon" href="{{cdnimg "/favicon.ico"}}">
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
     <meta name="renderer" content="webkit" />
     <meta name="viewport" content="width=device-width, initial-scale=1">

+ 1 - 1
views/account/login.tpl

@@ -2,7 +2,7 @@
 <html lang="zh-cn">
 <head>
     <meta charset="utf-8">
-    <link rel="shortcut icon" href="/favicon.ico">
+    <link rel="shortcut icon" href="{{cdnimg "/favicon.ico"}}">
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
     <meta name="renderer" content="webkit" />
     <meta name="viewport" content="width=device-width, initial-scale=1">

+ 1 - 1
views/account/register.tpl

@@ -2,7 +2,7 @@
 <html lang="zh-cn">
 <head>
     <meta charset="utf-8">
-    <link rel="shortcut icon" href="/favicon.ico">
+    <link rel="shortcut icon" href="{{cdnimg "/favicon.ico"}}">
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
     <meta name="renderer" content="webkit" />
     <meta name="viewport" content="width=device-width, initial-scale=1">

+ 2 - 2
views/book/dashboard.tpl

@@ -73,11 +73,11 @@
                             </div>
                             <div class="list">
                                 <span class="title">创建时间:</span>
-                                <span class="body"> {{dateformat .Model.CreateTime "2006-01-02 15:04:05"}} </span>
+                                <span class="body"> {{date_format .Model.CreateTime "2006-01-02 15:04:05"}} </span>
                             </div>
                             <div class="list">
                                 <span class="title">修改时间:</span>
-                                <span class="body"> {{dateformat .Model.CreateTime "2006-01-02 15:04:05"}} </span>
+                                <span class="body"> {{date_format .Model.CreateTime "2006-01-02 15:04:05"}} </span>
                             </div>
                         <div class="list">
                             <span class="title">担任角色:</span>

+ 11 - 3
views/book/setting.tpl

@@ -142,7 +142,7 @@
                     <label for="autoRelease">开启导出</label>
                     <div class="controls">
                         <div class="switch switch-small" data-on="primary" data-off="info">
-                            <input type="checkbox" id="isDownload" name="is_download"{{if .Model.IsDownload }} checked{{end}} data-size="small">
+                            <input type="checkbox" id="isDownload" name="is_download"{{if .Model.IsDownload }} checked{{end}} data-size="small" placeholder="开启导出">
                         </div>
                     </div>
                 </div>
@@ -150,7 +150,15 @@
                     <label for="autoRelease">开启分享</label>
                     <div class="controls">
                         <div class="switch switch-small" data-on="primary" data-off="info">
-                            <input type="checkbox" id="enableShare" name="enable_share"{{if .Model.IsEnableShare }} checked{{end}} data-size="small">
+                            <input type="checkbox" id="enableShare" name="enable_share"{{if .Model.IsEnableShare }} checked{{end}} data-size="small" placeholder="开启分享">
+                        </div>
+                    </div>
+                </div>
+                <div class="form-group">
+                    <label for="autoRelease">设置第一篇文档为默认首页</label>
+                    <div class="controls">
+                        <div class="switch switch-small" data-on="primary" data-off="info">
+                            <input type="checkbox" id="is_use_first_document" name="is_use_first_document"{{if .Model.IsUseFirstDocument }} checked{{end}} data-size="small" placeholder="设置第一篇文档为默认首页">
                         </div>
                     </div>
                 </div>
@@ -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],

+ 1 - 1
views/document/compare.tpl

@@ -4,7 +4,7 @@
 <head>
     <meta charset="utf-8" />
     <title>文档比较 - Powered by MinDoc</title>
-    <link rel="shortcut icon" href="/favicon.ico" />
+    <link rel="shortcut icon" href="{{cdnimg "/favicon.ico"}}" />
     <link href="/static/fonts/notosans.css" rel='stylesheet' type='text/css' />
     <script type="text/javascript" src="/static/jquery/1.12.4/jquery.min.js"></script>
     <link type='text/css' rel='stylesheet' href='/static/mergely/editor/lib/wicked-ui.css' />

+ 2 - 2
views/document/history.tpl

@@ -3,7 +3,7 @@
 <html lang="zh-cn">
 <head>
     <meta charset="utf-8">
-    <link rel="shortcut icon" href="/favicon.ico">
+    <link rel="shortcut icon" href="{{cdnimg "/favicon.ico"}}">
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
     <meta name="renderer" content="webkit" />
     <meta name="viewport" content="width=device-width, initial-scale=1">
@@ -42,7 +42,7 @@
             {{range $index,$item := .List}}
             <tr>
                 <td>{{$item.HistoryId}}</td>
-                <td>{{date $item.ModifyTime "Y-m-d H:i:s"}}</td>
+                <td>{{date_format $item.ModifyTime "2006-01-02 15:04:05"}}</td>
                 <td>{{$item.ModifyName}}</td>
                 <td>{{$item.Version}}</td>
                 <td>

+ 1 - 1
views/errors/403.tpl

@@ -3,7 +3,7 @@
 <head>
     <meta charset="utf-8">
     <meta name="author" content="Minho" />
-    <link rel="shortcut icon" href="/favicon.ico">
+    <link rel="shortcut icon" href="{{cdnimg "/favicon.ico"}}">
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
     <meta name="renderer" content="webkit" />
     <meta name="viewport" content="width=device-width, initial-scale=1">

+ 1 - 1
views/errors/404.tpl

@@ -3,7 +3,7 @@
 <head>
     <meta charset="utf-8">
     <meta name="author" content="Minho" />
-    <link rel="shortcut icon" href="/favicon.ico">
+    <link rel="shortcut icon" href="{{cdnimg "/favicon.ico"}}">
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
     <meta name="renderer" content="webkit" />
     <meta name="viewport" content="width=device-width, initial-scale=1">

+ 1 - 1
views/errors/error.tpl

@@ -3,7 +3,7 @@
 <head>
     <meta charset="utf-8">
     <meta name="author" content="Minho" />
-    <link rel="shortcut icon" href="/favicon.ico">
+    <link rel="shortcut icon" href="{{cdnimg "/favicon.ico"}}">
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
     <meta name="renderer" content="webkit" />
     <meta name="viewport" content="width=device-width, initial-scale=1">

+ 2 - 2
views/manager/attach_detailed.tpl

@@ -76,7 +76,7 @@
                     </div>
                     <div class="form-group">
                         <label>上传时间</label>
-                        <input type="text" value="{{date .Model.CreateTime "Y-m-d H:i:s"}}" class="form-control input-readonly" readonly placeholder="文件路径">
+                        <input type="text" value="{{date_format .Model.CreateTime "2006-01-02 15:04:05"}}" class="form-control input-readonly" readonly placeholder="文件路径">
                     </div>
                     <div class="form-group">
                         <label>用户账号</label>
@@ -100,7 +100,7 @@
 <script src="{{cdnjs "/static/bootstrap/js/bootstrap.min.js"}}"></script>
 <script src="{{cdnjs "/static/vuejs/vue.min.js"}}" type="text/javascript"></script>
 <script src="{{cdnjs "/static/js/jquery.form.js"}}" type="text/javascript"></script>
-<script src="/static/js/main.js" type="text/javascript"></script>
+<script src="{{cdnjs "/static/js/main.js"}}" type="text/javascript"></script>
 
 </body>
 </html>

+ 1 - 1
views/manager/books.tpl

@@ -87,7 +87,7 @@
                                 </div>
                                 <div class="info">
                                 <span title="创建时间" data-toggle="tooltip" data-placement="bottom"><i class="fa fa-clock-o"></i>
-                                    {{dateformat $item.CreateTime "2006-01-02 15:04:05"}}
+                                    {{date_format $item.CreateTime "2006-01-02 15:04:05"}}
 
                                 </span>
                                     <span title="创建者" data-toggle="tooltip" data-placement="bottom"><i class="fa fa-user"></i> {{if eq $item.RealName "" }}{{$item.CreateName}}{{else}}{{$item.RealName}}{{end}}</span>

+ 2 - 2
views/search/index.tpl

@@ -38,12 +38,12 @@
                     <div class="source">
                         <span class="item">来自:<a href="{{urlfor "DocumentController.Index" ":key" $item.BookIdentify}}" target="_blank">{{$item.BookName}}</a></span>
                         <span class="item">作者:{{$item.Author}}</span>
-                        <span class="item">更新时间:{{date  $item.ModifyTime "Y-m-d H:i:s"}}</span>
+                        <span class="item">更新时间:{{date_format  $item.ModifyTime "2006-01-02 15:04:05"}}</span>
                     </div>
                 </div>
                 {{else}}
                 <div class="search-empty">
-                    <img src="/static/images/search_empty.png" class="empty-image">
+                    <img src="{{cdnimg "/static/images/search_empty.png"}}" class="empty-image">
 					<span class="empty-text">暂无相关搜索结果</span>
                 </div>
                 {{end}}