1
0
Эх сурвалжийг харах

实现项目内搜索功能

Minho 8 жил өмнө
parent
commit
eba2fdc4ac

+ 32 - 2
controllers/document.go

@@ -832,10 +832,40 @@ func (c *DocumentController) QrCode() {
 	}
 }
 
+//项目内搜索.
 func (c *DocumentController) Search()  {
 	c.Prepare()
-	
-	c.JsonResult(0,"ok")
+
+	identify := c.Ctx.Input.Param(":key")
+	token := c.GetString("token")
+	keyword := strings.TrimSpace(c.GetString("keyword"))
+
+	if identify == ""{
+		c.JsonResult(6001,"参数错误")
+	}
+	if !c.EnableAnonymous && c.Member == nil {
+		c.Redirect(beego.URLFor("AccountController.Login"), 302)
+		return
+	}
+	bookResult := isReadable(identify,token,c)
+
+	docs,err := models.NewDocumentSearchResult().SearchDocument(keyword,bookResult.BookId)
+
+	if err != nil {
+		beego.Error(err)
+		c.JsonResult(6002,"搜索结果错误")
+	}
+	if len(docs) < 0 {
+		c.JsonResult(404,"没有数据库")
+	}
+	for _,doc := range docs {
+		doc.BookId = bookResult.BookId
+		doc.BookName = bookResult.BookName
+		doc.Description = bookResult.Description
+		doc.BookIdentify = bookResult.Identify
+	}
+
+	c.JsonResult(0,"ok",docs)
 }
 
 //递归生成文档序列数组.

+ 1 - 0
main.go

@@ -23,6 +23,7 @@ func main() {
 	commands.RegisterCommand()
 	commands.RegisterFunction()
 
+	
 	beego.SetStaticPath("uploads", "uploads")
 
 	beego.ErrorController(&controllers.ErrorController{})

+ 6 - 1
models/document.go

@@ -53,6 +53,7 @@ func NewDocument() *Document  {
 	}
 }
 
