search.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package controllers
  2. import (
  3. "github.com/rdlucklib/rdluck_tools/paging"
  4. "hongze/hongze_mfyx/models"
  5. "hongze/hongze_mfyx/services"
  6. "hongze/hongze_mfyx/utils"
  7. )
  8. type SearchController struct {
  9. BaseAuthController
  10. }
  11. type BaseSearchController struct {
  12. BaseCommonController
  13. }
  14. // @Title 综合搜索接口
  15. // @Description 综合搜索接口
  16. // @Param PageSize query int true "每页数据条数"
  17. // @Param CurrentIndex query int true "当前页页码,从1开始"
  18. // @Param KeyWord query string true "搜索关键词"
  19. // @Success 200 {object} models.SearchItem
  20. // @router /list [get]
  21. func (this *SearchController) List() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. pageSize, _ := this.GetInt("PageSize")
  28. currentIndex, _ := this.GetInt("CurrentIndex")
  29. var startSize int
  30. if pageSize <= 0 {
  31. pageSize = utils.PageSize20
  32. }
  33. if currentIndex <= 0 {
  34. currentIndex = 1
  35. }
  36. startSize = paging.StartIndex(currentIndex, pageSize)
  37. keyWord := this.GetString("KeyWord")
  38. if keyWord == "" {
  39. br.Msg = "请输入搜索词"
  40. br.ErrMsg = "请输入搜索词"
  41. return
  42. }
  43. user := this.User
  44. if user == nil {
  45. br.Msg = "请重新登录"
  46. br.Ret = 408
  47. return
  48. }
  49. resp := new(models.HomeResourceDataListResp)
  50. tmpResult, tmpTotalResult, err := services.EsComprehensiveSearch(keyWord, startSize, pageSize)
  51. if err != nil {
  52. br.Msg = "检索失败"
  53. br.ErrMsg = "检索失败,Err:" + err.Error()
  54. return
  55. }
  56. //for _, v := range tmpResult {
  57. // fmt.Println(v.Title, "Title", v.IsSummary)
  58. //}
  59. list, err := services.GetResourceDataEsList(tmpResult, user)
  60. if err != nil {
  61. br.Msg = "获取失败"
  62. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  63. return
  64. }
  65. if currentIndex == 1 {
  66. go services.AddSearchKeyWord(user, keyWord, 1)
  67. }
  68. resp.List = list
  69. page := paging.GetPaging(currentIndex, pageSize, int(tmpTotalResult))
  70. resp.Paging = page
  71. br.Ret = 200
  72. br.Success = true
  73. br.Msg = "获取成功"
  74. br.Data = resp
  75. }