history.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. package http
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "go-common/app/interface/main/history/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. )
  13. // history return the user history for mobile app.
  14. func history(c *bm.Context) {
  15. var (
  16. err error
  17. mid int64
  18. v = new(Histroy)
  19. )
  20. if err = c.Bind(v); err != nil {
  21. return
  22. }
  23. if tp, ok := business(c); !ok {
  24. return
  25. } else if tp > 0 {
  26. v.TP = tp
  27. }
  28. if midInter, ok := c.Get("mid"); ok {
  29. mid = midInter.(int64)
  30. }
  31. if mid <= 0 {
  32. c.JSON(nil, ecode.RequestErr)
  33. return
  34. }
  35. if v.Pn < 1 {
  36. v.Pn = 1
  37. }
  38. if v.Ps > cnf.History.Max || v.Ps <= 0 {
  39. v.Ps = cnf.History.Max
  40. }
  41. list, err := hisSvc.Videos(c, mid, v.Pn, v.Ps, v.TP)
  42. if err != nil {
  43. c.JSON(nil, err)
  44. return
  45. }
  46. c.JSON(list, nil)
  47. }
  48. // aids return the user histories.
  49. func aids(c *bm.Context) {
  50. var (
  51. mid int64
  52. )
  53. if midInter, ok := c.Get("mid"); ok {
  54. mid = midInter.(int64)
  55. }
  56. if mid <= 0 {
  57. c.JSON(nil, ecode.RequestErr)
  58. return
  59. }
  60. list, err := hisSvc.AVHistories(c, mid)
  61. if err != nil {
  62. c.JSON(nil, err)
  63. return
  64. }
  65. c.JSON(list, nil)
  66. }
  67. func managerHistory(c *bm.Context) {
  68. var (
  69. err error
  70. v = new(struct {
  71. Mid int64 `form:"mid" validate:"required,gt=0"`
  72. OnlyAV bool `form:"only_av"`
  73. })
  74. )
  75. if err = c.Bind(v); err != nil {
  76. return
  77. }
  78. history, err := hisSvc.ManagerHistory(c, v.OnlyAV, v.Mid)
  79. if err != nil {
  80. c.JSON(nil, err)
  81. return
  82. }
  83. c.JSON(history, nil)
  84. }
  85. // clearHistory clear the user histories.
  86. func clearHistory(c *bm.Context) {
  87. var (
  88. err error
  89. mid int64
  90. v = new(struct {
  91. TP int8 `form:"type"`
  92. })
  93. )
  94. if err = c.Bind(v); err != nil {
  95. return
  96. }
  97. if midInter, ok := c.Get("mid"); ok {
  98. mid = midInter.(int64)
  99. }
  100. if tp, ok := business(c); !ok {
  101. return
  102. } else if tp > 0 {
  103. v.TP = tp
  104. }
  105. if mid <= 0 {
  106. c.JSON(nil, ecode.RequestErr)
  107. return
  108. }
  109. var tps []int8
  110. if v.TP > 0 {
  111. tps = append(tps, v.TP)
  112. }
  113. c.JSON(nil, hisSvc.ClearHistory(c, mid, tps))
  114. }
  115. // delHistory delete the user history by aid.
  116. func delHistory(c *bm.Context) {
  117. var (
  118. err error
  119. mid int64
  120. v = new(struct {
  121. TP int8 `form:"type"`
  122. Aids []int64 `form:"aid,split"`
  123. })
  124. )
  125. if err = c.Bind(v); err != nil {
  126. return
  127. }
  128. if tp, ok := business(c); !ok {
  129. return
  130. } else if tp > 0 {
  131. v.TP = tp
  132. }
  133. if midInter, ok := c.Get("mid"); ok {
  134. mid = midInter.(int64)
  135. }
  136. if mid <= 0 {
  137. c.JSON(nil, ecode.RequestErr)
  138. return
  139. }
  140. c.JSON(nil, hisSvc.DelHistory(c, mid, v.Aids, v.TP))
  141. }
  142. func delete(c *bm.Context) {
  143. var (
  144. err error
  145. mid int64
  146. v = new(struct {
  147. Hid []string `form:"hid,split"`
  148. Bid []string `form:"bid,split"`
  149. })
  150. )
  151. if err = c.Bind(v); err != nil {
  152. return
  153. }
  154. if midInter, ok := c.Get("mid"); ok {
  155. mid = midInter.(int64)
  156. }
  157. if mid <= 0 {
  158. c.JSON(nil, ecode.RequestErr)
  159. return
  160. }
  161. var his []*model.History
  162. if len(v.Bid) == 0 {
  163. for _, hid := range v.Hid {
  164. hs := strings.Split(hid, "_")
  165. if len(hs) == 0 {
  166. continue
  167. }
  168. aid, _ := strconv.ParseInt(hs[0], 10, 0)
  169. if aid == 0 {
  170. continue
  171. }
  172. var tp int64
  173. if len(hs) == 2 {
  174. tp, _ = strconv.ParseInt(hs[1], 10, 0)
  175. }
  176. his = append(his, &model.History{
  177. Aid: aid,
  178. TP: int8(tp),
  179. })
  180. }
  181. }
  182. for _, bid := range v.Bid {
  183. bs := strings.Split(bid, "_")
  184. if len(bs) != 2 {
  185. continue
  186. }
  187. aid, _ := strconv.ParseInt(bs[1], 10, 0)
  188. if aid == 0 {
  189. continue
  190. }
  191. tp, err := model.CheckBusiness(bs[0])
  192. if err != nil {
  193. continue
  194. }
  195. his = append(his, &model.History{
  196. Aid: aid,
  197. TP: int8(tp),
  198. })
  199. }
  200. if len(his) == 0 {
  201. c.JSON(nil, ecode.RequestErr)
  202. return
  203. }
  204. c.JSON(nil, hisSvc.Delete(c, mid, his))
  205. }
  206. // addHistory add history into user redis set.
  207. func addHistory(c *bm.Context) {
  208. var (
  209. err error
  210. mid int64
  211. h *model.History
  212. now = time.Now().Unix()
  213. v = new(AddHistory)
  214. )
  215. // sid cid tp, dt
  216. // dt:devece type , sid :season_id,type:video type
  217. // aid :aid
  218. // cid:cid,epid
  219. if err = c.Bind(v); err != nil {
  220. return
  221. }
  222. if midInter, ok := c.Get("mid"); ok {
  223. mid = midInter.(int64)
  224. }
  225. if mid <= 0 {
  226. c.JSON(nil, ecode.RequestErr)
  227. return
  228. }
  229. if v.TP == model.TypeBangumi || v.TP == model.TypeMovie {
  230. if v.Sid <= 0 || v.Epid <= 0 {
  231. if v.Aid <= 0 {
  232. c.JSON(nil, ecode.RequestErr)
  233. return
  234. }
  235. }
  236. }
  237. if v.Aid <= 0 || (v.TP < model.TypeArticle && v.Cid <= 0) {
  238. c.JSON(nil, ecode.RequestErr)
  239. return
  240. }
  241. if v.DT <= 0 {
  242. if v.Platform == model.PlatformIOS {
  243. v.DT = model.DeviceIphone
  244. if v.Device == model.DevicePad {
  245. v.DT = model.DeviceIpad
  246. }
  247. } else if v.Platform == model.PlatformAndroid {
  248. v.DT = model.DeviceAndroid
  249. }
  250. }
  251. h = &model.History{
  252. Aid: v.Aid,
  253. Unix: now,
  254. Sid: v.Sid,
  255. Epid: v.Epid,
  256. Cid: v.Cid,
  257. TP: v.TP,
  258. STP: v.SubTP,
  259. DT: v.DT,
  260. }
  261. h.ConvertType()
  262. c.JSON(nil, hisSvc.AddHistory(c, mid, 0, h))
  263. }
  264. // report report view progress.
  265. func report(c *bm.Context) {
  266. var (
  267. err error
  268. mid int64
  269. h *model.History
  270. now = time.Now().Unix()
  271. v = new(HistoryReport)
  272. )
  273. if err = c.Bind(v); err != nil {
  274. return
  275. }
  276. if midInter, ok := c.Get("mid"); ok {
  277. mid = midInter.(int64)
  278. }
  279. if tp, ok := business(c); !ok {
  280. return
  281. } else if tp > 0 {
  282. v.Type = tp
  283. }
  284. if v.Progress < 0 {
  285. v.Progress = model.ProComplete
  286. }
  287. if v.Aid <= 0 || (v.Type < model.TypeArticle && v.Cid <= 0) {
  288. c.JSON(nil, ecode.RequestErr)
  289. return
  290. }
  291. if v.SubType <= 0 {
  292. v.SubType = v.SubTP
  293. }
  294. if v.Type == model.TypeBangumi || v.Type == model.TypeMovie {
  295. if v.Sid <= 0 || v.Epid <= 0 {
  296. v.Type = model.TypeUGC
  297. } else {
  298. v.Type = model.TypePGC
  299. }
  300. }
  301. if v.DT <= 0 {
  302. if v.Platform == model.PlatformIOS {
  303. v.DT = model.DeviceIphone
  304. if v.Device == model.DevicePad {
  305. v.DT = model.DeviceIpad
  306. }
  307. } else if v.Platform == model.PlatformAndroid {
  308. v.DT = model.DeviceAndroid
  309. if v.MobileApp == model.MobileAppAndroidTV {
  310. v.DT = model.DeviceAndroidTV
  311. }
  312. }
  313. }
  314. h = &model.History{
  315. Aid: v.Aid,
  316. Unix: now,
  317. Sid: v.Sid,
  318. Epid: v.Epid,
  319. Cid: v.Cid,
  320. Pro: v.Progress,
  321. TP: v.Type,
  322. STP: v.SubType,
  323. DT: v.DT,
  324. }
  325. c.JSON(nil, hisSvc.AddHistory(c, mid, v.Realtime, h))
  326. }
  327. // report report view progress.
  328. func innerReport(c *bm.Context) {
  329. var (
  330. err error
  331. h *model.History
  332. v = new(HistoryReport)
  333. )
  334. if err = c.Bind(v); err != nil {
  335. return
  336. }
  337. if tp, ok := business(c); !ok {
  338. return
  339. } else if tp > 0 {
  340. v.Type = tp
  341. }
  342. if v.Mid == 0 && v.Aid == 0 && v.Type == 0 {
  343. c.JSON(nil, ecode.RequestErr)
  344. return
  345. }
  346. if v.Type == model.TypeComic && v.Aid == 0 {
  347. v.Aid = v.Sid
  348. }
  349. if v.PlayTime == 0 {
  350. v.PlayTime = time.Now().Unix()
  351. }
  352. if v.Progress < 0 {
  353. v.Progress = model.ProComplete
  354. }
  355. h = &model.History{
  356. Aid: v.Aid,
  357. Unix: v.PlayTime,
  358. Sid: v.Sid,
  359. Epid: v.Epid,
  360. Cid: v.Cid,
  361. Pro: v.Progress,
  362. TP: v.Type,
  363. STP: v.SubType,
  364. DT: v.DT,
  365. }
  366. c.JSON(nil, hisSvc.AddHistory(c, v.Mid, v.Realtime, h))
  367. }
  368. // reports
  369. func reports(c *bm.Context) {
  370. var (
  371. err error
  372. mid int64
  373. hs = make([]*model.History, 0)
  374. v = new(struct {
  375. Type int8 `form:"type"`
  376. Data string `form:"data"`
  377. })
  378. )
  379. if err = c.Bind(v); err != nil {
  380. return
  381. }
  382. if tp, ok := business(c); !ok {
  383. return
  384. } else if tp > 0 {
  385. v.Type = tp
  386. }
  387. if midInter, ok := c.Get("mid"); ok {
  388. mid = midInter.(int64)
  389. }
  390. if mid <= 0 {
  391. c.JSON(nil, ecode.RequestErr)
  392. return
  393. }
  394. if err = json.Unmarshal([]byte(v.Data), &hs); err != nil {
  395. log.Error("json.Unmarshal(%s),err:%v.", v.Data, err)
  396. c.JSON(nil, ecode.RequestErr)
  397. return
  398. }
  399. if err = hisSvc.AddHistories(c, mid, v.Type, metadata.String(c, metadata.RemoteIP), hs); err != nil {
  400. c.JSON(nil, ecode.ServerErr)
  401. return
  402. }
  403. c.JSON(nil, nil)
  404. }
  405. // shadow return the user shadow status.
  406. func shadow(c *bm.Context) {
  407. var (
  408. err error
  409. mid int64
  410. )
  411. if midInter, ok := c.Get("mid"); ok {
  412. mid = midInter.(int64)
  413. }
  414. if mid <= 0 {
  415. c.JSON(nil, ecode.RequestErr)
  416. return
  417. }
  418. status, err := hisSvc.Shadow(c, mid)
  419. if err != nil {
  420. c.JSON(nil, err)
  421. return
  422. }
  423. c.JSON(status == model.ShadowOn, nil)
  424. }
  425. // setShadow the user shadow status.
  426. func setShadow(c *bm.Context) {
  427. var (
  428. err error
  429. mid int64
  430. shadow = model.ShadowOff
  431. v = new(struct {
  432. Switch bool `form:"switch"`
  433. })
  434. )
  435. if err = c.Bind(v); err != nil {
  436. return
  437. }
  438. if midInter, ok := c.Get("mid"); ok {
  439. mid = midInter.(int64)
  440. }
  441. if mid <= 0 {
  442. c.JSON(nil, ecode.RequestErr)
  443. return
  444. }
  445. if v.Switch {
  446. shadow = model.ShadowOn
  447. }
  448. c.JSON(nil, hisSvc.SetShadow(c, mid, shadow))
  449. }
  450. // flush flush users hisotry.
  451. func flush(c *bm.Context) {
  452. var (
  453. err error
  454. v = new(struct {
  455. Mids []int64 `form:"mids,split" validate:"required,min=1,dive,gt=0"`
  456. STime int64 `form:"time"`
  457. })
  458. )
  459. if err = c.Bind(v); err != nil {
  460. return
  461. }
  462. c.JSON(nil, hisSvc.FlushHistory(c, v.Mids, v.STime))
  463. }
  464. // position report report view progress.
  465. func position(c *bm.Context) {
  466. var (
  467. err error
  468. v = new(struct {
  469. Mid int64 `form:"mid"`
  470. Aid int64 `form:"aid"`
  471. TP int8 `form:"type"`
  472. })
  473. )
  474. if err = c.Bind(v); err != nil {
  475. return
  476. }
  477. if tp, ok := business(c); !ok {
  478. return
  479. } else if tp > 0 {
  480. v.TP = tp
  481. }
  482. if v.Mid == 0 && v.Aid == 0 {
  483. c.JSON(nil, ecode.RequestErr)
  484. return
  485. }
  486. c.JSON(hisSvc.Position(c, v.Mid, v.Aid, v.TP))
  487. }
  488. // position report report view progress.
  489. func resource(c *bm.Context) {
  490. var (
  491. err error
  492. v = new(struct {
  493. Mid int64 `form:"mid"`
  494. TP int8 `form:"type"`
  495. Pn int `form:"pn"`
  496. Ps int `form:"ps"`
  497. })
  498. )
  499. if err = c.Bind(v); err != nil {
  500. c.JSON(nil, ecode.RequestErr)
  501. return
  502. }
  503. if tp, ok := business(c); !ok {
  504. return
  505. } else if tp > 0 {
  506. v.TP = tp
  507. }
  508. if v.Mid == 0 || v.TP == 0 {
  509. c.JSON(nil, ecode.RequestErr)
  510. return
  511. }
  512. c.JSON(hisSvc.Histories(c, v.Mid, v.TP, v.Pn, v.Ps))
  513. }
  514. // position report report view progress.
  515. func resources(c *bm.Context) {
  516. var (
  517. err error
  518. v = new(struct {
  519. Mid int64 `form:"mid"`
  520. TP int8 `form:"type"`
  521. Aids []int64 `form:"aids,split"`
  522. })
  523. )
  524. if err = c.Bind(v); err != nil {
  525. c.JSON(nil, ecode.RequestErr)
  526. return
  527. }
  528. if tp, ok := business(c); !ok {
  529. return
  530. } else if tp > 0 {
  531. v.TP = tp
  532. }
  533. if v.Mid == 0 || v.TP == 0 {
  534. c.JSON(nil, ecode.RequestErr)
  535. return
  536. }
  537. if len(v.Aids) == 0 {
  538. c.JSON(nil, ecode.RequestErr)
  539. return
  540. }
  541. c.JSON(hisSvc.HistoryType(c, v.Mid, v.TP, v.Aids, metadata.String(c, metadata.RemoteIP)))
  542. }