2017-05-02 18:09:46 +08:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
2018-01-26 17:17:38 +08:00
|
|
|
"strings"
|
2021-03-23 15:09:17 +08:00
|
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
"github.com/mindoc-org/mindoc/conf"
|
|
|
|
"github.com/mindoc-org/mindoc/models"
|
|
|
|
"github.com/mindoc-org/mindoc/utils"
|
|
|
|
"github.com/mindoc-org/mindoc/utils/pagination"
|
|
|
|
"github.com/mindoc-org/mindoc/utils/sqltil"
|
2017-05-02 18:09:46 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type SearchController struct {
|
|
|
|
BaseController
|
|
|
|
}
|
2018-02-28 15:47:00 +08:00
|
|
|
|
2018-02-03 15:25:10 +08:00
|
|
|
//搜索首页
|
2018-01-26 17:17:38 +08:00
|
|
|
func (c *SearchController) Index() {
|
2017-05-02 18:09:46 +08:00
|
|
|
c.Prepare()
|
|
|
|
c.TplName = "search/index.tpl"
|
|
|
|
|
2017-05-05 11:40:59 +08:00
|
|
|
//如果没有开启你们访问则跳转到登录
|
|
|
|
if !c.EnableAnonymous && c.Member == nil {
|
2018-03-13 19:20:50 +08:00
|
|
|
c.Redirect(conf.URLFor("AccountController.Login"), 302)
|
2017-05-05 11:40:59 +08:00
|
|
|
return
|
|
|
|
}
|
2017-05-05 11:43:47 +08:00
|
|
|
|
2017-05-02 18:09:46 +08:00
|
|
|
keyword := c.GetString("keyword")
|
2018-01-26 17:17:38 +08:00
|
|
|
pageIndex, _ := c.GetInt("page", 1)
|
2017-05-02 18:09:46 +08:00
|
|
|
|
|
|
|
c.Data["BaseUrl"] = c.BaseUrl()
|
|
|
|
|
|
|
|
if keyword != "" {
|
|
|
|
c.Data["Keyword"] = keyword
|
2018-03-24 22:36:35 +08:00
|
|
|
memberId := 0
|
2017-05-02 18:09:46 +08:00
|
|
|
if c.Member != nil {
|
2018-03-24 22:36:35 +08:00
|
|
|
memberId = c.Member.MemberId
|
2017-05-02 18:09:46 +08:00
|
|
|
}
|
2018-12-24 15:23:17 +08:00
|
|
|
searchResult, totalCount, err := models.NewDocumentSearchResult().FindToPager(sqltil.EscapeLike(keyword), pageIndex, conf.PageSize, memberId)
|
2017-05-02 18:09:46 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2019-01-15 19:36:11 +08:00
|
|
|
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()
|
2018-01-26 17:17:38 +08:00
|
|
|
} else {
|
2017-05-02 18:09:46 +08:00
|
|
|
c.Data["PageHtml"] = ""
|
|
|
|
}
|
2018-03-24 22:36:35 +08:00
|
|
|
if len(searchResult) > 0 {
|
2018-11-29 15:11:34 +08:00
|
|
|
keywords := strings.Split(keyword," ")
|
2017-05-02 18:09:46 +08:00
|
|
|
|
2018-11-29 15:11:34 +08:00
|
|
|
for _, item := range searchResult {
|
|
|
|
for _,word := range keywords {
|
|
|
|
item.DocumentName = strings.Replace(item.DocumentName, word, "<em>"+word+"</em>", -1)
|
|
|
|
if item.Description != "" {
|
|
|
|
src := item.Description
|
2017-05-02 18:09:46 +08:00
|
|
|
|
2018-11-29 15:11:34 +08:00
|
|
|
r := []rune(utils.StripTags(item.Description))
|
2017-05-03 16:03:51 +08:00
|
|
|
|
2018-11-29 15:11:34 +08:00
|
|
|
if len(r) > 100 {
|
|
|
|
src = string(r[:100])
|
|
|
|
} else {
|
|
|
|
src = string(r)
|
|
|
|
}
|
|
|
|
item.Description = strings.Replace(src, word, "<em>"+word+"</em>", -1)
|
2017-05-02 18:09:46 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-26 17:17:38 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-24 22:36:35 +08:00
|
|
|
c.Data["Lists"] = searchResult
|
2017-05-02 18:09:46 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-03 15:25:10 +08:00
|
|
|
|
|
|
|
//搜索用户
|
|
|
|
func (c *SearchController) User() {
|
|
|
|
c.Prepare()
|
|
|
|
key := c.Ctx.Input.Param(":key")
|
|
|
|
keyword := strings.TrimSpace(c.GetString("q"))
|
2018-02-28 15:47:00 +08:00
|
|
|
if key == "" || keyword == "" {
|
|
|
|
c.JsonResult(404, "参数错误")
|
2018-02-03 15:25:10 +08:00
|
|
|
}
|
2018-12-24 15:23:17 +08:00
|
|
|
keyword = sqltil.EscapeLike(keyword)
|
2018-02-03 15:25:10 +08:00
|
|
|
|
|
|
|
book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
|
|
|
|
if err != nil {
|
|
|
|
if err == models.ErrPermissionDenied {
|
2018-02-28 15:47:00 +08:00
|
|
|
c.JsonResult(403, "没有权限")
|
2018-02-03 15:25:10 +08:00
|
|
|
}
|
2018-02-28 15:47:00 +08:00
|
|
|
c.JsonResult(500, "项目不存在")
|
2018-02-03 15:25:10 +08:00
|
|
|
}
|
|
|
|
|
2019-01-16 01:10:57 +08:00
|
|
|
//members, err := models.NewMemberRelationshipResult().FindNotJoinUsersByAccount(book.BookId, 10, "%"+keyword+"%")
|
|
|
|
members, err := models.NewMemberRelationshipResult().FindNotJoinUsersByAccountOrRealName(book.BookId, 10, "%"+keyword+"%")
|
2018-02-03 15:25:10 +08:00
|
|
|
if err != nil {
|
|
|
|
beego.Error("查询用户列表出错:" + err.Error())
|
2018-02-28 15:47:00 +08:00
|
|
|
c.JsonResult(500, err.Error())
|
2018-02-03 15:25:10 +08:00
|
|
|
}
|
|
|
|
result := models.SelectMemberResult{}
|
2018-02-28 15:47:00 +08:00
|
|
|
items := make([]models.KeyValueItem, 0)
|
2018-02-03 15:25:10 +08:00
|
|
|
|
2018-02-28 15:47:00 +08:00
|
|
|
for _, member := range members {
|
2018-02-03 15:25:10 +08:00
|
|
|
item := models.KeyValueItem{}
|
|
|
|
item.Id = member.MemberId
|
2019-01-16 01:10:57 +08:00
|
|
|
item.Text = member.Account + "[" + member.RealName + "]"
|
2018-02-28 15:47:00 +08:00
|
|
|
items = append(items, item)
|
2018-02-03 15:25:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
result.Result = items
|
|
|
|
|
2018-02-28 15:47:00 +08:00
|
|
|
c.JsonResult(0, "OK", result)
|
2018-02-03 15:25:10 +08:00
|
|
|
}
|