SearchController.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package controllers
  2. import (
  3. "strconv"
  4. "strings"
  5. "github.com/beego/beego/v2/core/logs"
  6. "github.com/beego/i18n"
  7. "github.com/mindoc-org/mindoc/conf"
  8. "github.com/mindoc-org/mindoc/models"
  9. "github.com/mindoc-org/mindoc/utils"
  10. "github.com/mindoc-org/mindoc/utils/pagination"
  11. "github.com/mindoc-org/mindoc/utils/sqltil"
  12. )
  13. type SearchController struct {
  14. BaseController
  15. }
  16. //搜索首页
  17. func (c *SearchController) Index() {
  18. c.Prepare()
  19. c.TplName = "search/index.tpl"
  20. //如果没有开启你们访问则跳转到登录
  21. if !c.EnableAnonymous && c.Member == nil {
  22. c.Redirect(conf.URLFor("AccountController.Login"), 302)
  23. return
  24. }
  25. keyword := c.GetString("keyword")
  26. pageIndex, _ := c.GetInt("page", 1)
  27. c.Data["BaseUrl"] = c.BaseUrl()
  28. if keyword != "" {
  29. c.Data["Keyword"] = keyword
  30. memberId := 0
  31. if c.Member != nil {
  32. memberId = c.Member.MemberId
  33. }
  34. searchResult, totalCount, err := models.NewDocumentSearchResult().FindToPager(sqltil.EscapeLike(keyword), pageIndex, conf.PageSize, memberId)
  35. if err != nil {
  36. logs.Error("搜索失败 ->", err)
  37. return
  38. }
  39. if totalCount > 0 {
  40. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize, c.BaseUrl())
  41. c.Data["PageHtml"] = pager.HtmlPages()
  42. } else {
  43. c.Data["PageHtml"] = ""
  44. }
  45. if len(searchResult) > 0 {
  46. keywords := strings.Split(keyword, " ")
  47. for _, item := range searchResult {
  48. for _, word := range keywords {
  49. item.DocumentName = strings.Replace(item.DocumentName, word, "<em>"+word+"</em>", -1)
  50. if item.Description != "" {
  51. src := item.Description
  52. r := []rune(utils.StripTags(item.Description))
  53. if len(r) > 100 {
  54. src = string(r[:100])
  55. } else {
  56. src = string(r)
  57. }
  58. item.Description = strings.Replace(src, word, "<em>"+word+"</em>", -1)
  59. }
  60. }
  61. if item.Identify == "" {
  62. item.Identify = strconv.Itoa(item.DocumentId)
  63. }
  64. if item.ModifyTime.IsZero() {
  65. item.ModifyTime = item.CreateTime
  66. }
  67. }
  68. }
  69. c.Data["Lists"] = searchResult
  70. }
  71. }
  72. //搜索用户
  73. func (c *SearchController) User() {
  74. c.Prepare()
  75. key := c.Ctx.Input.Param(":key")
  76. keyword := strings.TrimSpace(c.GetString("q"))
  77. if key == "" || keyword == "" {
  78. c.JsonResult(404, i18n.Tr(c.Lang, "message.param_error"))
  79. }
  80. keyword = sqltil.EscapeLike(keyword)
  81. book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
  82. if err != nil {
  83. if err == models.ErrPermissionDenied {
  84. c.JsonResult(403, i18n.Tr(c.Lang, "message.no_permission"))
  85. }
  86. c.JsonResult(500, i18n.Tr(c.Lang, "message.item_not_exist"))
  87. }
  88. //members, err := models.NewMemberRelationshipResult().FindNotJoinUsersByAccount(book.BookId, 10, "%"+keyword+"%")
  89. members, err := models.NewMemberRelationshipResult().FindNotJoinUsersByAccountOrRealName(book.BookId, 10, "%"+keyword+"%")
  90. if err != nil {
  91. logs.Error("查询用户列表出错:" + err.Error())
  92. c.JsonResult(500, err.Error())
  93. }
  94. result := models.SelectMemberResult{}
  95. items := make([]models.KeyValueItem, 0)
  96. for _, member := range members {
  97. item := models.KeyValueItem{}
  98. item.Id = member.MemberId
  99. item.Text = member.Account + "[" + member.RealName + "]"
  100. items = append(items, item)
  101. }
  102. result.Result = items
  103. c.JsonResult(0, "OK", result)
  104. }