+//根据文档ID查询指定文档.
 func (m *Document) Find(id int) (*Document,error) {
 	if id <= 0 {
 		return m,ErrInvalidParameter
@@ -150,7 +151,8 @@ func (m *Document) ReleaseContent(book_id int)  {
 	}
 }
 
-func (m *Document)  FindListByBookId(book_id int) (docs []*Document,err error) {
+//根据项目ID查询文档列表.
+func (m *Document) FindListByBookId(book_id int) (docs []*Document,err error) {
 	o := orm.NewOrm()
 
 	_,err = o.QueryTable(m.TableNameWithPrefix()).Filter("book_id",book_id).All(&docs)
@@ -162,3 +164,6 @@ func (m *Document)  FindListByBookId(book_id int) (docs []*Document,err error) {
 
 
 
+
+
+

+ 31 - 21
models/document_search_result.go

@@ -7,29 +7,29 @@ import (
 )
 
 type DocumentSearchResult struct {
-	DocumentId int		`json:"doc_id"`
-	DocumentName string	`json:"doc_name"`
+	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"`
-
+	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) {
+//分页全局搜索.
+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
-	keyword = "%"+keyword+"%"
+	keyword = "%" + keyword + "%"
 
 	if member_id <= 0 {
 		sql1 := `SELECT count(doc.document_id) as total_count FROM md_documents AS doc
@@ -43,15 +43,15 @@ WHERE book.privately_owned = 0 AND (doc.document_name LIKE ? OR doc.release LIKE
 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{
+		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)
+		_, err = o.Raw(sql2, keyword, keyword, offset, page_size).QueryRows(&search_result)
 		if err != nil {
 			return
 		}
-	}else{
+	} 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
@@ -66,12 +66,11 @@ WHERE (book.privately_owned = 0 OR rel1.relationship_id > 0)  AND (doc.document_
 WHERE (book.privately_owned = 0 OR rel1.relationship_id > 0)  AND (doc.document_name LIKE ? OR doc.release LIKE ?)
  ORDER BY doc.document_id DESC LIMIT ?,? `
 
-
-		err = o.Raw(sql1,member_id,keyword,keyword).QueryRow(&total_count)
-		if err != nil{
+		err = o.Raw(sql1, member_id, keyword, keyword).QueryRow(&total_count)
+		if err != nil {
 			return
 		}
-		_,err = o.Raw(sql2,member_id,keyword,keyword,offset,page_size).QueryRows(&search_result)
+		_, err = o.Raw(sql2, member_id, keyword, keyword, offset, page_size).QueryRows(&search_result)
 		if err != nil {
 			return
 		}
@@ -79,3 +78,14 @@ WHERE (book.privately_owned = 0 OR rel1.relationship_id > 0)  AND (doc.document_
 	return
 }
 
+//项目内搜索.
+func (m *DocumentSearchResult) SearchDocument(keyword string, book_id int) (docs []*DocumentSearchResult, err error) {
+	o := orm.NewOrm()
+
+	sql := "SELECT * FROM md_documents WHERE book_id = ? AND (document_name LIKE ? OR `release` LIKE ?) "
+	keyword = "%" + keyword + "%"
+
+	_, err = o.Raw(sql, book_id, keyword, keyword).QueryRows(&docs)
+
+	return
+}

+ 78 - 1
static/css/kancloud.css

@@ -271,10 +271,18 @@ h6 {
 .m-manual.manual-mode-view .manual-catalog {
     display: block
 }
+.m-manual.manual-mode-search .manual-search .search-container{
+    position: absolute;
+    top: 0;
+    left: 0;
+    right: 0;
+    bottom: 0;
+}
 .m-manual .manual-search .search-form{
-    margin: 5px ;
+    margin: 5px 5px 10px 5px;
     position: relative;
 }
+
 .m-manual .manual-search .btn-search{
     background-color: #ffffff;
     border: 0;
@@ -283,6 +291,75 @@ h6 {
     top: 2px;
     right: 5px;
 }
+.m-manual .manual-search .btn-search .fa{
+    width: 16px;
+    height: 16px;
+    vertical-align: middle;
+}
+.m-manual .manual-search .btn-search .loading{
+    background-image: url("../images/loading.gif");
+}
+
+.m-manual .manual-search .search-result{
+    position: absolute;
+    top: 45px;
+    left: 0;
+    right: 0;
+    bottom: 0;
+    width: 275px;
+    overflow-y: auto;
+    border-top: 1px solid #eee;
+}
+.m-manual .manual-search .search-result .search-empty{
+    position: absolute;
+    top: 45%;
+    left: 0;
+    right: 0;
+    text-align: center;
+}
+.m-manual .manual-search .search-result .search-empty i{
+    font-size: 50px;
+    display: block;
+    color: #999;
+    font-weight: 200;
+}
+.m-manual .manual-search .search-result .search-empty .text{
+    font-size: 16px;
+    font-weight: 200;
+    color: #999;
+    line-height: 40px;
+}
+.m-manual .manual-search .search-list{
+    position: absolute;
+    top: 0;
+    bottom: 0;
+    min-width: 100%;
+}
+.m-manual .manual-search .search-list a{
+    display: block;
+    border-bottom: 0;
+    height: 30px;
+    line-height: 24px;
+    padding: 3px 10px 3px 20px;
+    color: #666;
+    text-decoration: none;
+    white-space:nowrap;
+    overflow: hidden;
+}
+.m-manual .manual-search .search-list a:hover{
+    text-shadow: none;
+    background: #116cd6;
+    box-shadow: none;
+    color: #fff;
+    text-decoration: none;
+    white-space:nowrap;
+}
+.m-manual .manual-search .search-list a.active{
+    background: #10af88;
+    background: -webkit-linear-gradient(top, #beebff 0%, #a8e4ff 100%);
+    background: linear-gradient(to bottom, #10af88 0%, #10af88 100%);
+    color: #ffffff;
+}
 .m-manual .manual-left .m-copyright {
     border-top: 0;
     background: #fafafa;