web_danmu.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. package http
  2. import (
  3. "strconv"
  4. "go-common/library/ecode"
  5. "go-common/library/log"
  6. bm "go-common/library/net/http/blademaster"
  7. "go-common/library/net/metadata"
  8. "go-common/library/xstr"
  9. )
  10. func webListDmPurchases(c *bm.Context) {
  11. midI, ok := c.Get("mid")
  12. if !ok {
  13. c.JSON(nil, ecode.NoLogin)
  14. return
  15. }
  16. mid, _ := midI.(int64)
  17. ip := metadata.String(c, metadata.RemoteIP)
  18. danmus, err := danmuSvc.AdvDmPurchaseList(c, mid, ip)
  19. if err != nil {
  20. c.JSON(nil, err)
  21. return
  22. }
  23. c.JSON(danmus, nil)
  24. }
  25. func webPassDmPurchase(c *bm.Context) {
  26. params := c.Request.Form
  27. midI, ok := c.Get("mid")
  28. if !ok {
  29. c.JSON(nil, ecode.NoLogin)
  30. return
  31. }
  32. mid, _ := midI.(int64)
  33. danmuIDStr := params.Get("id")
  34. danmuID, err := strconv.ParseInt(danmuIDStr, 10, 64)
  35. if err != nil || danmuID == 0 {
  36. c.JSON(nil, ecode.RequestErr)
  37. return
  38. }
  39. ip := metadata.String(c, metadata.RemoteIP)
  40. err = danmuSvc.PassDmPurchase(c, mid, danmuID, ip)
  41. if err != nil {
  42. c.JSON(nil, err)
  43. return
  44. }
  45. c.JSON(nil, nil)
  46. }
  47. func webDenyDmPurchase(c *bm.Context) {
  48. params := c.Request.Form
  49. midI, ok := c.Get("mid")
  50. if !ok {
  51. c.JSON(nil, ecode.NoLogin)
  52. return
  53. }
  54. mid, _ := midI.(int64)
  55. danmuIDStr := params.Get("id")
  56. danmuID, err := strconv.ParseInt(danmuIDStr, 10, 64)
  57. if err != nil || danmuID == 0 {
  58. c.JSON(nil, ecode.RequestErr)
  59. return
  60. }
  61. ip := metadata.String(c, metadata.RemoteIP)
  62. err = danmuSvc.DenyDmPurchase(c, mid, danmuID, ip)
  63. if err != nil {
  64. c.JSON(nil, err)
  65. return
  66. }
  67. c.JSON(nil, nil)
  68. }
  69. func webCancelDmPurchase(c *bm.Context) {
  70. params := c.Request.Form
  71. midI, ok := c.Get("mid")
  72. if !ok {
  73. c.JSON(nil, ecode.NoLogin)
  74. return
  75. }
  76. mid, _ := midI.(int64)
  77. danmuIDStr := params.Get("id")
  78. danmuID, err := strconv.ParseInt(danmuIDStr, 10, 64)
  79. if err != nil || danmuID == 0 {
  80. c.JSON(nil, ecode.RequestErr)
  81. return
  82. }
  83. ip := metadata.String(c, metadata.RemoteIP)
  84. err = danmuSvc.CancelDmPurchase(c, mid, danmuID, ip)
  85. if err != nil {
  86. c.JSON(nil, err)
  87. return
  88. }
  89. c.JSON(nil, nil)
  90. }
  91. func webDmList(c *bm.Context) {
  92. params := c.Request.Form
  93. pnStr := params.Get("pn")
  94. psStr := params.Get("ps")
  95. idStr := params.Get("id")
  96. pool := params.Get("pool")
  97. aidStr := params.Get("aid")
  98. midStr := params.Get("mid")
  99. midI, ok := c.Get("mid")
  100. if !ok {
  101. c.JSON(nil, ecode.NoLogin)
  102. return
  103. }
  104. mid, _ := midI.(int64)
  105. id, err := strconv.ParseInt(idStr, 10, 64)
  106. if err != nil || id <= 0 {
  107. c.JSON(nil, ecode.RequestErr)
  108. return
  109. }
  110. aid, err := strconv.ParseInt(aidStr, 10, 64)
  111. if err != nil || aid <= 0 {
  112. c.JSON(nil, ecode.RequestErr)
  113. return
  114. }
  115. pn, err := strconv.Atoi(pnStr)
  116. if err != nil || pn <= 0 {
  117. pn = 1
  118. }
  119. ps, err := strconv.Atoi(psStr)
  120. if err != nil || ps <= 0 {
  121. ps = 20
  122. }
  123. ip := metadata.String(c, metadata.RemoteIP)
  124. order := "progress"
  125. orderStr := params.Get("order")
  126. if orderStr != order {
  127. order = "ctime"
  128. }
  129. list, err := danmuSvc.List(c, mid, aid, id, pn, ps, order, pool, midStr, ip)
  130. if err != nil {
  131. c.JSON(nil, err)
  132. return
  133. }
  134. c.JSON(list, nil)
  135. }
  136. func webDmEdit(c *bm.Context) {
  137. params := c.Request.Form
  138. idStr := params.Get("cid")
  139. dmidsStr := params.Get("dmids")
  140. stateStr := params.Get("state")
  141. midI, ok := c.Get("mid")
  142. if !ok {
  143. c.JSON(nil, ecode.NoLogin)
  144. return
  145. }
  146. mid, _ := midI.(int64)
  147. id, err := strconv.ParseInt(idStr, 10, 64)
  148. if err != nil || id <= 0 {
  149. err = ecode.RequestErr
  150. return
  151. }
  152. state, err := strconv.Atoi(stateStr)
  153. if err != nil {
  154. err = ecode.RequestErr
  155. return
  156. }
  157. var dmids []int64
  158. if dmids, err = xstr.SplitInts(dmidsStr); err != nil {
  159. c.JSON(nil, ecode.RequestErr)
  160. log.Error("xstr.SplitInts(dmidsStr %v) err(%v)", dmidsStr, err)
  161. return
  162. }
  163. for _, dmid := range dmids {
  164. if dmid <= 0 {
  165. c.JSON(nil, ecode.RequestErr)
  166. log.Error("dmids range err (dmid %d) err(%v)", dmid, err)
  167. return
  168. }
  169. }
  170. ip := metadata.String(c, metadata.RemoteIP)
  171. err = danmuSvc.Edit(c, mid, id, int8(state), dmids, ip)
  172. if err != nil {
  173. c.JSON(nil, err)
  174. return
  175. }
  176. c.JSON(nil, nil)
  177. }
  178. func webDmDistri(c *bm.Context) {
  179. midI, ok := c.Get("mid")
  180. if !ok {
  181. c.JSON(nil, ecode.NoLogin)
  182. return
  183. }
  184. mid, _ := midI.(int64)
  185. params := c.Request.Form
  186. cidStr := params.Get("cid")
  187. cid, err := strconv.ParseInt(cidStr, 10, 64)
  188. if err != nil || cid <= 0 {
  189. c.JSON(nil, ecode.RequestErr)
  190. return
  191. }
  192. aidStr := params.Get("aid")
  193. aid, err := strconv.ParseInt(aidStr, 10, 64)
  194. if err != nil || aid <= 0 {
  195. c.JSON(nil, ecode.RequestErr)
  196. return
  197. }
  198. ip := metadata.String(c, metadata.RemoteIP)
  199. v, err := arcSvc.Video(c, mid, aid, cid, ip)
  200. if err != nil {
  201. c.JSON(nil, err)
  202. return
  203. }
  204. list := make(map[int64]int64)
  205. if v.From == "vupload" {
  206. list, err = danmuSvc.Distri(c, mid, cid, ip)
  207. if err != nil {
  208. c.JSON(nil, err)
  209. return
  210. }
  211. }
  212. c.JSON(map[string]interface{}{
  213. "duration": v.Duration * 1000,
  214. "type": v.From,
  215. "list": list,
  216. }, nil)
  217. }
  218. func webDmTransfer(c *bm.Context) {
  219. params := c.Request.Form
  220. fromCIDStr := params.Get("from_cid")
  221. toCIDStr := params.Get("to_cid")
  222. offsetStr := params.Get("offset")
  223. offset, err := strconv.ParseFloat(offsetStr, 64)
  224. if err != nil {
  225. offset = float64(0)
  226. }
  227. midI, ok := c.Get("mid")
  228. if !ok {
  229. c.JSON(nil, ecode.NoLogin)
  230. return
  231. }
  232. mid, _ := midI.(int64)
  233. fromCID, err := strconv.ParseInt(fromCIDStr, 10, 64)
  234. if err != nil || fromCID <= 0 {
  235. err = ecode.RequestErr
  236. return
  237. }
  238. toCID, err := strconv.ParseInt(toCIDStr, 10, 64)
  239. if err != nil || toCID <= 0 {
  240. err = ecode.RequestErr
  241. return
  242. }
  243. ip := metadata.String(c, metadata.RemoteIP)
  244. ck := c.Request.Header.Get("cookie")
  245. ak := params.Get("access_key")
  246. err = danmuSvc.Transfer(c, mid, fromCID, toCID, offset, ak, ck, ip)
  247. if err != nil {
  248. c.JSON(nil, err)
  249. return
  250. }
  251. c.JSON(nil, nil)
  252. }
  253. func webDmUpPool(c *bm.Context) {
  254. params := c.Request.Form
  255. idStr := params.Get("cid")
  256. dmidsStr := params.Get("dmids")
  257. poolStr := params.Get("pool")
  258. midI, ok := c.Get("mid")
  259. if !ok {
  260. c.JSON(nil, ecode.NoLogin)
  261. return
  262. }
  263. mid, _ := midI.(int64)
  264. id, err := strconv.ParseInt(idStr, 10, 64)
  265. if err != nil || id <= 0 {
  266. err = ecode.RequestErr
  267. return
  268. }
  269. pool, err := strconv.Atoi(poolStr)
  270. if err != nil {
  271. err = ecode.RequestErr
  272. return
  273. }
  274. var dmids []int64
  275. if dmids, err = xstr.SplitInts(dmidsStr); err != nil {
  276. c.JSON(nil, ecode.RequestErr)
  277. log.Error("xstr.SplitInts(dmidsStr %v) err(%v)", dmidsStr, err)
  278. return
  279. }
  280. for _, dmid := range dmids {
  281. if dmid <= 0 {
  282. c.JSON(nil, ecode.RequestErr)
  283. log.Error("dmids range err (dmid %d) err(%v)", dmid, err)
  284. return
  285. }
  286. }
  287. ip := metadata.String(c, metadata.RemoteIP)
  288. err = danmuSvc.UpPool(c, id, dmids, int8(pool), mid, ip)
  289. if err != nil {
  290. c.JSON(nil, err)
  291. return
  292. }
  293. c.JSON(nil, nil)
  294. }
  295. func webDmReportCheck(c *bm.Context) {
  296. var err error
  297. params := c.Request.Form
  298. cidStr := params.Get("cid")
  299. cid, err := strconv.ParseInt(cidStr, 10, 64)
  300. if err != nil {
  301. err = ecode.RequestErr
  302. return
  303. }
  304. dmidStr := params.Get("dmid")
  305. dmid, err := strconv.ParseInt(dmidStr, 10, 64)
  306. if err != nil {
  307. err = ecode.RequestErr
  308. return
  309. }
  310. opStr := params.Get("op")
  311. op, err := strconv.ParseInt(opStr, 10, 64)
  312. if err != nil {
  313. err = ecode.RequestErr
  314. return
  315. }
  316. midI, ok := c.Get("mid")
  317. if !ok {
  318. c.JSON(nil, ecode.NoLogin)
  319. return
  320. }
  321. mid, _ := midI.(int64)
  322. ip := metadata.String(c, metadata.RemoteIP)
  323. err = danmuSvc.DmReportCheck(c, mid, cid, dmid, op, ip)
  324. if err != nil {
  325. c.JSON(nil, err)
  326. return
  327. }
  328. c.JSON(nil, nil)
  329. }
  330. func webDmProtectArchive(c *bm.Context) {
  331. midI, ok := c.Get("mid")
  332. if !ok {
  333. c.JSON(nil, ecode.NoLogin)
  334. return
  335. }
  336. mid, _ := midI.(int64)
  337. ip := metadata.String(c, metadata.RemoteIP)
  338. data, err := danmuSvc.DmProtectArchive(c, mid, ip)
  339. if err != nil {
  340. c.JSON(nil, err)
  341. return
  342. }
  343. c.JSON(data, nil)
  344. }
  345. func webDmProtectList(c *bm.Context) {
  346. params := c.Request.Form
  347. midI, ok := c.Get("mid")
  348. if !ok {
  349. c.JSON(nil, ecode.NoLogin)
  350. return
  351. }
  352. sortStr := params.Get("sort")
  353. if sortStr != "playtime" {
  354. sortStr = "ctime"
  355. }
  356. pnStr := params.Get("page")
  357. pn, err := strconv.ParseInt(pnStr, 10, 64)
  358. if err != nil || pn <= 0 {
  359. pn = 1
  360. }
  361. aidStr := params.Get("aid")
  362. mid, _ := midI.(int64)
  363. ip := metadata.String(c, metadata.RemoteIP)
  364. vlist, err := danmuSvc.DmProtectList(c, mid, pn, aidStr, sortStr, ip)
  365. if err != nil {
  366. c.JSON(nil, err)
  367. return
  368. }
  369. c.JSON(vlist, nil)
  370. }
  371. func webDmProtectOper(c *bm.Context) {
  372. params := c.Request.Form
  373. statusStr := params.Get("status")
  374. status, err := strconv.ParseInt(statusStr, 10, 64)
  375. if err != nil || status != 1 {
  376. status = 0
  377. }
  378. idsStr := params.Get("ids")
  379. _, err = xstr.SplitInts(idsStr)
  380. if err != nil {
  381. c.JSON(nil, ecode.RequestErr)
  382. return
  383. }
  384. midI, ok := c.Get("mid")
  385. if !ok {
  386. c.JSON(nil, ecode.NoLogin)
  387. return
  388. }
  389. mid, _ := midI.(int64)
  390. ip := metadata.String(c, metadata.RemoteIP)
  391. err = danmuSvc.DmProtectOper(c, mid, status, idsStr, ip)
  392. if err != nil {
  393. c.JSON(nil, err)
  394. return
  395. }
  396. c.JSON(nil, nil)
  397. }
  398. func webDmReport(c *bm.Context) {
  399. params := c.Request.Form
  400. midI, ok := c.Get("mid")
  401. if !ok {
  402. c.JSON(nil, ecode.NoLogin)
  403. return
  404. }
  405. pnStr := params.Get("pn")
  406. psStr := params.Get("ps")
  407. pn, err := strconv.ParseInt(pnStr, 10, 64)
  408. if err != nil || pn <= 0 {
  409. pn = 1
  410. }
  411. ps, err := strconv.ParseInt(psStr, 10, 64)
  412. if err != nil || ps <= 0 {
  413. ps = 20
  414. }
  415. aidStr := params.Get("aid")
  416. mid, _ := midI.(int64)
  417. ip := metadata.String(c, metadata.RemoteIP)
  418. data, err := danmuSvc.DmReportList(c, mid, pn, ps, aidStr, ip)
  419. if err != nil {
  420. c.JSON(nil, err)
  421. return
  422. }
  423. c.JSON(data, nil)
  424. }
  425. func webUserMid(c *bm.Context) {
  426. params := c.Request.Form
  427. _, ok := c.Get("mid")
  428. if !ok {
  429. c.JSON(nil, ecode.NoLogin)
  430. return
  431. }
  432. nameStr := params.Get("name")
  433. ip := metadata.String(c, metadata.RemoteIP)
  434. mid, err := danmuSvc.UserMid(c, nameStr, ip)
  435. if err != nil {
  436. c.JSON(nil, err)
  437. return
  438. }
  439. c.JSONMap(map[string]interface{}{
  440. "mid": mid,
  441. }, nil)
  442. }