collection.go 2.8 KB

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