SearchController.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package controllers
  2. import (
  3. "github.com/astaxie/beego"
  4. "github.com/lifei6671/mindoc/conf"
  5. "github.com/lifei6671/mindoc/models"
  6. "github.com/lifei6671/mindoc/utils"
  7. "github.com/lifei6671/mindoc/utils/pagination"
  8. "github.com/lifei6671/mindoc/utils/sqltil"
  9. "strconv"
  10. "strings"
  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. return
  36. }
  37. if totalCount > 0 {
  38. pager := pagination.NewPagination(c.Ctx.Request, totalCount, conf.PageSize,c.BaseUrl())
  39. c.Data["PageHtml"] = pager.HtmlPages()
  40. } else {
  41. c.Data["PageHtml"] = ""
  42. }
  43. if len(searchResult) > 0 {
  44. keywords := strings.Split(keyword," ")
  45. for _, item := range searchResult {
  46. for _,word := range keywords {
  47. item.DocumentName = strings.Replace(item.DocumentName, word, "<em>"+word+"</em>", -1)
  48. if item.Description != "" {
  49. src := item.Description
  50. r := []rune(utils.StripTags(item.Description))
  51. if len(r) > 100 {
  52. src = string(r[:100])
  53. } else {
  54. src = string(r)
  55. }
  56. item.Description = strings.Replace(src, word, "<em>"+word+"</em>", -1)
  57. }
  58. }
  59. if item.Identify == "" {
  60. item.Identify = strconv.Itoa(item.DocumentId)
  61. }
  62. if item.ModifyTime.IsZero() {
  63. item.ModifyTime = item.CreateTime
  64. }
  65. }
  66. }
  67. c.Data["Lists"] = searchResult
  68. }
  69. }
  70. //搜索用户
  71. func (c *SearchController) User() {
  72. c.Prepare()
  73. key := c.Ctx.Input.Param(":key")
  74. keyword := strings.TrimSpace(c.GetString("q"))
  75. if key == "" || keyword == "" {
  76. c.JsonResult(404, "参数错误")
  77. }
  78. keyword = sqltil.EscapeLike(keyword)
  79. book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
  80. if err != nil {
  81. if err == models.ErrPermissionDenied {
  82. c.JsonResult(403, "没有权限")
  83. }
  84. c.JsonResult(500, "项目不存在")
  85. }
  86. members, err := models.NewMemberRelationshipResult().FindNotJoinUsersByAccount(book.BookId, 10, "%"+keyword+"%")
  87. if err != nil {
  88. beego.Error("查询用户列表出错:" + err.Error())
  89. c.JsonResult(500, err.Error())
  90. }
  91. result := models.SelectMemberResult{}
  92. items := make([]models.KeyValueItem, 0)
  93. for _, member := range members {
  94. item := models.KeyValueItem{}
  95. item.Id = member.MemberId
  96. item.Text = member.Account
  97. items = append(items, item)
  98. }
  99. result.Result = items
  100. c.JsonResult(0, "OK", result)
  101. }