123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- package http
- import (
- "strconv"
- "go-common/app/interface/main/playlist/conf"
- "go-common/app/interface/main/playlist/model"
- favmdl "go-common/app/service/main/favorite/model"
- "go-common/library/ecode"
- bm "go-common/library/net/http/blademaster"
- )
- func whiteList(c *bm.Context) {
- var (
- err error
- vmid, mid int64
- )
- params := c.Request.Form
- if midStr, ok := c.Get("mid"); ok {
- mid = midStr.(int64)
- }
- vmidStr := params.Get("vmid")
- if vmidStr != "" {
- if vmid, err = strconv.ParseInt(vmidStr, 10, 64); err != nil {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- }
- if mid == 0 && vmid <= 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- if vmid > 0 {
- mid = vmid
- }
- c.JSON(plSvc.White(c, mid))
- }
- func add(c *bm.Context) {
- var (
- err error
- mid, pid, public int64
- name, description, cover string
- )
- params := c.Request.Form
- midStr, _ := c.Get("mid")
- mid = midStr.(int64)
- publicStr := params.Get("public")
- if publicStr != "" {
- if public, err = strconv.ParseInt(publicStr, 10, 64); err != nil {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- }
- name = params.Get("name")
- if name == "" || len([]rune(name)) > conf.Conf.Rule.MaxNameLimit {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- description = params.Get("description")
- if description != "" {
- if len([]rune(description)) > conf.Conf.Rule.MaxPlDescLimit {
- c.JSON(nil, ecode.PlDescTooLong)
- return
- }
- }
- cover = params.Get("cover")
- if pid, err = plSvc.Add(c, mid, int8(public), name, description, cover, c.Request.Header.Get("Cookie"), params.Get("access_key")); err != nil {
- c.JSON(nil, switchCode(err, favmdl.TypePlayVideo))
- return
- }
- data := make(map[string]interface{}, 1)
- data["pid"] = pid
- c.JSON(data, nil)
- }
- func del(c *bm.Context) {
- var (
- err error
- mid, pid int64
- )
- params := c.Request.Form
- midStr, _ := c.Get("mid")
- mid = midStr.(int64)
- pidStr := params.Get("pid")
- if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid < 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- c.JSON(nil, switchCode(plSvc.Del(c, mid, pid), favmdl.TypePlayVideo))
- }
- func update(c *bm.Context) {
- var (
- err error
- mid, pid, public int64
- name, description, cover string
- )
- params := c.Request.Form
- midStr, _ := c.Get("mid")
- mid = midStr.(int64)
- publicStr := params.Get("public")
- if publicStr != "" {
- if public, err = strconv.ParseInt(publicStr, 10, 64); err != nil {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- }
- pidStr := params.Get("pid")
- if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid < 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- name = params.Get("name")
- if name == "" || len([]rune(name)) > conf.Conf.Rule.MaxNameLimit {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- description = params.Get("description")
- if description != "" {
- if len([]rune(description)) > conf.Conf.Rule.MaxPlDescLimit {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- }
- cover = params.Get("cover")
- 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))
- }
- func info(c *bm.Context) {
- var (
- err error
- pid, mid int64
- list *model.Playlist
- )
- params := c.Request.Form
- if midStr, ok := c.Get("mid"); ok {
- mid = midStr.(int64)
- }
- pidStr := params.Get("pid")
- if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid <= 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- if list, err = plSvc.Info(c, mid, pid); err != nil {
- c.JSON(nil, switchCode(err, favmdl.TypePlayVideo))
- return
- }
- c.JSON(list, nil)
- }
- func report(c *bm.Context) {
- var (
- err error
- pid, aid int64
- )
- params := c.Request.Form
- pidStr := params.Get("pid")
- if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid <= 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- aidStr := params.Get("aid")
- if aidStr != "" {
- if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil || aid <= 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- }
- c.JSON(true, plSvc.PubView(c, pid, aid))
- }
- func reportShare(c *bm.Context) {
- var (
- err error
- pid, aid int64
- )
- params := c.Request.Form
- pidStr := params.Get("pid")
- if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid <= 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- aidStr := params.Get("aid")
- if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil || aid <= 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- c.JSON(true, plSvc.PubShare(c, pid, aid))
- }
- func list(c *bm.Context) {
- var (
- err error
- vmid, mid int64
- pn, ps, sort, total int
- list []*model.Playlist
- )
- params := c.Request.Form
- if midStr, ok := c.Get("mid"); ok {
- mid = midStr.(int64)
- }
- vmidStr := params.Get("vmid")
- if vmidStr != "" {
- if vmid, err = strconv.ParseInt(vmidStr, 10, 64); err != nil {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- }
- if mid == 0 && vmid <= 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- if vmid > 0 {
- mid = vmid
- }
- pnStr := params.Get("pn")
- if pn, err = strconv.Atoi(pnStr); err != nil || pn < 1 {
- pn = 1
- }
- psStr := params.Get("ps")
- if ps, err = strconv.Atoi(psStr); err != nil || ps < 1 || ps > conf.Conf.Rule.MaxPlsPageSize {
- ps = conf.Conf.Rule.MaxPlsPageSize
- }
- sortStr := params.Get("sort")
- if sortStr != "" {
- if sort, err = strconv.Atoi(sortStr); err != nil || sort < 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- }
- if list, total, err = plSvc.List(c, mid, pn, ps, sort); err != nil {
- c.JSON(nil, switchCode(err, favmdl.TypePlayVideo))
- return
- }
- data := make(map[string]interface{}, 2)
- page := map[string]int{
- "num": pn,
- "size": ps,
- "total": total,
- }
- data["page"] = page
- data["list"] = list
- c.JSON(data, nil)
- }
- func addFavorite(c *bm.Context) {
- var (
- err error
- mid, pid int64
- )
- params := c.Request.Form
- midStr, _ := c.Get("mid")
- mid = midStr.(int64)
- pidStr := params.Get("pid")
- if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid < 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- c.JSON(nil, switchCode(plSvc.AddFavorite(c, mid, pid), favmdl.TypePlayList))
- }
- func delFavorite(c *bm.Context) {
- var (
- err error
- mid, pid int64
- )
- params := c.Request.Form
- midStr, _ := c.Get("mid")
- mid = midStr.(int64)
- pidStr := params.Get("pid")
- if pid, err = strconv.ParseInt(pidStr, 10, 64); err != nil || pid < 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- c.JSON(nil, switchCode(plSvc.DelFavorite(c, mid, pid), favmdl.TypePlayList))
- }
- func listFavorite(c *bm.Context) {
- var (
- err error
- mid, vmid int64
- pn, ps, sort, total int
- list []*model.Playlist
- )
- params := c.Request.Form
- if midStr, ok := c.Get("mid"); ok {
- mid = midStr.(int64)
- }
- vmidStr := params.Get("vmid")
- if vmidStr != "" {
- if vmid, err = strconv.ParseInt(vmidStr, 10, 64); err != nil {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- }
- pnStr := params.Get("pn")
- if pn, err = strconv.Atoi(pnStr); err != nil || pn < 1 {
- pn = 1
- }
- psStr := params.Get("ps")
- if ps, err = strconv.Atoi(psStr); err != nil || ps < 1 || ps > conf.Conf.Rule.MaxPlsPageSize {
- ps = conf.Conf.Rule.MaxPlsPageSize
- }
- sortStr := params.Get("sort")
- if sortStr != "" {
- if sort, err = strconv.Atoi(sortStr); err != nil || sort < 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- }
- if list, total, err = plSvc.ListFavorite(c, mid, vmid, pn, ps, sort); err != nil {
- c.JSON(nil, switchCode(err, favmdl.TypePlayList))
- return
- }
- data := make(map[string]interface{}, 2)
- page := map[string]int{
- "num": pn,
- "size": ps,
- "total": total,
- }
- data["page"] = page
- data["list"] = list
- c.JSON(data, nil)
- }
- func switchCode(err error, tp int8) error {
- if err == nil {
- return err
- }
- switch ecode.Cause(err) {
- case ecode.FavNameTooLong:
- err = ecode.PlNameTooLong
- case ecode.FavFolderExist:
- err = ecode.PlExist
- case ecode.FavMaxFolderCount:
- err = ecode.PlMaxCount
- case ecode.FavCanNotDelDefault:
- err = ecode.PlCanNotDelDefault
- case ecode.FavFloderAlreadyDel:
- err = ecode.PlAlreadyDel
- case ecode.FavResourceOverflow:
- err = ecode.PlVideoOverflow
- case ecode.FavResourceAlreadyDel:
- if tp == favmdl.TypePlayVideo {
- err = ecode.PlVideoAlreadyDel
- } else if tp == favmdl.TypePlayList {
- err = ecode.PlFavAlreadyDel
- }
- case ecode.FavResourceExist:
- if tp == favmdl.TypePlayVideo {
- err = ecode.PlVideoExist
- } else if tp == favmdl.TypePlayList {
- err = ecode.PlFavExist
- }
- case ecode.FavFolderNotExist:
- err = ecode.PlNotExist
- }
- return err
- }
|