home.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package controllers
  2. import (
  3. "hongze/hongze_cygx/models"
  4. "hongze/hongze_cygx/services"
  5. "hongze/hongze_cygx/utils"
  6. "rdluck_tools/paging"
  7. "strings"
  8. )
  9. type HomeController struct {
  10. BaseCommonController
  11. }
  12. // @Title 首页列表接口
  13. // @Description 首页列表接口
  14. // @Param PageSize query int true "每页数据条数"
  15. // @Param CurrentIndex query int true "当前页页码,从1开始"
  16. // @Param ChartPermissionId query int true "品类id,最新传0"
  17. // @Success 200 {object} models.HomeListResp
  18. // @router /list [get]
  19. func (this *HomeController) ListHome() {
  20. br := new(models.BaseResponse).Init()
  21. defer func() {
  22. this.Data["json"] = br
  23. this.ServeJSON()
  24. }()
  25. pageSize, _ := this.GetInt("PageSize")
  26. currentIndex, _ := this.GetInt("CurrentIndex")
  27. chartPermissionId, _ := this.GetInt("ChartPermissionId")
  28. var startSize int
  29. if pageSize <= 0 {
  30. pageSize = utils.PageSize20
  31. }
  32. if currentIndex <= 0 {
  33. currentIndex = 1
  34. }
  35. startSize = paging.StartIndex(currentIndex, pageSize)
  36. var condition string
  37. var pars []interface{}
  38. var total int
  39. resp := new(models.HomeListResp)
  40. page := paging.GetPaging(currentIndex, pageSize, total)
  41. condition += ` AND is_summary = 1 `
  42. if chartPermissionId > 0 {
  43. categoryId, err := models.GetCategoryId(chartPermissionId)
  44. if err != nil && err.Error() != utils.ErrNoRow() {
  45. br.Msg = "获取信息失败"
  46. br.ErrMsg = "获取分类权限信息失败,Err:" + err.Error()
  47. return
  48. }
  49. categoryinfo, err := models.GetChartPermissionById(chartPermissionId)
  50. if err != nil {
  51. br.Msg = "获取信息失败"
  52. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  53. return
  54. }
  55. page = paging.GetPaging(currentIndex, pageSize, total)
  56. if categoryId != "" {
  57. condition += ` AND category_id IN(` + categoryId + `)`
  58. condition += ` OR ( category_name LIKE '%` + categoryinfo.PermissionName + `%' AND publish_status = 1 AND is_summary = 1 )`
  59. } else {
  60. condition += ` AND category_name LIKE '%` + categoryinfo.PermissionName + `%'`
  61. }
  62. }
  63. //condition = ` AND a.category_id NOT IN (85,71) `
  64. total, err := models.GetHomeCount(condition, pars)
  65. if err != nil {
  66. br.Msg = "获取信息失败"
  67. br.Msg = "获取帖子总数失败,Err:" + err.Error()
  68. return
  69. }
  70. page = paging.GetPaging(currentIndex, pageSize, total)
  71. list, err := models.GetHomeList(condition, pars, startSize, pageSize)
  72. if err != nil {
  73. br.Msg = "获取信息失败"
  74. br.Msg = "获取帖子数据失败,Err:" + err.Error()
  75. return
  76. }
  77. lenList := len(list)
  78. for i := 0; i < lenList; i++ {
  79. item := list[i]
  80. list[i].Body, _ = services.GetReportContentTextSub(item.Body)
  81. list[i].PublishDate = utils.StrTimeToTime(item.PublishDate).Format(utils.FormatDateTimeNoSecond) //时间字符串格式转时间格式
  82. if strings.Contains(item.CategoryName, "研选") {
  83. list[i].IsResearch = true
  84. }
  85. if item.Pv > 999 {
  86. list[i].Pv = 999
  87. }
  88. }
  89. resp.List = list
  90. resp.Paging = page
  91. br.Ret = 200
  92. br.Success = true
  93. br.Msg = "获取成功"
  94. br.Data = resp
  95. }