home_controller.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package home
  2. import (
  3. logger "eta/eta_mini_ht_api/common/component/log"
  4. "eta/eta_mini_ht_api/common/exception"
  5. "eta/eta_mini_ht_api/common/utils/page"
  6. "eta/eta_mini_ht_api/controllers"
  7. homeService "eta/eta_mini_ht_api/service/home"
  8. "eta/eta_mini_ht_api/service/user"
  9. "time"
  10. )
  11. type HomeController struct {
  12. controllers.ListController
  13. }
  14. func isLogin(listType string) bool {
  15. if listType == "logout" {
  16. logger.Info("当前用户未登录,展示部分详情")
  17. return false
  18. }
  19. if listType == "login" {
  20. return true
  21. }
  22. return false
  23. }
  24. // Search 搜索报告列表
  25. // @Description 搜索报告列表
  26. // @Success 200 {object}
  27. // @router /search [get]
  28. func (r *HomeController) Search(key string) {
  29. controllers.Wrap(&r.BaseController, func() (result *controllers.WrapData, err error) {
  30. result = r.InitWrapData("分页搜索列表失败")
  31. if key == "" {
  32. err = exception.New(exception.SearchKeyEmptyError)
  33. r.FailedResult("分页搜索报告列表失败", result)
  34. return
  35. }
  36. detailType := r.Data["detailType"].(string)
  37. userInfo := r.Data["user"].(user.User)
  38. pageRes := page.Page{
  39. Current: r.PageInfo.Current,
  40. PageSize: r.PageInfo.PageSize,
  41. }
  42. if r.PageInfo.TimeStamp == 0 {
  43. r.PageInfo.TimeStamp = time.Now().Unix()
  44. } else {
  45. pageRes.LatestId = r.PageInfo.LatestId
  46. pageRes.Total = r.PageInfo.Total
  47. }
  48. var total int64
  49. var list []homeService.HomeSearch
  50. list, total, err = homeService.Search(key, r.PageInfo, isLogin(detailType), userInfo.Id)
  51. if err != nil {
  52. r.FailedResult("分页搜索报告列表失败", result)
  53. return
  54. }
  55. var dataList []interface{}
  56. for _, item := range list {
  57. switch item.ResultType {
  58. case homeService.ReportResultType:
  59. dataList = append(dataList, item.Report)
  60. case homeService.MediaResultType:
  61. dataList = append(dataList, item.Media)
  62. }
  63. }
  64. r.PageInfo.Total = total
  65. pageRes.TotalPage = page.TotalPages(pageRes.Total, pageRes.PageSize)
  66. reports := new(page.PageResult)
  67. reports.Data = dataList
  68. reports.Page = pageRes
  69. r.SuccessResult("分页搜索报告列表成功", reports, result)
  70. return
  71. })
  72. }