collection.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package collection
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "hongze/hongze_yb/controller/response"
  6. "hongze/hongze_yb/models/request"
  7. responseModel "hongze/hongze_yb/models/response"
  8. "hongze/hongze_yb/services"
  9. "hongze/hongze_yb/services/collection"
  10. "hongze/hongze_yb/services/user"
  11. )
  12. // List 收藏列表
  13. // @Description 收藏列表
  14. // @Param from_type query int false "来源类型:0-全部; 1-研报; 2-线上路演; 3-视频社区"
  15. // @Param keywords query string false "搜索关键词"
  16. // @Param curr_page query int false "当前页码"
  17. // @Param page_size query int false "每页数量"
  18. // @Success 200 {object}
  19. // @failure 400 {string} string "活动获取失败"
  20. // @Router /collection/list [get]
  21. func List(c *gin.Context) {
  22. var req request.CollectionListReq
  23. if err := c.Bind(&req); err != nil {
  24. response.Fail("参数有误", c)
  25. return
  26. }
  27. userInfo := user.GetInfoByClaims(c)
  28. page := services.GetCurrPageByClaims(c)
  29. pageSize := services.GetPageSizeByClaims(c)
  30. total, list, e := collection.GetCollectionList(int(userInfo.UserID), req.FromType, page, pageSize, req.Keywords)
  31. if e != nil {
  32. fmt.Println(e.Error())
  33. response.FailMsg("获取收藏列表失败", "获取收藏列表失败, Err: "+e.Error(), c)
  34. return
  35. }
  36. response.OkData("获取成功", &responseModel.CollectionListResp{
  37. List: list,
  38. Paging: responseModel.GetPaging(page, pageSize, total),
  39. }, c)
  40. }
  41. // Collect
  42. // @Description 加入收藏
  43. // @Success 200 {string} string "操作成功"
  44. // @Router /collection/collect [post]
  45. func Collect(c *gin.Context) {
  46. var req request.CollectionCollectReq
  47. if c.ShouldBind(&req) != nil {
  48. response.Fail("参数有误", c)
  49. return
  50. }
  51. if req.CollectionType <= 0 {
  52. response.Fail("收藏类型有误", c)
  53. return
  54. }
  55. if req.PrimaryId <= 0 {
  56. response.Fail("参数有误", c)
  57. return
  58. }
  59. userInfo := user.GetInfoByClaims(c)
  60. if userInfo.UserID <= 0 {
  61. response.Fail("请登录后操作", c)
  62. return
  63. }
  64. collectionId, e := collection.AddCollection(int(userInfo.UserID), req.CollectionType, req.PrimaryId, req.ExtendId, req.SourceAgent)
  65. if e != nil {
  66. response.FailMsg("操作失败", "加入收藏失败, Err: "+e.Error(), c)
  67. return
  68. }
  69. response.OkData("操作成功", collectionId, c)
  70. }
  71. // Cancel
  72. // @Description 取消收藏
  73. // @Success 200 {string} string "操作成功"
  74. // @Router /collection/cancel [post]
  75. func Cancel(c *gin.Context) {
  76. var req request.CollectionCancelReq
  77. if c.ShouldBind(&req) != nil {
  78. response.Fail("参数有误", c)
  79. return
  80. }
  81. if req.CollectionId <= 0 {
  82. response.Fail("参数有误", c)
  83. return
  84. }
  85. userInfo := user.GetInfoByClaims(c)
  86. if userInfo.UserID <= 0 {
  87. response.Fail("请登录后操作", c)
  88. return
  89. }
  90. if e := collection.CancelCollection(int(userInfo.UserID), req.CollectionId); e != nil {
  91. response.FailMsg("操作失败", "取消收藏失败, Err: "+e.Error(), c)
  92. return
  93. }
  94. response.Ok("操作成功", c)
  95. }