modules.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package http
  2. import (
  3. "strconv"
  4. "time"
  5. "go-common/app/admin/main/tv/model"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. func modulesAdd(c *bm.Context) {
  12. var (
  13. err error
  14. res = map[string]interface{}{}
  15. )
  16. param := new(model.Modules)
  17. if err = c.Bind(param); err != nil {
  18. return
  19. }
  20. if err = modValid(param.ModCore); err != nil {
  21. c.JSON(nil, err)
  22. return
  23. }
  24. if err = tvSrv.ModulesAdd(param); err != nil {
  25. res["message"] = "添加模块列表失败!" + err.Error()
  26. c.JSONMap(res, ecode.RequestErr)
  27. return
  28. }
  29. pageID := strconv.Itoa(int(param.ID))
  30. if err := tvSrv.SetPublish(c, pageID, model.ModulesPublishNo); err != nil {
  31. res["message"] = "设置模块发布状态失败!"
  32. c.JSONMap(res, ecode.RequestErr)
  33. return
  34. }
  35. c.JSON(nil, nil)
  36. }
  37. func modValid(core model.ModCore) (err error) {
  38. var t int
  39. if t, err = strconv.Atoi(core.Type); err != nil {
  40. log.Error("atoi %s, Err %v", core.Type, err)
  41. return
  42. }
  43. if t < 2 || t > 7 {
  44. err = ecode.RequestErr
  45. return
  46. }
  47. if sup := tvSrv.TypeSupport(core.SrcType, atoi(core.Source)); !sup {
  48. err = ecode.RequestErr
  49. }
  50. return
  51. }
  52. func modulesEditPost(c *bm.Context) {
  53. var (
  54. err error
  55. res = map[string]interface{}{}
  56. )
  57. param := new(model.ModulesAddParam)
  58. if err = c.Bind(param); err != nil {
  59. return
  60. }
  61. if err = modValid(param.ModCore); err != nil {
  62. c.JSON(nil, err)
  63. return
  64. }
  65. v := &model.Modules{
  66. PageID: param.PageID,
  67. Flexible: param.Flexible,
  68. Icon: param.Icon,
  69. Title: param.Title,
  70. Capacity: param.Capacity,
  71. More: param.More,
  72. Order: param.Order,
  73. Moretype: param.Moretype,
  74. Morepage: param.Morepage,
  75. ModCore: param.ModCore,
  76. }
  77. if err = tvSrv.ModulesEditPost(param.ID, v); err != nil {
  78. res["message"] = "编辑数据失败!" + err.Error()
  79. c.JSONMap(res, ecode.RequestErr)
  80. return
  81. }
  82. c.JSON(nil, nil)
  83. }
  84. func modulesList(c *bm.Context) {
  85. var (
  86. err error
  87. res = map[string]interface{}{}
  88. v []*model.Modules
  89. p model.ModPub
  90. )
  91. param := new(struct {
  92. PageID string `form:"page_id" validate:"required"`
  93. })
  94. if err = c.Bind(param); err != nil {
  95. return
  96. }
  97. if v, err = tvSrv.ModulesList(param.PageID); err != nil {
  98. res["message"] = "获取模块列表失败!" + err.Error()
  99. c.JSONMap(res, ecode.RequestErr)
  100. return
  101. }
  102. if p, err = tvSrv.GetModPub(c, param.PageID); err != nil {
  103. if err == memcache.ErrNotFound {
  104. nowTime := time.Now()
  105. t := nowTime.Format("2006-01-02 15:04:05")
  106. p = model.ModPub{
  107. Time: t,
  108. State: 0,
  109. }
  110. } else {
  111. res["message"] = "获取模块发布状态失败!" + err.Error()
  112. c.JSONMap(res, ecode.RequestErr)
  113. return
  114. }
  115. }
  116. m := &model.ModulesList{
  117. Items: v,
  118. PubState: p.State,
  119. PubTime: p.Time,
  120. }
  121. c.JSON(m, nil)
  122. }
  123. func modulesEditGet(c *bm.Context) {
  124. var (
  125. err error
  126. res = map[string]interface{}{}
  127. v = &model.Modules{}
  128. )
  129. param := new(struct {
  130. ID uint64 `form:"id" validate:"required"`
  131. })
  132. if err = c.Bind(param); err != nil {
  133. return
  134. }
  135. if v, err = tvSrv.ModulesEditGet(param.ID); err != nil {
  136. res["message"] = "获取数据失败!" + err.Error()
  137. c.JSONMap(res, ecode.RequestErr)
  138. return
  139. }
  140. c.JSON(v, nil)
  141. }
  142. func modulesPublish(c *bm.Context) {
  143. var (
  144. err error
  145. res = map[string]interface{}{}
  146. tmp = &model.Param{}
  147. tmpRes []*model.RegList
  148. )
  149. param := new(struct {
  150. IDs []int `form:"ids,split" validate:"required,min=1,dive,gt=0"`
  151. DeletedIDs []int `form:"deleted_ids,split" validate:"required,dive,gt=0"`
  152. PageID string `form:"page_id" validate:"required"`
  153. })
  154. if err = c.Bind(param); err != nil {
  155. return
  156. }
  157. if param.PageID != "0" {
  158. tmp.PageID = param.PageID
  159. if tmpRes, err = tvSrv.RegList(c, tmp); err != nil || len(tmpRes) != 1 {
  160. log.Error("search region is publish count(%d) error(%v)", len(tmpRes), err)
  161. c.JSON(nil, ecode.RequestErr)
  162. return
  163. }
  164. if tmpRes[0].Valid == 0 {
  165. log.Error("该分区region(%d)未上线 valid(%d)", param.PageID, tmpRes[0].Valid)
  166. c.JSON(nil, ecode.RequestErr)
  167. return
  168. }
  169. }
  170. if err := tvSrv.ModulesPublish(c, param.PageID, model.ModulesPublishYes, param.IDs, param.DeletedIDs); err != nil {
  171. res["message"] = "模块发布失败!" + err.Error()
  172. c.JSONMap(res, ecode.RequestErr)
  173. return
  174. }
  175. c.JSON(nil, nil)
  176. }
  177. func supCat(c *bm.Context) {
  178. if len(tvSrv.SupCats) > 0 {
  179. c.JSON(tvSrv.SupCats, nil)
  180. return
  181. }
  182. log.Error("SupCats empty")
  183. c.JSON(nil, ecode.ServiceUnavailable)
  184. }