diff --git a/controllers/search.go b/controllers/search.go new file mode 100644 index 00000000..d2cc02c8 --- /dev/null +++ b/controllers/search.go @@ -0,0 +1,92 @@ +package controllers + +import ( + "github.com/lifei6671/godoc/models" + "github.com/lifei6671/godoc/conf" + "github.com/lifei6671/godoc/utils" + "github.com/astaxie/beego" + "strings" + "regexp" + "strconv" +) + +type SearchController struct { + BaseController +} + +func (c *SearchController) Index() { + c.Prepare() + c.TplName = "search/index.tpl" + + keyword := c.GetString("keyword") + pageIndex,_ := c.GetInt("page",1) + + c.Data["BaseUrl"] = c.BaseUrl() + + if keyword != "" { + c.Data["Keyword"] = keyword + member_id := 0 + if c.Member != nil { + member_id = c.Member.MemberId + } + search_result,totalCount,err := models.NewDocumentSearchResult().FindToPager(keyword,pageIndex,conf.PageSize,member_id) + + if err != nil { + beego.Error(err) + return + } + if totalCount > 0 { + html := utils.GetPagerHtml(c.Ctx.Request.RequestURI, pageIndex, conf.PageSize, totalCount) + + c.Data["PageHtml"] = html + }else { + c.Data["PageHtml"] = "" + } + if len(search_result) > 0 { + for _,item := range search_result { + item.DocumentName = strings.Replace(item.DocumentName,keyword,"" + keyword + "",-1) + + if item.Description != "" { + src := item.Description + + //将HTML标签全转换成小写 + re, _ := regexp.Compile("\\<[\\S\\s]+?\\>") + src = re.ReplaceAllStringFunc(src, strings.ToLower) + + //去除STYLE + re, _ = regexp.Compile("\\") + src = re.ReplaceAllString(src, "") + + //去除SCRIPT + re, _ = regexp.Compile("\\") + src = re.ReplaceAllString(src, "") + + //去除所有尖括号内的HTML代码,并换成换行符 + re, _ = regexp.Compile("\\<[\\S\\s]+?\\>") + src = re.ReplaceAllString(src, "\n") + + //去除连续的换行符 + re, _ = regexp.Compile("\\s{2,}") + src = re.ReplaceAllString(src, "\n") + + r := []rune(src) + beego.Info(r) + if len(r) > 100 { + src = string(r[:100]) + }else{ + src = string(r) + } + item.Description = strings.Replace(src, keyword, "" + keyword + "", -1) + } + + if item.Identify == ""{ + item.Identify = strconv.Itoa(item.DocumentId) + } + if item.ModifyTime.IsZero() { + item.ModifyTime = item.CreateTime + } + } + } + c.Data["Lists"] = search_result + } +} diff --git a/models/document.go b/models/document.go index fae98513..78ca72df 100644 --- a/models/document.go +++ b/models/document.go @@ -134,4 +134,3 @@ func (m *Document) ReleaseContent(book_id int) { - diff --git a/models/document_search_result.go b/models/document_search_result.go new file mode 100644 index 00000000..cd06364e --- /dev/null +++ b/models/document_search_result.go @@ -0,0 +1,79 @@ +package models + +import ( + "time" + + "github.com/astaxie/beego/orm" +) + +type DocumentSearchResult struct { + DocumentId int `json:"doc_id"` + DocumentName string `json:"doc_name"` + // Identify 文档唯一标识 + Identify string `json:"identify"` + Description string `json:"description"` + Author string `json:"author"` + ModifyTime time.Time `json:"modify_time"` + CreateTime time.Time `json:"create_time"` + BookId int `json:"book_id"` + BookName string `json:"book_name"` + BookIdentify string `json:"book_identify"` + +} + +func NewDocumentSearchResult() *DocumentSearchResult { + return &DocumentSearchResult{} +} + +func (m *DocumentSearchResult) FindToPager(keyword string,page_index,page_size,member_id int) (search_result []*DocumentSearchResult,total_count int,err error) { + o := orm.NewOrm() + + offset := (page_index - 1) * page_size + + if member_id <= 0 { + sql1 := `SELECT count(doc.document_id) as total_count FROM md_documents AS doc + LEFT JOIN md_books as book ON doc.book_id = book.book_id +WHERE book.privately_owned = 0 AND (doc.document_name LIKE '%?%' OR doc.release LIKE '%?%') ` + + sql2 := `SELECT doc.document_id,doc.modify_time,doc.create_time,doc.document_name,doc.identify,doc.release as description,doc.modify_time,book.identify as book_identify,book.book_name,rel.member_id,member.account AS author FROM md_documents AS doc + LEFT JOIN md_books as book ON doc.book_id = book.book_id + LEFT JOIN md_relationship AS rel ON book.book_id = rel.book_id AND role_id = 0 + LEFT JOIN md_members as member ON rel.member_id = member.member_id +WHERE book.privately_owned = 0 AND (doc.document_name LIKE '%?%' OR doc.release LIKE '%?%') + ORDER BY doc.document_id DESC LIMIT ?,? ` + + err = o.Raw(sql1,keyword,keyword).QueryRow(&total_count) + if err != nil{ + return + } + _,err = o.Raw(sql2,keyword,keyword,offset,page_size).QueryRows(&search_result) + if err != nil { + return + } + }else{ + sql1 := `SELECT count(doc.document_id) as total_count FROM md_documents AS doc + LEFT JOIN md_books as book ON doc.book_id = book.book_id + LEFT JOIN md_relationship AS rel ON doc.book_id = rel.book_id AND role_id = 0 +WHERE (book.privately_owned = 0 OR rel.relationship_id > 0) AND (doc.document_name LIKE ? OR doc.release LIKE ?) ` + + sql2 := `SELECT doc.document_id,doc.modify_time,doc.create_time,doc.document_name,doc.identify,doc.release as description,doc.modify_time,book.identify as book_identify,book.book_name,rel.member_id,member.account AS author FROM md_documents AS doc + LEFT JOIN md_books as book ON doc.book_id = book.book_id + LEFT JOIN md_relationship AS rel ON book.book_id = rel.book_id AND role_id = 0 + LEFT JOIN md_members as member ON rel.member_id = member.member_id +WHERE (book.privately_owned = 0 OR rel.relationship_id > 0) AND (doc.document_name LIKE ? OR doc.release LIKE ?) + ORDER BY doc.document_id DESC LIMIT ?,? ` + + keyword = "%"+keyword+"%" + + err = o.Raw(sql1,keyword,keyword).QueryRow(&total_count) + if err != nil{ + return + } + _,err = o.Raw(sql2,keyword,keyword,offset,page_size).QueryRows(&search_result) + if err != nil { + return + } + } + return +} + diff --git a/routers/router.go b/routers/router.go index 5b0d0b8a..a36fcc35 100644 --- a/routers/router.go +++ b/routers/router.go @@ -63,4 +63,7 @@ func init() { beego.Router("/comment/create", &controllers.CommentController{},"post:Create") beego.Router("/comment/lists", &controllers.CommentController{},"get:Lists") beego.Router("/comment/index", &controllers.CommentController{},"*:Index") + + beego.Router("/search",&controllers.SearchController{},"get:Index") } + diff --git a/static/css/main.css b/static/css/main.css index 95ceb41a..e138e1a7 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -289,6 +289,87 @@ textarea{ .users-list .list-item .operate{ float: right; } +/**************用户搜索界面样式********************/ +.manual-search-reader .searchbar{ + padding: 8px; +} +.manual-search-reader .manual-body{ + margin-top: 60px; +} +.manual-search-reader .search-head{ + margin: 10px auto; + padding-bottom: 15px; + line-height: 1.5em; + border-bottom: 3px solid #EEEEEE; +} +.manual-search-reader .search-head .search-title{ + font-size: 22px; + font-weight: 300; +} +.manual-search-reader .search-body { + margin-top: 80px; +} +.manual-search-reader .searchbar .search-btn { + display: inline-block; + line-height: 100%; + cursor: pointer; + margin-top: -10px; + margin-left: -45px; + border: 0; + background-color: transparent +} +.manual-search-reader .searchbar .search-btn>i.fa{ + padding: 10px; +} +.manual-search-reader .search-empty .empty-image{ + margin: 5px auto; + display: block; + text-align: center; + opacity: 0.3; + filter: alpha(opacity=30); +} +.manual-search-reader .search-item{ + margin: 0 15px; + padding: 10px 20px; + line-height: 25px; +} +.manual-search-reader .search-item:hover{ + background-color: #F5F5F5; +} +.manual-search-reader .search-item a{ + color: #0886E9; +} +.manual-search-reader .search-item em{ + color: #FF802C; + font-style:normal; +} +.manual-search-reader .search-item .title{ + font-size: 16px; + font-weight: 400; +} +.manual-search-reader .search-item .description{ + color: #666; + line-height: 25px; + min-height: 20px; + font-size: 12px; +} +.manual-search-reader .search-item .site { + overflow: hidden; + text-overflow: ellipsis; + -o-text-overflow: ellipsis; + white-space: nowrap; + max-width: 600px; + color: #008000; + font-size: 12px; +} +.manual-search-reader .search-item .source .item { + display: inline-block; + margin-right: 15px; +} +.manual-search-reader .search-item .source,.manual-search-reader .search-item .source a{ + font-size: 12px; + color: #999999; +} /**************用户登录界面样式*******************/ .login .login-body{ diff --git a/static/images/search_empty.png b/static/images/search_empty.png new file mode 100644 index 00000000..c7404a3c Binary files /dev/null and b/static/images/search_empty.png differ diff --git a/static/js/markdown.js b/static/js/markdown.js index bbbfc786..a06cd762 100644 --- a/static/js/markdown.js +++ b/static/js/markdown.js @@ -243,7 +243,7 @@ $(function () { if (doc_name === ""){ return showError("目录名称不能为空","#add-error-message") } - window.addDocumentFormIndex = layer.load(1, { shade: [0.1,'#fff'] }); + $("#btnSaveDocument").button("loading"); return true; }, success : function (res) { @@ -266,7 +266,7 @@ $(function () { }else{ showError(res.message,"#add-error-message") } - layer.close(window.addDocumentFormIndex); + $("#btnSaveDocument").button("reset"); } }); diff --git a/views/book/create.tpl b/views/book/create.tpl deleted file mode 100644 index 88932d58..00000000 --- a/views/book/create.tpl +++ /dev/null @@ -1,73 +0,0 @@ - - - - - - - - 概要 - Powered by MinDoc - - - - - - - - - - - -
- {{template "widgets/header.tpl" .}} -
-
-
- - -
-
-
-
- 项目设置 - - -
-
-
-
-
-
- - -
- -
-
-
- -
-
-
-
-
- {{template "widgets/footer.tpl" .}} -
- - - - - - \ No newline at end of file diff --git a/views/book/dashboard.tpl b/views/book/dashboard.tpl index ab3066c4..da7c73d5 100644 --- a/views/book/dashboard.tpl +++ b/views/book/dashboard.tpl @@ -83,11 +83,15 @@ 担任角色: {{.Model.RoleName}} -
+ {{/*
评论数量: {{.Model.CommentCount}} 条 -
-
{{.Model.Description}}
+
*/}} +
+ 文档标签: + {{.Model.Label}} +
+
{{.Model.Description}}
diff --git a/views/book/index.tpl b/views/book/index.tpl index 5728abab..e8707d42 100644 --- a/views/book/index.tpl +++ b/views/book/index.tpl @@ -168,7 +168,7 @@ @@ -204,16 +204,18 @@ if(description.length > 500){ return showError("描述信息不超过500个字符"); } + $("#btnSaveDocument").button("loading"); return showSuccess(""); }, success : function (res) { - console.log(res); + $("#btnSaveDocument").button("reset"); if(res.errcode === 0){ window.app.lists.splice(0,0,res.data); $("#addBookDialogModal").modal("hide"); }else{ showError(res.message); } + } }); diff --git a/views/manager/books.tpl b/views/manager/books.tpl index 47b412a9..3c2bf686 100644 --- a/views/manager/books.tpl +++ b/views/manager/books.tpl @@ -29,7 +29,7 @@
  • 仪表盘
  • 用户管理
  • 项目管理
  • -
  • 评论管理
  • + {{/*
  • 评论管理
  • */}}
  • 配置管理
  • diff --git a/views/manager/edit_book.tpl b/views/manager/edit_book.tpl index 8411cd6a..187658e9 100644 --- a/views/manager/edit_book.tpl +++ b/views/manager/edit_book.tpl @@ -30,7 +30,7 @@
  • 仪表盘
  • 用户管理
  • 项目管理
  • -
  • 评论管理
  • + {{/*
  • 评论管理
  • */}}
  • 配置管理
  • diff --git a/views/manager/index.tpl b/views/manager/index.tpl index 0671e304..0c02020a 100644 --- a/views/manager/index.tpl +++ b/views/manager/index.tpl @@ -29,7 +29,7 @@
  • 仪表盘
  • 用户管理
  • 项目管理
  • -
  • 评论管理
  • + {{/*
  • 评论管理
  • */}}
  • 配置管理
  • @@ -55,11 +55,13 @@ 会员数量 {{.Model.MemberNumber}} + {{/*
    评论数量 {{.Model.CommentNumber}}
    + */}}
    附件数量 diff --git a/views/manager/setting.tpl b/views/manager/setting.tpl index 1e4c3479..724d2f88 100644 --- a/views/manager/setting.tpl +++ b/views/manager/setting.tpl @@ -28,7 +28,7 @@
  • 仪表盘
  • 用户管理
  • 项目管理
  • -
  • 评论管理
  • + {{/*
  • 评论管理
  • */}}
  • 配置管理
  • diff --git a/views/manager/users.tpl b/views/manager/users.tpl index d6972bf4..98dba9bb 100644 --- a/views/manager/users.tpl +++ b/views/manager/users.tpl @@ -29,7 +29,7 @@
  • 仪表盘
  • 用户管理
  • 项目管理
  • -
  • 评论管理
  • + {{/*
  • 评论管理
  • */}}
  • 配置管理
  • diff --git a/views/search/index.tpl b/views/search/index.tpl new file mode 100644 index 00000000..8074b675 --- /dev/null +++ b/views/search/index.tpl @@ -0,0 +1,58 @@ + + + + + + + + 搜索 - Powered by MinDoc + + + + + + + + + + + +
    + {{template "widgets/header.tpl" .}} +
    +
    + 显示"{{.Keyword}}"的搜索结果 +
    +
    +
    + {{range $index,$item := .Lists}} +
    + +
    + {{str2html $item.Description}} +
    +
    {{$.BaseUrl}}{{urlfor "DocumentController.Read" ":key" $item.BookIdentify ":id" $item.Identify}}
    +
    + 来自:{{$item.BookName}} + 作者:{{$item.Author}} + 更新时间:{{date $item.ModifyTime "Y-m-d H:i:s"}} +
    +
    + {{else}} +
    + +
    + {{end}} +
    +
    +
    +
    + {{template "widgets/footer.tpl" .}} +
    + + + + \ No newline at end of file diff --git a/views/widgets/header.tpl b/views/widgets/header.tpl index 3e3ab51d..34616d8f 100644 --- a/views/widgets/header.tpl +++ b/views/widgets/header.tpl @@ -8,6 +8,15 @@ {{.SITE_NAME}} {{end}} + + {{/* + */}}