search.go 2.7 KB

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