view.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. package http
  2. import (
  3. "context"
  4. "strconv"
  5. "time"
  6. "go-common/app/interface/main/app-view/model"
  7. "go-common/app/interface/main/app-view/model/view"
  8. resource "go-common/app/service/main/resource/model"
  9. "go-common/library/ecode"
  10. bm "go-common/library/net/http/blademaster"
  11. "go-common/library/net/metadata"
  12. )
  13. const (
  14. _viewPath = "/x/v2/view"
  15. _viewPagePath = "/x/v2/view/page"
  16. _headerBuvid = "Buvid"
  17. )
  18. var (
  19. _rate = []int64{564, 1328, 2592, 4192}
  20. _formats = map[int]string{1: "mp4", 2: "hdmp4", 3: "flv", 4: "flv"}
  21. _dislike = []*view.Dislike{
  22. {
  23. ID: 5,
  24. Name: "标题党/封面党",
  25. },
  26. {
  27. ID: 6,
  28. Name: "内容质量差",
  29. },
  30. {
  31. ID: 7,
  32. Name: "内容/封面令人不适",
  33. },
  34. {
  35. ID: 8,
  36. Name: "营销广告",
  37. },
  38. }
  39. )
  40. // viewIndex view handler
  41. func viewIndex(c *bm.Context) {
  42. var (
  43. mid, aid, movieID int64
  44. err error
  45. parentMode int
  46. )
  47. params := c.Request.Form
  48. header := c.Request.Header
  49. // get params
  50. aidStr := params.Get("aid")
  51. movieidStr := params.Get("movie_id")
  52. mobiApp := params.Get("mobi_app")
  53. device := params.Get("device")
  54. ak := params.Get("access_key")
  55. buildStr := params.Get("build")
  56. from := params.Get("from")
  57. trackid := params.Get("trackid")
  58. network := params.Get("network")
  59. adExtra := params.Get("ad_extra")
  60. parentModeStr := params.Get("parent_mode")
  61. parentMode, _ = strconv.Atoi(parentModeStr)
  62. qnStr := params.Get("qn")
  63. qn, _ := strconv.Atoi(qnStr)
  64. fnverStr := params.Get("fnver")
  65. fnver, _ := strconv.Atoi(fnverStr)
  66. fnvalStr := params.Get("fnval")
  67. fnval, _ := strconv.Atoi(fnvalStr)
  68. forceHost, _ := strconv.Atoi(params.Get("force_host"))
  69. spmid := params.Get("spmid")
  70. fromSpmid := params.Get("from_spmid")
  71. // check params
  72. if aidStr == "" && movieidStr == "" {
  73. c.JSON(nil, ecode.RequestErr)
  74. return
  75. }
  76. if aidStr != "" && aidStr != "0" {
  77. if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil {
  78. c.JSON(nil, ecode.RequestErr)
  79. return
  80. }
  81. } else if movieidStr != "" && movieidStr != "0" {
  82. if movieID, err = strconv.ParseInt(movieidStr, 10, 64); err != nil {
  83. c.JSON(nil, ecode.RequestErr)
  84. return
  85. }
  86. }
  87. if aid < 1 && movieID < 1 {
  88. c.JSON(nil, ecode.RequestErr)
  89. return
  90. }
  91. build, err := strconv.Atoi(buildStr)
  92. if err != nil {
  93. c.JSON(nil, ecode.RequestErr)
  94. return
  95. }
  96. plat := model.Plat(mobiApp, device)
  97. buvid := header.Get("Buvid")
  98. disid := header.Get("Display-ID")
  99. cdnIP := header.Get("X-Cache-Server-Addr")
  100. if midInter, ok := c.Get("mid"); ok {
  101. mid = midInter.(int64)
  102. }
  103. autoplay, _ := strconv.Atoi(params.Get("autoplay"))
  104. now := time.Now()
  105. // view
  106. ip := metadata.String(c, metadata.RemoteIP)
  107. viewSvr.ViewInfoc(mid, int(plat), trackid, aidStr, ip, _viewPath, buildStr, buvid, disid, from, now, err, autoplay, spmid, fromSpmid)
  108. data, err := viewSvr.View(c, mid, aid, movieID, plat, build, qn, fnver, fnval, forceHost, parentMode, ak, mobiApp, device, buvid, cdnIP, network, adExtra, from, now)
  109. if err != nil {
  110. c.JSON(nil, err)
  111. return
  112. }
  113. data.Dislikes = _dislike
  114. compMeta(data, build)
  115. if mid == 0 && data.Duration > 360 {
  116. data.Paster, _ = viewSvr.Paster(c, plat, resource.VdoAdsTypeNologin, aidStr, strconv.Itoa(int(data.TypeID)), buvid)
  117. }
  118. c.JSON(data, nil)
  119. viewSvr.RelateInfoc(mid, aid, int(plat), trackid, buildStr, buvid, disid, ip, _viewPath, data.ReturnCode, data.UserFeature, from, data.Relates, now, data.IsRec)
  120. }
  121. // viewPage view page handler.
  122. func viewPage(c *bm.Context) {
  123. var (
  124. mid, aid int64
  125. build int
  126. err error
  127. )
  128. params := c.Request.Form
  129. header := c.Request.Header
  130. // get params
  131. aidStr := params.Get("aid")
  132. mobiApp := params.Get("mobi_app")
  133. device := params.Get("device")
  134. ak := params.Get("access_key")
  135. buildStr := params.Get("build")
  136. from := params.Get("from")
  137. trackid := params.Get("trackid")
  138. spmid := params.Get("spmid")
  139. fromSpmid := params.Get("from_spmid")
  140. // check params
  141. if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil {
  142. c.JSON(nil, ecode.RequestErr)
  143. return
  144. }
  145. if aid < 1 {
  146. c.JSON(nil, ecode.RequestErr)
  147. return
  148. }
  149. if build, err = strconv.Atoi(buildStr); err != nil {
  150. c.JSON(nil, ecode.RequestErr)
  151. return
  152. }
  153. plat := model.Plat(mobiApp, device)
  154. buvid := header.Get("Buvid")
  155. disid := header.Get("Display-ID")
  156. cdnIP := header.Get("X-Cache-Server-Addr")
  157. if midInter, ok := c.Get("mid"); ok {
  158. mid = midInter.(int64)
  159. }
  160. autoplay, _ := strconv.Atoi(params.Get("autoplay"))
  161. ip := metadata.String(c, metadata.RemoteIP)
  162. now := time.Now()
  163. // view page
  164. viewSvr.ViewInfoc(mid, int(plat), trackid, aidStr, ip, _viewPagePath, buildStr, buvid, disid, from, now, err, autoplay, spmid, fromSpmid)
  165. data, err := viewSvr.ViewPage(c, mid, aid, 0, plat, build, ak, mobiApp, device, cdnIP, false, now)
  166. if err != nil {
  167. c.JSON(nil, err)
  168. return
  169. }
  170. compMeta(data, build)
  171. c.JSON(data, nil)
  172. }
  173. // videoShot video shot .
  174. func videoShot(c *bm.Context) {
  175. var aid, cid int64
  176. params := c.Request.Form
  177. if aid, _ = strconv.ParseInt(params.Get("aid"), 10, 64); aid < 1 {
  178. c.JSON(nil, ecode.RequestErr)
  179. return
  180. }
  181. if cid, _ = strconv.ParseInt(params.Get("cid"), 10, 64); cid < 1 {
  182. c.JSON(nil, ecode.RequestErr)
  183. return
  184. }
  185. c.JSON(viewSvr.Shot(c, aid, cid))
  186. }
  187. // addShare add a share.
  188. func addShare(c *bm.Context) {
  189. params := c.Request.Form
  190. mobiApp := params.Get("mobi_app")
  191. buvid := c.Request.Header.Get("Buvid")
  192. from := params.Get("from")
  193. build := params.Get("build")
  194. // check
  195. aidStr := params.Get("aid")
  196. aid, err := strconv.ParseInt(aidStr, 10, 64)
  197. if err != nil {
  198. c.JSON(nil, ecode.RequestErr)
  199. return
  200. }
  201. var mid int64
  202. if midInter, ok := c.Get("mid"); ok {
  203. mid = midInter.(int64)
  204. }
  205. share, isReport, upID, err := viewSvr.AddShare(c, aid, mid, metadata.String(c, metadata.RemoteIP))
  206. c.JSON(struct {
  207. Aid int64 `json:"aid"`
  208. Count int `json:"count"`
  209. }{aid, share}, err)
  210. if err != nil {
  211. return
  212. }
  213. // for ai big data
  214. sendUserAct(mid, mobiApp, buvid, from, build, aidStr, "av", "share")
  215. if collector != nil && isReport {
  216. collector.InfoAntiCheat2(c, strconv.FormatInt(upID, 10), strconv.FormatInt(aid, 10), strconv.FormatInt(mid, 10), strconv.FormatInt(aid, 10), "av", "share", "")
  217. }
  218. }
  219. func addCoin(c *bm.Context) {
  220. var (
  221. mid, aid, upID int64
  222. avType int64
  223. actLike = "cointolike"
  224. actCoin = "coin"
  225. )
  226. params := c.Request.Form
  227. // check
  228. mobiApp := params.Get("mobi_app")
  229. buvid := c.Request.Header.Get("Buvid")
  230. from := params.Get("from")
  231. build := params.Get("build")
  232. aidStr := params.Get("aid")
  233. upIDStr := params.Get("upid")
  234. ak := params.Get("access_key")
  235. multiStr := params.Get("multiply")
  236. selectLikeStr := params.Get("select_like")
  237. selectLike, _ := strconv.Atoi(selectLikeStr)
  238. aid, _ = strconv.ParseInt(aidStr, 10, 64)
  239. upID, _ = strconv.ParseInt(upIDStr, 10, 64)
  240. multiply, err := strconv.ParseInt(multiStr, 10, 64)
  241. if err != nil {
  242. c.JSON(nil, ecode.RequestErr)
  243. return
  244. }
  245. if avType, _ = strconv.ParseInt(params.Get("avtype"), 10, 64); avType == 0 {
  246. avType = 1
  247. }
  248. if midInter, ok := c.Get("mid"); ok {
  249. mid = midInter.(int64)
  250. }
  251. // for ai big data
  252. sendUserAct(mid, mobiApp, buvid, from, build, aidStr, "av", actCoin)
  253. if selectLike == 1 {
  254. sendUserAct(mid, mobiApp, buvid, from, build, aidStr, "av", actLike)
  255. }
  256. prompt, like, err := viewSvr.AddCoin(c, aid, mid, upID, avType, multiply, ak, selectLike)
  257. c.JSON(struct {
  258. Prompt bool `json:"prompt,omitempty"`
  259. Like bool `json:"like"`
  260. }{Prompt: prompt, Like: like}, err)
  261. if err != nil {
  262. return
  263. }
  264. if collector != nil {
  265. var itemType = "archive"
  266. if avType == 2 {
  267. itemType = "article"
  268. }
  269. collector.InfoAntiCheat2(c, strconv.FormatInt(upID, 10), strconv.FormatInt(aid, 10), strconv.FormatInt(mid, 10), strconv.FormatInt(aid, 10), itemType, actCoin, "")
  270. if like {
  271. collector.InfoAntiCheat2(c, strconv.FormatInt(upID, 10), strconv.FormatInt(aid, 10), strconv.FormatInt(mid, 10), strconv.FormatInt(aid, 10), "av", actLike, "")
  272. }
  273. }
  274. }
  275. func like(c *bm.Context) {
  276. var mid int64
  277. params := c.Request.Form
  278. // check
  279. mobiApp := params.Get("mobi_app")
  280. buvid := c.Request.Header.Get("Buvid")
  281. from := params.Get("from")
  282. build := params.Get("build")
  283. aidStr := params.Get("aid")
  284. aid, err := strconv.ParseInt(aidStr, 10, 64)
  285. if err != nil {
  286. c.JSON(nil, ecode.RequestErr)
  287. return
  288. }
  289. like, err := strconv.Atoi(params.Get("like"))
  290. if err != nil {
  291. c.JSON(nil, ecode.RequestErr)
  292. return
  293. }
  294. if like != 0 && like != 1 {
  295. c.JSON(nil, ecode.RequestErr)
  296. return
  297. }
  298. if midInter, ok := c.Get("mid"); ok {
  299. mid = midInter.(int64)
  300. }
  301. upperID, toast, err := viewSvr.Like(c, aid, mid, int8(like))
  302. c.JSON(struct {
  303. Toast string `json:"toast"`
  304. }{Toast: toast}, err)
  305. if err != nil {
  306. return
  307. }
  308. // for ai big data
  309. action := "like"
  310. if like == 1 {
  311. action = "like_cancel"
  312. }
  313. sendUserAct(mid, mobiApp, buvid, from, build, aidStr, "av", action)
  314. if collector != nil {
  315. collector.InfoAntiCheat2(c, strconv.FormatInt(upperID, 10), strconv.FormatInt(aid, 10), strconv.FormatInt(mid, 10), strconv.FormatInt(aid, 10), "av", action, "")
  316. }
  317. }
  318. func dislike(c *bm.Context) {
  319. var mid int64
  320. params := c.Request.Form
  321. mobiApp := params.Get("mobi_app")
  322. buvid := c.Request.Header.Get("Buvid")
  323. from := params.Get("from")
  324. build := params.Get("build")
  325. // check
  326. aidStr := params.Get("aid")
  327. aid, err := strconv.ParseInt(aidStr, 10, 64)
  328. if err != nil {
  329. c.JSON(nil, ecode.RequestErr)
  330. return
  331. }
  332. dislike, err := strconv.Atoi(params.Get("dislike"))
  333. if err != nil {
  334. c.JSON(nil, ecode.RequestErr)
  335. return
  336. }
  337. if dislike != 0 && dislike != 1 {
  338. c.JSON(nil, ecode.RequestErr)
  339. return
  340. }
  341. if midInter, ok := c.Get("mid"); ok {
  342. mid = midInter.(int64)
  343. }
  344. upperID, err := viewSvr.Dislike(c, aid, mid, int8(dislike))
  345. c.JSON(nil, err)
  346. if err != nil {
  347. return
  348. }
  349. // for ai big data
  350. action := "dislike"
  351. if dislike == 1 {
  352. action = "dislike_cancel"
  353. }
  354. sendUserAct(mid, mobiApp, buvid, from, build, aidStr, "av", action)
  355. if collector != nil {
  356. collector.InfoAntiCheat2(c, strconv.FormatInt(upperID, 10), strconv.FormatInt(aid, 10), strconv.FormatInt(mid, 10), strconv.FormatInt(aid, 10), "av", action, "")
  357. }
  358. }
  359. func addFav(c *bm.Context) {
  360. var (
  361. mid int64
  362. vmid int64
  363. fid int64
  364. aid int64
  365. err error
  366. )
  367. params := c.Request.Form
  368. if midI, ok := c.Get("mid"); ok {
  369. mid = midI.(int64)
  370. }
  371. // check
  372. if aid, err = strconv.ParseInt(params.Get("aid"), 10, 64); err != nil {
  373. c.JSON(nil, ecode.RequestErr)
  374. return
  375. }
  376. if vmid, err = strconv.ParseInt(params.Get("vmid"), 10, 64); err != nil {
  377. c.JSON(nil, ecode.RequestErr)
  378. return
  379. }
  380. if fid, err = strconv.ParseInt(params.Get("fid"), 10, 64); err != nil {
  381. c.JSON(nil, ecode.RequestErr)
  382. return
  383. }
  384. ak := params.Get("access_key")
  385. prompt, err := viewSvr.AddFav(c, mid, vmid, []int64{fid}, aid, ak)
  386. c.JSON(struct {
  387. Prompt bool `json:"prompt,omitempty"`
  388. }{Prompt: prompt}, err)
  389. }
  390. // adDislike ad dislike
  391. func adDislike(c *bm.Context) {
  392. var mid int64
  393. params := c.Request.Form
  394. header := c.Request.Header
  395. if midInter, ok := c.Get("mid"); ok {
  396. mid = midInter.(int64)
  397. }
  398. gt := params.Get("goto")
  399. id, _ := strconv.ParseInt(params.Get("id"), 10, 64)
  400. reasonID, _ := strconv.ParseInt(params.Get("reason_id"), 10, 64)
  401. cmreasonID, _ := strconv.ParseInt(params.Get("cm_reason_id"), 10, 64)
  402. rid, _ := strconv.ParseInt(params.Get("rid"), 10, 64)
  403. tagID, _ := strconv.ParseInt(params.Get("tag_id"), 10, 64)
  404. adcb := params.Get("ad_cb")
  405. buvid := header.Get("Buvid")
  406. if buvid == "" && mid == 0 {
  407. c.JSON(nil, ecode.RequestErr)
  408. return
  409. }
  410. // for ad data
  411. err := dislikePub.Send(context.TODO(), strconv.FormatInt(mid, 10), &cmDislike{
  412. ID: id,
  413. Buvid: buvid,
  414. Goto: gt,
  415. Mid: mid,
  416. ReasonID: reasonID,
  417. CMReasonID: cmreasonID,
  418. UpperID: 0,
  419. Rid: rid,
  420. TagID: tagID,
  421. ADCB: adcb,
  422. })
  423. c.JSON(nil, err)
  424. }
  425. func compMeta(v *view.View, build int) {
  426. if build == 5800 || build == 508000 {
  427. for _, vp := range v.Pages {
  428. metas := make([]*view.Meta, 0, 4)
  429. for i, r := range _rate {
  430. meta := &view.Meta{
  431. Quality: i + 1,
  432. Size: int64(float64(r*v.Duration) * 1.1 / 8.0),
  433. Format: _formats[i+1],
  434. }
  435. metas = append(metas, meta)
  436. }
  437. vp.Metas = metas
  438. }
  439. }
  440. }
  441. // vipPlayURL get big-member token.
  442. func vipPlayURL(c *bm.Context) {
  443. var mid int64
  444. if midInter, ok := c.Get("mid"); ok {
  445. mid = midInter.(int64)
  446. }
  447. params := c.Request.Form
  448. aid, err := strconv.ParseInt(params.Get("aid"), 10, 64)
  449. if err != nil {
  450. c.JSON(nil, ecode.RequestErr)
  451. return
  452. }
  453. cid, _ := strconv.ParseInt(params.Get("cid"), 10, 64)
  454. if aid == 0 || cid == 0 {
  455. c.JSON(nil, ecode.RequestErr)
  456. return
  457. }
  458. c.JSON(viewSvr.VipPlayURL(c, aid, cid, mid))
  459. }
  460. // follow check if follow.
  461. func follow(c *bm.Context) {
  462. var mid int64
  463. if midInter, ok := c.Get("mid"); ok {
  464. mid = midInter.(int64)
  465. }
  466. params := c.Request.Form
  467. vmid, err := strconv.ParseInt(params.Get("vmid"), 10, 64)
  468. if err != nil || vmid <= 0 {
  469. c.JSON(nil, ecode.RequestErr)
  470. return
  471. }
  472. c.JSON(viewSvr.Follow(c, vmid, mid))
  473. }
  474. func upperRecmd(c *bm.Context) {
  475. var (
  476. mid int64
  477. vmid int64
  478. header = c.Request.Header
  479. params = c.Request.Form
  480. )
  481. if midInter, ok := c.Get("mid"); ok {
  482. mid = midInter.(int64)
  483. }
  484. mobiApp := params.Get("mobi_app")
  485. device := params.Get("device")
  486. platform := params.Get("platform")
  487. buildStr := params.Get("build")
  488. build, err := strconv.Atoi(buildStr)
  489. if err != nil {
  490. c.JSON(nil, ecode.RequestErr)
  491. return
  492. }
  493. if vmid, err = strconv.ParseInt(params.Get("vmid"), 10, 64); err != nil {
  494. c.JSON(nil, ecode.RequestErr)
  495. return
  496. }
  497. plat := model.Plat(mobiApp, device)
  498. buvid := header.Get(_headerBuvid)
  499. data, err := viewSvr.UpperRecmd(c, plat, platform, mobiApp, device, buvid, build, mid, vmid)
  500. c.JSON(data, err)
  501. }
  502. func likeTriple(c *bm.Context) {
  503. var (
  504. mid int64
  505. actTriple = "triplelike"
  506. )
  507. params := &view.TripleParam{}
  508. if err := c.Bind(params); err != nil {
  509. return
  510. }
  511. buvid := c.Request.Header.Get("Buvid")
  512. if midInter, ok := c.Get("mid"); ok {
  513. mid = midInter.(int64)
  514. }
  515. // for ai big data
  516. sendUserAct(mid, params.MobiApp, buvid, params.From, params.Build, strconv.FormatInt(params.AID, 10), "av", actTriple)
  517. triple, err := viewSvr.LikeTriple(c, params.AID, mid, params.Ak)
  518. c.JSON(triple, err)
  519. if err != nil {
  520. return
  521. }
  522. if collector != nil && triple.Anticheat {
  523. collector.InfoAntiCheat2(c, strconv.FormatInt(triple.UpID, 10), strconv.FormatInt(params.AID, 10), strconv.FormatInt(mid, 10), strconv.FormatInt(params.AID, 10), "av", actTriple, "")
  524. }
  525. }
  526. func sendUserAct(mid int64, mobiApp, buvid, from, build, itemID, itemType, action string) {
  527. userActPub.Send(context.TODO(), strconv.FormatInt(mid, 10), &userAct{
  528. Client: mobiApp,
  529. Buvid: buvid,
  530. Mid: mid,
  531. Time: time.Now().Unix(),
  532. From: from,
  533. Build: build,
  534. ItemID: itemID,
  535. ItemType: itemType,
  536. Action: action,
  537. })
  538. }