recommends.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package http
  2. import (
  3. "strconv"
  4. "time"
  5. "go-common/app/interface/openplatform/article/conf"
  6. "go-common/app/interface/openplatform/article/dao"
  7. "go-common/app/interface/openplatform/article/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/metadata"
  12. "go-common/library/xstr"
  13. )
  14. const (
  15. _headerBuvid = "Buvid"
  16. _buvid = "buvid3"
  17. _recommendRegion = 0
  18. _rankPage = 9
  19. )
  20. func recommends(c *bm.Context) {
  21. var (
  22. mid int64
  23. rs []*model.RecommendArtWithLike
  24. err error
  25. params = c.Request.Form
  26. aids []int64
  27. sky *model.SkyHorseResp
  28. )
  29. if aids, _ = xstr.SplitInts(params.Get("aids")); len(aids) == 0 {
  30. aids, _ = xstr.SplitInts(params.Get("adis")) //兼容ios客户端bug
  31. }
  32. cid, _ := strconv.ParseInt(params.Get("cid"), 10, 64)
  33. sort, _ := strconv.Atoi(params.Get("sort"))
  34. pn, _ := strconv.ParseInt(params.Get("pn"), 10, 64)
  35. ps, _ := strconv.ParseInt(params.Get("ps"), 10, 64)
  36. if pn <= 0 {
  37. pn = 1
  38. }
  39. if pn > conf.Conf.Article.MaxRecommendPnSize {
  40. c.JSON(nil, ecode.RequestErr)
  41. return
  42. }
  43. if ps <= 0 {
  44. ps = 20
  45. } else if ps > conf.Conf.Article.MaxRecommendPsSize {
  46. ps = conf.Conf.Article.MaxRecommendPsSize
  47. }
  48. device := params.Get("device")
  49. mobiApp := params.Get("mobi_app")
  50. plat := model.Plat(mobiApp, device)
  51. if midInter, ok := c.Get("mid"); ok {
  52. mid = midInter.(int64)
  53. }
  54. pageType, _ := strconv.Atoi(params.Get("from"))
  55. if pageType == 0 {
  56. // 分区页
  57. pageType = 2
  58. }
  59. buildStr := params.Get("build")
  60. build, _ := strconv.Atoi(buildStr)
  61. buvid := buvid(c)
  62. if rs, sky, err = artSrv.SkyHorse(c, cid, int(pn), int(ps), aids, sort, mid, build, buvid, plat); err != nil {
  63. dao.PromError("推荐接口")
  64. log.Error("service.Recommends(%d) error(%+v)", mid, err)
  65. c.JSON(nil, err)
  66. return
  67. }
  68. var as []*model.Meta
  69. for _, r := range rs {
  70. as = append(as, &r.Meta)
  71. }
  72. artSrv.RecommendInfoc(mid, plat, pageType, int(cid), build, buvid, metadata.String(c, metadata.RemoteIP), as, false, time.Now(), pn, sky)
  73. if rs == nil {
  74. rs = []*model.RecommendArtWithLike{}
  75. }
  76. res := make(map[string]interface{})
  77. res["aids_len"] = conf.Conf.Article.RecommendAidLen
  78. res["data"] = rs
  79. c.JSONMap(res, nil)
  80. }
  81. func home(c *bm.Context) {
  82. var (
  83. mid int64
  84. rs *model.RecommendHome
  85. err error
  86. params = c.Request.Form
  87. ip = metadata.String(c, metadata.RemoteIP)
  88. aids []int64
  89. sky *model.SkyHorseResp
  90. )
  91. aids, _ = xstr.SplitInts(params.Get("aids"))
  92. pn, _ := strconv.ParseInt(params.Get("pn"), 10, 64)
  93. ps, _ := strconv.ParseInt(params.Get("ps"), 10, 64)
  94. if pn <= 0 {
  95. pn = 1
  96. }
  97. if pn > conf.Conf.Article.MaxRecommendPnSize {
  98. c.JSON(nil, ecode.RequestErr)
  99. return
  100. }
  101. if ps <= 0 {
  102. ps = 20
  103. } else if ps > conf.Conf.Article.MaxRecommendPsSize {
  104. ps = conf.Conf.Article.MaxRecommendPsSize
  105. }
  106. device := params.Get("device")
  107. mobiApp := params.Get("mobi_app")
  108. plat := model.Plat(mobiApp, device)
  109. if midInter, ok := c.Get("mid"); ok {
  110. mid = midInter.(int64)
  111. }
  112. pageType, _ := strconv.Atoi(params.Get("from"))
  113. if pageType == 0 {
  114. // 首页tab
  115. pageType = 8
  116. }
  117. buildStr := params.Get("build")
  118. build, _ := strconv.Atoi(buildStr)
  119. buvid := buvid(c)
  120. if rs, sky, err = artSrv.RecommendHome(c, int8(plat), build, int(pn), int(ps), aids, mid, ip, time.Now(), buvid); err != nil {
  121. dao.PromError("推荐接口")
  122. log.Error("service.Recommends(%d) error(%+v)", mid, err)
  123. c.JSON(nil, ecode.Degrade)
  124. return
  125. }
  126. var as []*model.Meta
  127. for _, r := range rs.Articles {
  128. as = append(as, &r.Meta)
  129. }
  130. artSrv.RecommendInfoc(mid, plat, pageType, _recommendRegion, build, buvid, metadata.String(c, metadata.RemoteIP), as, false, time.Now(), pn, sky)
  131. if len(rs.Ranks) > 0 && pn == 1 {
  132. var as []*model.Meta
  133. for _, r := range rs.Ranks {
  134. as = append(as, r.Meta)
  135. }
  136. artSrv.RecommendInfoc(mid, plat, _rankPage, 0, build, buvid, metadata.String(c, metadata.RemoteIP), as, false, time.Now(), pn, nil)
  137. }
  138. res := make(map[string]interface{})
  139. res["aids_len"] = conf.Conf.Article.RecommendAidLen
  140. res["data"] = rs
  141. c.JSONMap(res, nil)
  142. }
  143. func recommendsPlus(c *bm.Context) {
  144. var (
  145. mid int64
  146. rs *model.RecommendPlus
  147. err error
  148. params = c.Request.Form
  149. aids []int64
  150. sky *model.SkyHorseResp
  151. )
  152. if aids, _ = xstr.SplitInts(params.Get("aids")); len(aids) == 0 {
  153. aids, _ = xstr.SplitInts(params.Get("adis")) //兼容ios客户端bug
  154. }
  155. cid, _ := strconv.ParseInt(params.Get("cid"), 10, 64)
  156. sort, _ := strconv.Atoi(params.Get("sort"))
  157. pn, _ := strconv.ParseInt(params.Get("pn"), 10, 64)
  158. ps, _ := strconv.ParseInt(params.Get("ps"), 10, 64)
  159. if pn <= 0 {
  160. pn = 1
  161. }
  162. if pn > conf.Conf.Article.MaxRecommendPnSize {
  163. c.JSON(nil, ecode.RequestErr)
  164. return
  165. }
  166. if ps <= 0 {
  167. ps = 20
  168. } else if ps > conf.Conf.Article.MaxRecommendPsSize {
  169. ps = conf.Conf.Article.MaxRecommendPsSize
  170. }
  171. device := params.Get("device")
  172. mobiApp := params.Get("mobi_app")
  173. plat := model.Plat(mobiApp, device)
  174. if midInter, ok := c.Get("mid"); ok {
  175. mid = midInter.(int64)
  176. }
  177. pageType, _ := strconv.Atoi(params.Get("from"))
  178. if pageType == 0 {
  179. // 分区页
  180. pageType = 2
  181. }
  182. buildStr := params.Get("build")
  183. build, _ := strconv.Atoi(buildStr)
  184. buvid := buvid(c)
  185. if rs, sky, err = artSrv.RecommendPlus(c, cid, int8(plat), build, int(pn), int(ps), aids, mid, time.Now(), sort, buvid); err != nil {
  186. c.JSON(nil, err)
  187. dao.PromError("推荐接口")
  188. log.Error("service.Recommends(%d) error(%+v)", mid, err)
  189. return
  190. }
  191. var as []*model.Meta
  192. for _, r := range rs.Articles {
  193. as = append(as, &r.Meta)
  194. }
  195. artSrv.RecommendInfoc(mid, plat, pageType, int(cid), build, buvid, metadata.String(c, metadata.RemoteIP), as, false, time.Now(), pn, sky)
  196. if len(rs.Ranks) > 0 && pn == 1 {
  197. var as []*model.Meta
  198. for _, r := range rs.Ranks {
  199. as = append(as, r.Meta)
  200. }
  201. artSrv.RecommendInfoc(mid, plat, _rankPage, 0, build, buvid, metadata.String(c, metadata.RemoteIP), as, false, time.Now(), pn, nil)
  202. }
  203. if rs.Articles == nil {
  204. rs.Articles = []*model.RecommendArtWithLike{}
  205. }
  206. res := make(map[string]interface{})
  207. res["aids_len"] = conf.Conf.Article.RecommendAidLen
  208. res["data"] = rs
  209. c.JSONMap(res, nil)
  210. }
  211. func allRecommends(c *bm.Context) {
  212. v := new(struct {
  213. Pn int `form:"pn" validate:"min=1"`
  214. Ps int `form:"ps" validate:"min=1"`
  215. })
  216. if err := c.Bind(v); err != nil {
  217. c.JSON(nil, ecode.RequestErr)
  218. return
  219. }
  220. count, recommends, err := artSrv.AllRecommends(c, v.Pn, v.Ps)
  221. c.JSON(map[string]interface{}{
  222. "total": count,
  223. "list": recommends,
  224. }, err)
  225. }