mindoc/controllers/SearchController.go

116 lines
2.8 KiB
Go
Raw Normal View History

2017-05-02 18:09:46 +08:00
package controllers
import (
"github.com/astaxie/beego"
2017-06-14 09:23:29 +08:00
"github.com/lifei6671/mindoc/conf"
"github.com/lifei6671/mindoc/models"
"github.com/lifei6671/mindoc/utils"
2018-01-26 18:07:55 +08:00
"github.com/lifei6671/mindoc/utils/pagination"
2017-05-02 18:09:46 +08:00
"strconv"
"strings"
2017-05-02 18:09:46 +08:00
)
type SearchController struct {
BaseController
}
//搜索首页
func (c *SearchController) Index() {
2017-05-02 18:09:46 +08:00
c.Prepare()
c.TplName = "search/index.tpl"
//如果没有开启你们访问则跳转到登录
if !c.EnableAnonymous && c.Member == nil {
2018-03-13 19:20:50 +08:00
c.Redirect(conf.URLFor("AccountController.Login"), 302)
return
}
2017-05-05 11:43:47 +08:00
2017-05-02 18:09:46 +08:00
keyword := c.GetString("keyword")
pageIndex, _ := c.GetInt("page", 1)
2017-05-02 18:09:46 +08:00
c.Data["BaseUrl"] = c.BaseUrl()
if keyword != "" {
c.Data["Keyword"] = keyword
memberId := 0
2017-05-02 18:09:46 +08:00
if c.Member != nil {
memberId = c.Member.MemberId
2017-05-02 18:09:46 +08:00
}
searchResult, totalCount, err := models.NewDocumentSearchResult().FindToPager(keyword, pageIndex, conf.PageSize, memberId)
2017-05-02 18:09:46 +08:00
if err != nil {
beego.Error("查询搜索结果失败 => ",err)
2017-05-02 18:09:46 +08:00
return
}
if totalCount > 0 {
2018-03-13 14:14:56 +08:00
pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize,c.BaseUrl())
2018-01-26 18:07:55 +08:00
c.Data["PageHtml"] = pager.HtmlPages()
} else {
2017-05-02 18:09:46 +08:00
c.Data["PageHtml"] = ""
}
if len(searchResult) > 0 {
for _, item := range searchResult {
item.DocumentName = strings.Replace(item.DocumentName, keyword, "<em>"+keyword+"</em>", -1)
2017-05-02 18:09:46 +08:00
if item.Description != "" {
src := item.Description
r := []rune(utils.StripTags(item.Description))
2017-05-03 16:03:51 +08:00
2017-05-02 18:09:46 +08:00
if len(r) > 100 {
src = string(r[:100])
} else {
2017-05-02 18:09:46 +08:00
src = string(r)
}
item.Description = strings.Replace(src, keyword, "<em>"+keyword+"</em>", -1)
2017-05-02 18:09:46 +08:00
}
if item.Identify == "" {
2017-05-02 18:09:46 +08:00
item.Identify = strconv.Itoa(item.DocumentId)
}
if item.ModifyTime.IsZero() {
item.ModifyTime = item.CreateTime
}
}
}
c.Data["Lists"] = searchResult
2017-05-02 18:09:46 +08:00
}
}
//搜索用户
func (c *SearchController) User() {
c.Prepare()
key := c.Ctx.Input.Param(":key")
keyword := strings.TrimSpace(c.GetString("q"))
if key == "" || keyword == "" {
c.JsonResult(404, "参数错误")
}
book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
if err != nil {
if err == models.ErrPermissionDenied {
c.JsonResult(403, "没有权限")
}
c.JsonResult(500, "项目不存在")
}
members, err := models.NewMemberRelationshipResult().FindNotJoinUsersByAccount(book.BookId, 10, "%"+keyword+"%")
if err != nil {
beego.Error("查询用户列表出错:" + err.Error())
c.JsonResult(500, err.Error())
}
result := models.SelectMemberResult{}
items := make([]models.KeyValueItem, 0)
for _, member := range members {
item := models.KeyValueItem{}
item.Id = member.MemberId
item.Text = member.Account
items = append(items, item)
}
result.Result = items
c.JsonResult(0, "OK", result)
}