1
0

SearchController.go 3.1 KB

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