channel.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. package http
  2. import (
  3. "strconv"
  4. "go-common/app/interface/main/space/conf"
  5. "go-common/app/interface/main/space/model"
  6. "go-common/library/ecode"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/xstr"
  9. )
  10. func channel(c *bm.Context) {
  11. var (
  12. vmid, mid, cid int64
  13. isGuest bool
  14. err error
  15. )
  16. params := c.Request.Form
  17. if midInter, ok := c.Get("mid"); ok {
  18. mid = midInter.(int64)
  19. }
  20. vmidStr := params.Get("mid")
  21. cidStr := params.Get("cid")
  22. guestStr := params.Get("guest")
  23. if vmid, err = strconv.ParseInt(vmidStr, 10, 64); err != nil || (vmid <= 0 && mid <= 0) {
  24. c.JSON(nil, ecode.RequestErr)
  25. return
  26. }
  27. if cid, err = strconv.ParseInt(cidStr, 10, 64); err != nil || cid <= 0 {
  28. c.JSON(nil, ecode.RequestErr)
  29. return
  30. }
  31. if guestStr != "" {
  32. if isGuest, err = strconv.ParseBool(guestStr); err != nil {
  33. c.JSON(nil, ecode.RequestErr)
  34. return
  35. }
  36. }
  37. if !isGuest && vmid > 0 && mid != vmid {
  38. mid = vmid
  39. }
  40. c.JSON(spcSvc.Channel(c, mid, cid))
  41. }
  42. func channelIndex(c *bm.Context) {
  43. var (
  44. vmid, mid int64
  45. isGuest bool
  46. err error
  47. )
  48. params := c.Request.Form
  49. if midInter, ok := c.Get("mid"); ok {
  50. mid = midInter.(int64)
  51. }
  52. vmidStr := params.Get("mid")
  53. guestStr := params.Get("guest")
  54. if vmid, err = strconv.ParseInt(vmidStr, 10, 64); err != nil || (vmid <= 0 && mid <= 0) {
  55. c.JSON(nil, ecode.RequestErr)
  56. return
  57. }
  58. if guestStr != "" {
  59. if isGuest, err = strconv.ParseBool(guestStr); err != nil {
  60. c.JSON(nil, ecode.RequestErr)
  61. return
  62. }
  63. }
  64. if !isGuest && vmid > 0 && mid != vmid {
  65. isGuest = true
  66. mid = vmid
  67. }
  68. c.JSON(spcSvc.ChannelIndex(c, mid, isGuest))
  69. }
  70. func channelList(c *bm.Context) {
  71. var (
  72. vmid, mid int64
  73. channels []*model.Channel
  74. isGuest bool
  75. err error
  76. )
  77. params := c.Request.Form
  78. vmidStr := params.Get("mid")
  79. guestStr := params.Get("guest")
  80. if midInter, ok := c.Get("mid"); ok {
  81. mid = midInter.(int64)
  82. }
  83. if vmid, err = strconv.ParseInt(vmidStr, 10, 64); err != nil || (vmid <= 0 && mid <= 0) {
  84. c.JSON(nil, ecode.RequestErr)
  85. return
  86. }
  87. if guestStr != "" {
  88. if isGuest, err = strconv.ParseBool(guestStr); err != nil {
  89. c.JSON(nil, ecode.RequestErr)
  90. return
  91. }
  92. }
  93. if !isGuest && vmid > 0 && mid != vmid {
  94. isGuest = true
  95. mid = vmid
  96. }
  97. if channels, err = spcSvc.ChannelList(c, mid, isGuest); err != nil {
  98. c.JSON(nil, err)
  99. return
  100. }
  101. data := make(map[string]interface{}, 2)
  102. data["count"] = len(channels)
  103. data["list"] = channels
  104. c.JSON(data, nil)
  105. }
  106. func addChannel(c *bm.Context) {
  107. var (
  108. mid, cid int64
  109. err error
  110. )
  111. params := c.Request.Form
  112. midStr, _ := c.Get("mid")
  113. mid = midStr.(int64)
  114. name := params.Get("name")
  115. intro := params.Get("intro")
  116. if name == "" || len([]rune(name)) > conf.Conf.Rule.MaxChNameLen {
  117. c.JSON(nil, ecode.ChNameToLong)
  118. return
  119. }
  120. if intro != "" && len([]rune(intro)) > conf.Conf.Rule.MaxChIntroLen {
  121. c.JSON(nil, ecode.ChIntroToLong)
  122. return
  123. }
  124. if cid, err = spcSvc.AddChannel(c, mid, name, intro); err != nil {
  125. c.JSON(nil, err)
  126. return
  127. }
  128. c.JSON(struct {
  129. Cid int64 `json:"cid"`
  130. }{Cid: cid}, nil)
  131. }
  132. func editChannel(c *bm.Context) {
  133. var (
  134. mid, cid int64
  135. err error
  136. )
  137. params := c.Request.Form
  138. midStr, _ := c.Get("mid")
  139. mid = midStr.(int64)
  140. cidStr := params.Get("cid")
  141. name := params.Get("name")
  142. intro := params.Get("intro")
  143. if cid, err = strconv.ParseInt(cidStr, 10, 64); err != nil || cid <= 0 {
  144. c.JSON(nil, ecode.RequestErr)
  145. return
  146. }
  147. if name == "" {
  148. c.JSON(nil, ecode.RequestErr)
  149. return
  150. }
  151. if len([]rune(name)) > conf.Conf.Rule.MaxChNameLen {
  152. c.JSON(nil, ecode.ChNameToLong)
  153. return
  154. }
  155. if intro != "" && len([]rune(intro)) > conf.Conf.Rule.MaxChIntroLen {
  156. c.JSON(nil, ecode.ChIntroToLong)
  157. return
  158. }
  159. c.JSON(nil, spcSvc.EditChannel(c, mid, cid, name, intro))
  160. }
  161. func delChannel(c *bm.Context) {
  162. var (
  163. mid, cid int64
  164. err error
  165. )
  166. params := c.Request.Form
  167. midStr, _ := c.Get("mid")
  168. mid = midStr.(int64)
  169. cidStr := params.Get("cid")
  170. if cid, err = strconv.ParseInt(cidStr, 10, 64); err != nil || cid <= 0 {
  171. c.JSON(nil, ecode.RequestErr)
  172. return
  173. }
  174. c.JSON(nil, spcSvc.DelChannel(c, mid, cid))
  175. }
  176. func channelVideo(c *bm.Context) {
  177. var (
  178. vmid, mid, cid int64
  179. pn, ps int
  180. isGuest, order bool
  181. channelDetail *model.ChannelDetail
  182. err error
  183. )
  184. params := c.Request.Form
  185. vmidStr := params.Get("mid")
  186. cidStr := params.Get("cid")
  187. pnStr := params.Get("pn")
  188. psStr := params.Get("ps")
  189. guestStr := params.Get("guest")
  190. orderStr := params.Get("order")
  191. if midInter, ok := c.Get("mid"); ok {
  192. mid = midInter.(int64)
  193. }
  194. if vmid, err = strconv.ParseInt(vmidStr, 10, 64); err != nil || (vmid <= 0 && mid <= 0) {
  195. c.JSON(nil, ecode.RequestErr)
  196. return
  197. }
  198. if cid, err = strconv.ParseInt(cidStr, 10, 64); err != nil || cid <= 0 {
  199. c.JSON(nil, ecode.RequestErr)
  200. return
  201. }
  202. if pn, err = strconv.Atoi(pnStr); err != nil || pn < 1 {
  203. pn = 1
  204. }
  205. if ps, err = strconv.Atoi(psStr); err != nil || ps < 1 || ps > conf.Conf.Rule.MaxChArcsPs {
  206. ps = conf.Conf.Rule.MaxChArcsPs
  207. }
  208. if guestStr != "" {
  209. if isGuest, err = strconv.ParseBool(guestStr); err != nil {
  210. c.JSON(nil, ecode.RequestErr)
  211. return
  212. }
  213. }
  214. if !isGuest && vmid > 0 && mid != vmid {
  215. isGuest = true
  216. mid = vmid
  217. }
  218. if orderStr != "" {
  219. if order, err = strconv.ParseBool(orderStr); err != nil {
  220. c.JSON(nil, ecode.RequestErr)
  221. return
  222. }
  223. }
  224. if channelDetail, err = spcSvc.ChannelVideos(c, mid, cid, pn, ps, isGuest, order); err != nil {
  225. c.JSON(nil, err)
  226. return
  227. }
  228. data := make(map[string]interface{}, 2)
  229. page := map[string]int{
  230. "num": pn,
  231. "size": ps,
  232. "count": channelDetail.Count,
  233. }
  234. data["page"] = page
  235. data["list"] = channelDetail
  236. c.JSON(data, nil)
  237. }
  238. func addChannelVideo(c *bm.Context) {
  239. var (
  240. mid, cid int64
  241. aids []int64
  242. err error
  243. )
  244. params := c.Request.Form
  245. midStr, _ := c.Get("mid")
  246. mid = midStr.(int64)
  247. cidStr := params.Get("cid")
  248. if cid, err = strconv.ParseInt(cidStr, 10, 64); err != nil || cid <= 0 {
  249. c.JSON(nil, ecode.RequestErr)
  250. return
  251. }
  252. aidStr := params.Get("aids")
  253. if aidStr == "" {
  254. c.JSON(nil, ecode.RequestErr)
  255. return
  256. }
  257. if aids, err = xstr.SplitInts(aidStr); err != nil || len(aids) == 0 || len(aids) > conf.Conf.Rule.MaxChArcAddLimit {
  258. c.JSON(nil, ecode.RequestErr)
  259. return
  260. }
  261. aidMap := make(map[int64]int64, len(aids))
  262. for _, aid := range aids {
  263. aidMap[aid] = aid
  264. }
  265. if len(aidMap) < len(aids) {
  266. c.JSON(nil, ecode.RequestErr)
  267. return
  268. }
  269. c.JSON(spcSvc.AddChannelArc(c, mid, cid, aids))
  270. }
  271. func delChannelVideo(c *bm.Context) {
  272. var (
  273. mid, cid, aid int64
  274. err error
  275. )
  276. params := c.Request.Form
  277. midStr, _ := c.Get("mid")
  278. mid = midStr.(int64)
  279. cidStr := params.Get("cid")
  280. if cid, err = strconv.ParseInt(cidStr, 10, 64); err != nil || cid <= 0 {
  281. c.JSON(nil, ecode.RequestErr)
  282. return
  283. }
  284. aidStr := params.Get("aid")
  285. if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil || aid <= 0 {
  286. c.JSON(nil, ecode.RequestErr)
  287. return
  288. }
  289. c.JSON(nil, spcSvc.DelChannelArc(c, mid, cid, aid))
  290. }
  291. func sortChannelVideo(c *bm.Context) {
  292. var (
  293. mid, cid, aid int64
  294. orderNum int
  295. err error
  296. )
  297. params := c.Request.Form
  298. midStr, _ := c.Get("mid")
  299. mid = midStr.(int64)
  300. cidStr := params.Get("cid")
  301. aidStr := params.Get("aid")
  302. toStr := params.Get("to")
  303. if cid, err = strconv.ParseInt(cidStr, 10, 64); err != nil || cid <= 0 {
  304. c.JSON(nil, ecode.RequestErr)
  305. return
  306. }
  307. if aid, err = strconv.ParseInt(aidStr, 10, 64); err != nil || aid <= 0 {
  308. c.JSON(nil, ecode.RequestErr)
  309. return
  310. }
  311. if orderNum, err = strconv.Atoi(toStr); err != nil || orderNum < 0 {
  312. c.JSON(nil, ecode.RequestErr)
  313. return
  314. }
  315. c.JSON(nil, spcSvc.SortChannelArc(c, mid, cid, aid, orderNum))
  316. }
  317. func checkChannelVideo(c *bm.Context) {
  318. var (
  319. mid, cid int64
  320. err error
  321. )
  322. midStr, _ := c.Get("mid")
  323. mid = midStr.(int64)
  324. cidStr := c.Request.Form.Get("cid")
  325. if cid, err = strconv.ParseInt(cidStr, 10, 64); err != nil || cid <= 0 {
  326. c.JSON(nil, ecode.RequestErr)
  327. return
  328. }
  329. c.JSON(nil, spcSvc.CheckChannelVideo(c, mid, cid))
  330. }