search.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/pagination"
  7. "regexp"
  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. //将HTML标签全转换成小写
  49. re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
  50. src = re.ReplaceAllStringFunc(src, strings.ToLower)
  51. //去除STYLE
  52. re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
  53. src = re.ReplaceAllString(src, "")
  54. //去除SCRIPT
  55. re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
  56. src = re.ReplaceAllString(src, "")
  57. //去除所有尖括号内的HTML代码,并换成换行符
  58. re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
  59. src = re.ReplaceAllString(src, "\n")
  60. //去除连续的换行符
  61. re, _ = regexp.Compile("\\s{2,}")
  62. src = re.ReplaceAllString(src, "\n")
  63. r := []rune(src)
  64. if len(r) > 100 {
  65. src = string(r[:100])
  66. } else {
  67. src = string(r)
  68. }
  69. item.Description = strings.Replace(src, keyword, "<em>"+keyword+"</em>", -1)
  70. }
  71. if item.Identify == "" {
  72. item.Identify = strconv.Itoa(item.DocumentId)
  73. }
  74. if item.ModifyTime.IsZero() {
  75. item.ModifyTime = item.CreateTime
  76. }
  77. }
  78. }
  79. c.Data["Lists"] = search_result
  80. }
  81. }
  82. //搜索用户
  83. func (c *SearchController) User() {
  84. c.Prepare()
  85. key := c.Ctx.Input.Param(":key")
  86. keyword := strings.TrimSpace(c.GetString("q"))
  87. if key == "" || keyword == ""{
  88. c.JsonResult(404,"参数错误")
  89. }
  90. book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
  91. if err != nil {
  92. if err == models.ErrPermissionDenied {
  93. c.JsonResult(403,"没有权限")
  94. }
  95. c.JsonResult(500,"项目不存在")
  96. }
  97. members,err := models.NewMemberRelationshipResult().FindNotJoinUsersByAccount(book.BookId,10,"%"+keyword+"%")
  98. if err != nil {
  99. beego.Error("查询用户列表出错:" + err.Error())
  100. c.JsonResult(500,err.Error())
  101. }
  102. result := models.SelectMemberResult{}
  103. items := make([]models.KeyValueItem,0)
  104. for _,member := range members {
  105. item := models.KeyValueItem{}
  106. item.Id = member.MemberId
  107. item.Text = member.Account
  108. items = append(items,item)
  109. }
  110. result.Result = items
  111. c.JsonResult(0,"OK", result)
  112. }