favorite.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package http
  2. import (
  3. "strconv"
  4. "go-common/app/interface/openplatform/article/conf"
  5. "go-common/app/interface/openplatform/article/dao"
  6. artmdl "go-common/app/interface/openplatform/article/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/log/infoc"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/metadata"
  12. )
  13. func addFavorite(c *bm.Context) {
  14. var (
  15. err error
  16. mid, aid, fid int64
  17. params = c.Request.Form
  18. ip = metadata.String(c, metadata.RemoteIP)
  19. )
  20. midInter, _ := c.Get("mid")
  21. mid = midInter.(int64)
  22. aidStr := params.Get("id")
  23. if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil || aid <= 0 {
  24. c.JSON(nil, ecode.RequestErr)
  25. return
  26. }
  27. fidStr := params.Get("fid")
  28. if fidStr != "" {
  29. if fid, err = strconv.ParseInt(fidStr, 10, 64); err != nil || fid < 0 {
  30. c.JSON(nil, ecode.RequestErr)
  31. return
  32. }
  33. }
  34. meta, err := artSrv.ArticleMeta(c, aid)
  35. if err != nil {
  36. c.JSON(nil, err)
  37. return
  38. }
  39. if meta == nil {
  40. c.JSON(nil, ecode.NothingFound)
  41. return
  42. }
  43. err = artSrv.AddFavorite(c, mid, aid, fid, ip)
  44. artSrv.CheatInfoc.InfoAntiCheat2(c, strconv.FormatInt(meta.Author.Mid, 10), "", strconv.FormatInt(mid, 10), aidStr, "article", infoc.ActionFav, fidStr)
  45. c.JSON(nil, err)
  46. }
  47. func delFavorite(c *bm.Context) {
  48. var (
  49. err error
  50. mid, aid, fid int64
  51. params = c.Request.Form
  52. ip = metadata.String(c, metadata.RemoteIP)
  53. )
  54. midInter, _ := c.Get("mid")
  55. mid = midInter.(int64)
  56. aidStr := params.Get("id")
  57. if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil || aid <= 0 {
  58. c.JSON(nil, ecode.RequestErr)
  59. return
  60. }
  61. fidStr := params.Get("fid")
  62. if fidStr != "" {
  63. if fid, err = strconv.ParseInt(fidStr, 10, 64); err != nil || fid < 0 {
  64. c.JSON(nil, ecode.RequestErr)
  65. return
  66. }
  67. }
  68. if err = artSrv.DelFavorite(c, mid, aid, fid, ip); err != nil {
  69. dao.PromError("删除收藏")
  70. log.Error("artSrv.DelFavorite(%d,%d,%d) error(%+v)", mid, aid, fid, err)
  71. }
  72. c.JSON(nil, err)
  73. }
  74. func favorites(c *bm.Context) {
  75. var (
  76. err error
  77. mid, fid int64
  78. pn, ps int
  79. favs []*artmdl.Favorite
  80. page *artmdl.Page
  81. params = c.Request.Form
  82. ip = metadata.String(c, metadata.RemoteIP)
  83. )
  84. midInter, _ := c.Get("mid")
  85. mid = midInter.(int64)
  86. fidStr := params.Get("fid")
  87. if fidStr != "" {
  88. if fid, err = strconv.ParseInt(fidStr, 10, 64); err != nil || fid < 0 {
  89. c.JSON(nil, ecode.RequestErr)
  90. return
  91. }
  92. }
  93. pnStr := params.Get("pn")
  94. if pn, err = strconv.Atoi(pnStr); err != nil || pn < 1 {
  95. pn = 1
  96. }
  97. psStr := params.Get("ps")
  98. if ps, err = strconv.Atoi(psStr); err != nil || ps < 1 {
  99. ps = conf.Conf.Article.CreationDefaultSize
  100. } else if ps > conf.Conf.Article.CreationMaxSize {
  101. ps = conf.Conf.Article.CreationMaxSize
  102. }
  103. if favs, page, err = artSrv.ValidFavs(c, mid, fid, pn, ps, ip); err != nil {
  104. c.JSON(nil, err)
  105. return
  106. }
  107. type data struct {
  108. Favorites []*artmdl.Favorite `json:"favorites"`
  109. Page *artmdl.Page `json:"page"`
  110. }
  111. c.JSON(&data{
  112. Favorites: favs,
  113. Page: page,
  114. }, nil)
  115. }
  116. func allFavorites(c *bm.Context) {
  117. var (
  118. err error
  119. mid, fid int64
  120. pn, ps int
  121. favs []*artmdl.Favorite
  122. page *artmdl.Page
  123. params = c.Request.Form
  124. ip = metadata.String(c, metadata.RemoteIP)
  125. )
  126. midInter, _ := c.Get("mid")
  127. mid = midInter.(int64)
  128. fidStr := params.Get("fid")
  129. if fidStr != "" {
  130. if fid, err = strconv.ParseInt(fidStr, 10, 64); err != nil || fid < 0 {
  131. c.JSON(nil, ecode.RequestErr)
  132. return
  133. }
  134. }
  135. pnStr := params.Get("pn")
  136. if pn, err = strconv.Atoi(pnStr); err != nil || pn < 1 {
  137. pn = 1
  138. }
  139. psStr := params.Get("ps")
  140. if ps, err = strconv.Atoi(psStr); err != nil || ps < 1 {
  141. ps = conf.Conf.Article.CreationDefaultSize
  142. } else if ps > conf.Conf.Article.CreationMaxSize {
  143. ps = conf.Conf.Article.CreationMaxSize
  144. }
  145. if favs, page, err = artSrv.Favs(c, mid, fid, pn, ps, ip); err != nil {
  146. c.JSON(nil, err)
  147. return
  148. }
  149. type data struct {
  150. Favorites []*artmdl.Favorite `json:"favorites"`
  151. Page *artmdl.Page `json:"page"`
  152. }
  153. c.JSON(&data{
  154. Favorites: favs,
  155. Page: page,
  156. }, nil)
  157. }