1
0

SearchController.go 2.8 KB

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