| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package controllers
- import (
- "github.com/astaxie/beego"
- "github.com/lifei6671/mindoc/conf"
- "github.com/lifei6671/mindoc/models"
- "github.com/lifei6671/mindoc/utils/pagination"
- "regexp"
- "strconv"
- "strings"
- )
- type SearchController struct {
- BaseController
- }
- //搜索首页
- func (c *SearchController) Index() {
- c.Prepare()
- c.TplName = "search/index.tpl"
- //如果没有开启你们访问则跳转到登录
- if !c.EnableAnonymous && c.Member == nil {
- c.Redirect(beego.URLFor("AccountController.Login"), 302)
- return
- }
- keyword := c.GetString("keyword")
- pageIndex, _ := c.GetInt("page", 1)
- c.Data["BaseUrl"] = c.BaseUrl()
- if keyword != "" {
- c.Data["Keyword"] = keyword
- member_id := 0
- if c.Member != nil {
- member_id = c.Member.MemberId
- }
- search_result, totalCount, err := models.NewDocumentSearchResult().FindToPager(keyword, pageIndex, conf.PageSize, member_id)
- if err != nil {
- beego.Error(err)
- return
- }
- if totalCount > 0 {
- pager := pagination.NewPagination(c.Ctx.Request,totalCount,conf.PageSize)
- c.Data["PageHtml"] = pager.HtmlPages()
- } else {
- c.Data["PageHtml"] = ""
- }
- if len(search_result) > 0 {
- for _, item := range search_result {
- item.DocumentName = strings.Replace(item.DocumentName, keyword, "<em>"+keyword+"</em>", -1)
- if item.Description != "" {
- src := item.Description
- //将HTML标签全转换成小写
- re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
- src = re.ReplaceAllStringFunc(src, strings.ToLower)
- //去除STYLE
- re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
- src = re.ReplaceAllString(src, "")
- //去除SCRIPT
- re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
- src = re.ReplaceAllString(src, "")
- //去除所有尖括号内的HTML代码,并换成换行符
- re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
- src = re.ReplaceAllString(src, "\n")
- //去除连续的换行符
- re, _ = regexp.Compile("\\s{2,}")
- src = re.ReplaceAllString(src, "\n")
- r := []rune(src)
- if len(r) > 100 {
- src = string(r[:100])
- } else {
- src = string(r)
- }
- item.Description = strings.Replace(src, keyword, "<em>"+keyword+"</em>", -1)
- }
- if item.Identify == "" {
- item.Identify = strconv.Itoa(item.DocumentId)
- }
- if item.ModifyTime.IsZero() {
- item.ModifyTime = item.CreateTime
- }
- }
- }
- c.Data["Lists"] = search_result
- }
- }
- //搜索用户
- func (c *SearchController) User() {
- c.Prepare()
- key := c.Ctx.Input.Param(":key")
- keyword := strings.TrimSpace(c.GetString("q"))
- if key == "" || keyword == ""{
- c.JsonResult(404,"参数错误")
- }
- book, err := models.NewBookResult().FindByIdentify(key, c.Member.MemberId)
- if err != nil {
- if err == models.ErrPermissionDenied {
- c.JsonResult(403,"没有权限")
- }
- c.JsonResult(500,"项目不存在")
- }
- members,err := models.NewMemberRelationshipResult().FindNotJoinUsersByAccount(book.BookId,10,"%"+keyword+"%")
- if err != nil {
- beego.Error("查询用户列表出错:" + err.Error())
- c.JsonResult(500,err.Error())
- }
- result := models.SelectMemberResult{}
- items := make([]models.KeyValueItem,0)
- for _,member := range members {
- item := models.KeyValueItem{}
- item.Id = member.MemberId
- item.Text = member.Account
- items = append(items,item)
- }
- result.Result = items
- c.JsonResult(0,"OK", result)
- }
|