app_data.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. package http
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/interface/main/creative/model/data"
  6. artmdl "go-common/app/interface/openplatform/article/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/metadata"
  11. "go-common/library/sync/errgroup"
  12. )
  13. func converTmid(c *bm.Context, mid int64) (retMid int64) {
  14. tmidStr := c.Request.Form.Get("tmid")
  15. tmid, _ := strconv.ParseInt(tmidStr, 10, 64)
  16. if tmid > 0 && dataSvc.IsWhite(mid) {
  17. retMid = tmid
  18. } else {
  19. retMid = mid
  20. }
  21. return
  22. }
  23. func appDataArc(c *bm.Context) {
  24. params := c.Request.Form
  25. aidStr := params.Get("aid")
  26. ip := metadata.String(c, metadata.RemoteIP)
  27. midStr, _ := c.Get("mid")
  28. mid := midStr.(int64)
  29. if mid <= 0 {
  30. c.JSON(nil, ecode.CreativeNotLogin)
  31. return
  32. }
  33. // check params
  34. aid, err := strconv.ParseInt(aidStr, 10, 64)
  35. if err != nil {
  36. log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
  37. c.JSON(nil, ecode.RequestErr)
  38. return
  39. }
  40. mid = converTmid(c, mid)
  41. arcStat, err := dataSvc.ArchiveStat(c, aid, mid, ip)
  42. if err != nil {
  43. c.JSON(nil, err)
  44. return
  45. }
  46. _, vds, _ := arcSvc.Videos(c, mid, aid, ip)
  47. arcStat.Videos = vds
  48. c.JSON(arcStat, nil)
  49. }
  50. func appDataVideoQuit(c *bm.Context) {
  51. params := c.Request.Form
  52. cidStr := params.Get("cid")
  53. ip := metadata.String(c, metadata.RemoteIP)
  54. // check user
  55. midStr, _ := c.Get("mid")
  56. mid := midStr.(int64)
  57. if mid <= 0 {
  58. c.JSON(nil, ecode.CreativeNotLogin)
  59. return
  60. }
  61. // check params
  62. cid, err := strconv.ParseInt(cidStr, 10, 64)
  63. if err != nil {
  64. log.Error("strconv.ParseInt(%s) error(%v)", cidStr, err)
  65. c.JSON(nil, ecode.RequestErr)
  66. return
  67. }
  68. mid = converTmid(c, mid)
  69. pts, err := dataSvc.AppVideoQuitPoints(c, cid, mid, ip)
  70. if err != nil {
  71. c.JSON(nil, err)
  72. return
  73. }
  74. c.JSON(pts, nil)
  75. }
  76. func appFan(c *bm.Context) {
  77. req := c.Request
  78. params := req.Form
  79. tyStr := params.Get("type")
  80. ip := metadata.String(c, metadata.RemoteIP)
  81. // check user
  82. midStr, _ := c.Get("mid")
  83. mid := midStr.(int64)
  84. if mid <= 0 {
  85. c.JSON(nil, ecode.CreativeNotLogin)
  86. return
  87. }
  88. var (
  89. err error
  90. ty int
  91. fan *data.AppFan
  92. )
  93. ty, err = strconv.Atoi(tyStr)
  94. if err != nil {
  95. log.Error("strconv.Atoi(%s) error(%v)", tyStr, err)
  96. c.JSON(nil, ecode.RequestErr)
  97. return
  98. }
  99. mid = converTmid(c, mid)
  100. fan, err = dataSvc.UpFansAnalysisForApp(c, mid, ty, ip)
  101. if err != nil {
  102. c.JSON(nil, err)
  103. return
  104. }
  105. c.JSON(fan, nil)
  106. }
  107. func appFanRank(c *bm.Context) {
  108. req := c.Request
  109. params := req.Form
  110. tyStr := params.Get("type")
  111. ip := metadata.String(c, metadata.RemoteIP)
  112. // check user
  113. midStr, _ := c.Get("mid")
  114. mid := midStr.(int64)
  115. if mid <= 0 {
  116. c.JSON(nil, ecode.CreativeNotLogin)
  117. return
  118. }
  119. var (
  120. err error
  121. ty int
  122. rk map[string][]*data.RankInfo
  123. )
  124. ty, err = strconv.Atoi(tyStr)
  125. if err != nil {
  126. log.Error("strconv.Atoi(%s) error(%v)", tyStr, err)
  127. c.JSON(nil, ecode.RequestErr)
  128. return
  129. }
  130. mid = converTmid(c, mid)
  131. rk, err = dataSvc.FanRankApp(c, mid, ty, ip)
  132. if err != nil {
  133. c.JSON(nil, err)
  134. return
  135. }
  136. c.JSON(rk, nil)
  137. }
  138. func appOverView(c *bm.Context) {
  139. req := c.Request
  140. params := req.Form
  141. tyStr := params.Get("type")
  142. ip := metadata.String(c, metadata.RemoteIP)
  143. midStr, _ := c.Get("mid")
  144. mid := midStr.(int64)
  145. if mid <= 0 {
  146. c.JSON(nil, ecode.CreativeNotLogin)
  147. return
  148. }
  149. var (
  150. err error
  151. ty int
  152. ao *data.AppOverView
  153. )
  154. ty, err = strconv.Atoi(tyStr)
  155. if err != nil {
  156. log.Error("strconv.Atoi(%s) error(%v)", tyStr, err)
  157. c.JSON(nil, ecode.RequestErr)
  158. return
  159. }
  160. mid = converTmid(c, mid)
  161. ao, err = dataSvc.OverView(c, mid, int8(ty), ip)
  162. if err != nil {
  163. c.JSON(nil, err)
  164. return
  165. }
  166. c.JSON(ao, nil)
  167. }
  168. func appArchiveAnalyze(c *bm.Context) {
  169. params := c.Request.Form
  170. aidStr := params.Get("aid")
  171. ip := metadata.String(c, metadata.RemoteIP)
  172. midStr, _ := c.Get("mid")
  173. mid := midStr.(int64)
  174. if mid <= 0 {
  175. c.JSON(nil, ecode.CreativeNotLogin)
  176. return
  177. }
  178. aid, err := strconv.ParseInt(aidStr, 10, 64)
  179. if err != nil {
  180. log.Error("strconv.ParseInt(%s) error(%v)", aidStr, err)
  181. c.JSON(nil, ecode.RequestErr)
  182. return
  183. }
  184. mid = converTmid(c, mid)
  185. arcStat, err := dataSvc.ArchiveAnalyze(c, aid, mid, ip)
  186. if err != nil {
  187. c.JSON(nil, err)
  188. return
  189. }
  190. _, vds, _ := arcSvc.Videos(c, mid, aid, ip)
  191. arcStat.Videos = vds
  192. c.JSON(arcStat, nil)
  193. }
  194. func appVideoRetention(c *bm.Context) {
  195. params := c.Request.Form
  196. cidStr := params.Get("cid")
  197. ip := metadata.String(c, metadata.RemoteIP)
  198. midStr, _ := c.Get("mid")
  199. mid := midStr.(int64)
  200. if mid <= 0 {
  201. c.JSON(nil, ecode.CreativeNotLogin)
  202. return
  203. }
  204. cid, err := strconv.ParseInt(cidStr, 10, 64)
  205. if err != nil {
  206. log.Error("strconv.ParseInt(%s) error(%v)", cidStr, err)
  207. c.JSON(nil, ecode.RequestErr)
  208. return
  209. }
  210. mid = converTmid(c, mid)
  211. pts, err := dataSvc.VideoRetention(c, cid, mid, ip)
  212. if err != nil {
  213. c.JSON(nil, err)
  214. return
  215. }
  216. c.JSON(pts, nil)
  217. }
  218. func appDataArticle(c *bm.Context) {
  219. ip := metadata.String(c, metadata.RemoteIP)
  220. midStr, _ := c.Get("mid")
  221. mid := midStr.(int64)
  222. if mid <= 0 {
  223. c.JSON(nil, ecode.CreativeNotLogin)
  224. return
  225. }
  226. var (
  227. artStat artmdl.UpStat
  228. artIncr []*artmdl.ThirtyDayArticle
  229. g = &errgroup.Group{}
  230. ctx = context.TODO()
  231. )
  232. mid = converTmid(c, mid)
  233. g.Go(func() error {
  234. artStat, _ = artSvc.ArticleStat(ctx, mid, ip)
  235. return nil
  236. })
  237. g.Go(func() error {
  238. artIncr, _ = dataSvc.ThirtyDayArticle(ctx, mid, ip)
  239. return nil
  240. })
  241. g.Wait()
  242. c.JSON(map[string]interface{}{
  243. "stat": artStat,
  244. "incr": artIncr,
  245. }, nil)
  246. }