favorite.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. package http
  2. import (
  3. "strconv"
  4. "go-common/app/interface/main/playlist/conf"
  5. "go-common/app/interface/main/playlist/model"
  6. favmdl "go-common/app/service/main/favorite/model"
  7. "go-common/library/ecode"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. func whiteList(c *bm.Context) {
  11. var (
  12. err error
  13. vmid, mid int64
  14. )
  15. params := c.Request.Form
  16. if midStr, ok := c.Get("mid"); ok {
  17. mid = midStr.(int64)
  18. }
  19. vmidStr := params.Get("vmid")
  20. if vmidStr != "" {
  21. if vmid, err = strconv.ParseInt(vmidStr, 10, 64); err != nil {
  22. c.JSON(nil, ecode.RequestErr)
  23. return
  24. }
  25. }
  26. if mid == 0 && vmid <= 0 {
  27. c.JSON(nil, ecode.RequestErr)
  28. return
  29. }
  30. if vmid > 0 {
  31. mid = vmid
  32. }
  33. c.JSON(plSvc.White(c, mid))
  34. }
  35. func add(c *bm.Context) {
  36. var (
  37. err error
  38. mid, pid, public int64
  39. name, description, cover string
  40. )
  41. params := c.Request.Form
  42. midStr, _ := c.Get("mid")
  43. mid = midStr.(int64)
  44. publicStr := params.Get("public")
  45. if publicStr != "" {
  46. if public, err = strconv.ParseInt(publicStr, 10, 64); err != nil {
  47. c.JSON(nil, ecode.RequestErr)
  48. return
  49. }
  50. }
  51. name = params.Get("name")
  52. if name == "" || len([]rune(name)) > conf.Conf.Rule.MaxNameLimit {
  53. c.JSON(nil, ecode.RequestErr)
  54. return
  55. }
  56. description = params.Get("description")
  57. if description != "" {
  58. if len([]rune(description)) > conf.Conf.Rule.MaxPlDescLimit {
  59. c.JSON(nil, ecode.PlDescTooLong)
  60. return
  61. }
  62. }
  63. cover = params.Get("cover")
  64. if pid, err = plSvc.Add(c, mid, int8(public), name, description, cover, c.Request.Header.Get("Cookie"), params.Get("access_key")); err != nil {
  65. c.JSON(nil, switchCode(err, favmdl.TypePlayVideo))
  66. return
  67. }
  68. data := make(map[string]interface{}, 1)
  69. data["pid"] = pid
  70. c.JSON(data, nil)
  71. }
  72. func del(c *bm.Context) {
  73. var (
  74. err error
  75. mid, pid int64
  76. )
  77. params := c.Request.Form
  78. midStr, _ := c.Get("mid")
  79. mid = midStr.(int64)
  80. pidStr := params.Get("pid")
  81. if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid < 0 {
  82. c.JSON(nil, ecode.RequestErr)
  83. return
  84. }
  85. c.JSON(nil, switchCode(plSvc.Del(c, mid, pid), favmdl.TypePlayVideo))
  86. }
  87. func update(c *bm.Context) {
  88. var (
  89. err error
  90. mid, pid, public int64
  91. name, description, cover string
  92. )
  93. params := c.Request.Form
  94. midStr, _ := c.Get("mid")
  95. mid = midStr.(int64)
  96. publicStr := params.Get("public")
  97. if publicStr != "" {
  98. if public, err = strconv.ParseInt(publicStr, 10, 64); err != nil {
  99. c.JSON(nil, ecode.RequestErr)
  100. return
  101. }
  102. }
  103. pidStr := params.Get("pid")
  104. if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid < 0 {
  105. c.JSON(nil, ecode.RequestErr)
  106. return
  107. }
  108. name = params.Get("name")
  109. if name == "" || len([]rune(name)) > conf.Conf.Rule.MaxNameLimit {
  110. c.JSON(nil, ecode.RequestErr)
  111. return
  112. }
  113. description = params.Get("description")
  114. if description != "" {
  115. if len([]rune(description)) > conf.Conf.Rule.MaxPlDescLimit {
  116. c.JSON(nil, ecode.RequestErr)
  117. return
  118. }
  119. }
  120. cover = params.Get("cover")
  121. c.JSON(nil, switchCode(plSvc.Update(c, mid, pid, int8(public), name, description, cover, c.Request.Header.Get("Cookie"), params.Get("access_key")), favmdl.TypePlayVideo))
  122. }
  123. func info(c *bm.Context) {
  124. var (
  125. err error
  126. pid, mid int64
  127. list *model.Playlist
  128. )
  129. params := c.Request.Form
  130. if midStr, ok := c.Get("mid"); ok {
  131. mid = midStr.(int64)
  132. }
  133. pidStr := params.Get("pid")
  134. if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid <= 0 {
  135. c.JSON(nil, ecode.RequestErr)
  136. return
  137. }
  138. if list, err = plSvc.Info(c, mid, pid); err != nil {
  139. c.JSON(nil, switchCode(err, favmdl.TypePlayVideo))
  140. return
  141. }
  142. c.JSON(list, nil)
  143. }
  144. func report(c *bm.Context) {
  145. var (
  146. err error
  147. pid, aid int64
  148. )
  149. params := c.Request.Form
  150. pidStr := params.Get("pid")
  151. if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid <= 0 {
  152. c.JSON(nil, ecode.RequestErr)
  153. return
  154. }
  155. aidStr := params.Get("aid")
  156. if aidStr != "" {
  157. if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil || aid <= 0 {
  158. c.JSON(nil, ecode.RequestErr)
  159. return
  160. }
  161. }
  162. c.JSON(true, plSvc.PubView(c, pid, aid))
  163. }
  164. func reportShare(c *bm.Context) {
  165. var (
  166. err error
  167. pid, aid int64
  168. )
  169. params := c.Request.Form
  170. pidStr := params.Get("pid")
  171. if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid <= 0 {
  172. c.JSON(nil, ecode.RequestErr)
  173. return
  174. }
  175. aidStr := params.Get("aid")
  176. if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil || aid <= 0 {
  177. c.JSON(nil, ecode.RequestErr)
  178. return
  179. }
  180. c.JSON(true, plSvc.PubShare(c, pid, aid))
  181. }
  182. func list(c *bm.Context) {
  183. var (
  184. err error
  185. vmid, mid int64
  186. pn, ps, sort, total int
  187. list []*model.Playlist
  188. )
  189. params := c.Request.Form
  190. if midStr, ok := c.Get("mid"); ok {
  191. mid = midStr.(int64)
  192. }
  193. vmidStr := params.Get("vmid")
  194. if vmidStr != "" {
  195. if vmid, err = strconv.ParseInt(vmidStr, 10, 64); err != nil {
  196. c.JSON(nil, ecode.RequestErr)
  197. return
  198. }
  199. }
  200. if mid == 0 && vmid <= 0 {
  201. c.JSON(nil, ecode.RequestErr)
  202. return
  203. }
  204. if vmid > 0 {
  205. mid = vmid
  206. }
  207. pnStr := params.Get("pn")
  208. if pn, err = strconv.Atoi(pnStr); err != nil || pn < 1 {
  209. pn = 1
  210. }
  211. psStr := params.Get("ps")
  212. if ps, err = strconv.Atoi(psStr); err != nil || ps < 1 || ps > conf.Conf.Rule.MaxPlsPageSize {
  213. ps = conf.Conf.Rule.MaxPlsPageSize
  214. }
  215. sortStr := params.Get("sort")
  216. if sortStr != "" {
  217. if sort, err = strconv.Atoi(sortStr); err != nil || sort < 0 {
  218. c.JSON(nil, ecode.RequestErr)
  219. return
  220. }
  221. }
  222. if list, total, err = plSvc.List(c, mid, pn, ps, sort); err != nil {
  223. c.JSON(nil, switchCode(err, favmdl.TypePlayVideo))
  224. return
  225. }
  226. data := make(map[string]interface{}, 2)
  227. page := map[string]int{
  228. "num": pn,
  229. "size": ps,
  230. "total": total,
  231. }
  232. data["page"] = page
  233. data["list"] = list
  234. c.JSON(data, nil)
  235. }
  236. func addFavorite(c *bm.Context) {
  237. var (
  238. err error
  239. mid, pid int64
  240. )
  241. params := c.Request.Form
  242. midStr, _ := c.Get("mid")
  243. mid = midStr.(int64)
  244. pidStr := params.Get("pid")
  245. if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid < 0 {
  246. c.JSON(nil, ecode.RequestErr)
  247. return
  248. }
  249. c.JSON(nil, switchCode(plSvc.AddFavorite(c, mid, pid), favmdl.TypePlayList))
  250. }
  251. func delFavorite(c *bm.Context) {
  252. var (
  253. err error
  254. mid, pid int64
  255. )
  256. params := c.Request.Form
  257. midStr, _ := c.Get("mid")
  258. mid = midStr.(int64)
  259. pidStr := params.Get("pid")
  260. if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid < 0 {
  261. c.JSON(nil, ecode.RequestErr)
  262. return
  263. }
  264. c.JSON(nil, switchCode(plSvc.DelFavorite(c, mid, pid), favmdl.TypePlayList))
  265. }
  266. func listFavorite(c *bm.Context) {
  267. var (
  268. err error
  269. mid, vmid int64
  270. pn, ps, sort, total int
  271. list []*model.Playlist
  272. )
  273. params := c.Request.Form
  274. if midStr, ok := c.Get("mid"); ok {
  275. mid = midStr.(int64)
  276. }
  277. vmidStr := params.Get("vmid")
  278. if vmidStr != "" {
  279. if vmid, err = strconv.ParseInt(vmidStr, 10, 64); err != nil {
  280. c.JSON(nil, ecode.RequestErr)
  281. return
  282. }
  283. }
  284. pnStr := params.Get("pn")
  285. if pn, err = strconv.Atoi(pnStr); err != nil || pn < 1 {
  286. pn = 1
  287. }
  288. psStr := params.Get("ps")
  289. if ps, err = strconv.Atoi(psStr); err != nil || ps < 1 || ps > conf.Conf.Rule.MaxPlsPageSize {
  290. ps = conf.Conf.Rule.MaxPlsPageSize
  291. }
  292. sortStr := params.Get("sort")
  293. if sortStr != "" {
  294. if sort, err = strconv.Atoi(sortStr); err != nil || sort < 0 {
  295. c.JSON(nil, ecode.RequestErr)
  296. return
  297. }
  298. }
  299. if list, total, err = plSvc.ListFavorite(c, mid, vmid, pn, ps, sort); err != nil {
  300. c.JSON(nil, switchCode(err, favmdl.TypePlayList))
  301. return
  302. }
  303. data := make(map[string]interface{}, 2)
  304. page := map[string]int{
  305. "num": pn,
  306. "size": ps,
  307. "total": total,
  308. }
  309. data["page"] = page
  310. data["list"] = list
  311. c.JSON(data, nil)
  312. }
  313. func switchCode(err error, tp int8) error {
  314. if err == nil {
  315. return err
  316. }
  317. switch ecode.Cause(err) {
  318. case ecode.FavNameTooLong:
  319. err = ecode.PlNameTooLong
  320. case ecode.FavFolderExist:
  321. err = ecode.PlExist
  322. case ecode.FavMaxFolderCount:
  323. err = ecode.PlMaxCount
  324. case ecode.FavCanNotDelDefault:
  325. err = ecode.PlCanNotDelDefault
  326. case ecode.FavFloderAlreadyDel:
  327. err = ecode.PlAlreadyDel
  328. case ecode.FavResourceOverflow:
  329. err = ecode.PlVideoOverflow
  330. case ecode.FavResourceAlreadyDel:
  331. if tp == favmdl.TypePlayVideo {
  332. err = ecode.PlVideoAlreadyDel
  333. } else if tp == favmdl.TypePlayList {
  334. err = ecode.PlFavAlreadyDel
  335. }
  336. case ecode.FavResourceExist:
  337. if tp == favmdl.TypePlayVideo {
  338. err = ecode.PlVideoExist
  339. } else if tp == favmdl.TypePlayList {
  340. err = ecode.PlFavExist
  341. }
  342. case ecode.FavFolderNotExist:
  343. err = ecode.PlNotExist
  344. }
  345. return err
  346. }