new.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package controllers
  2. import (
  3. "cc-officialweb/models"
  4. "cc-officialweb/service"
  5. "cc-officialweb/unity"
  6. "cc-officialweb/utils"
  7. "encoding/json"
  8. beego "github.com/beego/beego/v2/server/web"
  9. "github.com/go-playground/validator/v10"
  10. "strconv"
  11. )
  12. type NewController struct {
  13. beego.Controller
  14. }
  15. func (n *NewController) Get() {
  16. types := n.GetString("types")
  17. news := service.GetNews(types)
  18. n.Data["News"] = news
  19. n.TplName = "news.html"
  20. }
  21. // NewDetail 根据id获取对应新闻详情
  22. func (n *NewController) NewDetail() {
  23. var success []string
  24. param := n.Ctx.Input.Param(":id")
  25. atoi, _ := strconv.Atoi(param)
  26. id, err := unity.GetById(atoi, &models.News{})
  27. if err == nil {
  28. n.Data["detail"] = id
  29. }
  30. exams := service.GetResource("example")
  31. for _, v := range exams {
  32. success = append(success, v.Url)
  33. }
  34. n.Data["example"] = success
  35. n.TplName = "news-detail.html"
  36. }
  37. // GetAllNew 获得所有新闻消息
  38. func (n *NewController) GetAllNew() {
  39. var page unity.PageParams
  40. err := json.Unmarshal(n.Ctx.Input.RequestBody, &page)
  41. if err != nil {
  42. n.Data["json"] = &JSON{Code: 101, Msg: "参数错误"}
  43. n.ServeJSON()
  44. return
  45. }
  46. result, total, current, err := unity.Paginate(page, models.News{})
  47. if err != nil {
  48. n.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
  49. n.ServeJSON()
  50. return
  51. } else {
  52. n.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{
  53. Current: current,
  54. Size: total,
  55. Data: result,
  56. }}
  57. n.ServeJSON()
  58. return
  59. }
  60. }
  61. // GetNewDetail 获得详细新闻
  62. func (n *NewController) GetNewDetail() {
  63. getString := n.GetString("id")
  64. id, _ := strconv.Atoi(getString)
  65. validate := validator.New()
  66. err2 := validate.Var(id, "required")
  67. if err2 != nil {
  68. n.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  69. n.ServeJSON()
  70. return
  71. }
  72. result, err := unity.GetById(id, &models.News{})
  73. if err != nil {
  74. n.Data["json"] = &JSON{Code: 101, Msg: "获取失败"}
  75. n.ServeJSON()
  76. return
  77. } else {
  78. n.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: result}
  79. n.ServeJSON()
  80. return
  81. }
  82. }
  83. // GetNewsType 获取新闻类型
  84. func (n *NewController) GetNewsType() {
  85. var news []models.News
  86. tx := utils.DB.Find(&news)
  87. if tx.RowsAffected > 0 {
  88. n.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: news}
  89. n.ServeJSON()
  90. return
  91. } else {
  92. n.Data["json"] = &JSON{Code: 101, Msg: "获取失败"}
  93. n.ServeJSON()
  94. return
  95. }
  96. }
  97. // UpdateNews 修改新闻
  98. func (n *NewController) UpdateNews() {
  99. var newsdto models.NewsDto
  100. var news models.News
  101. err := json.Unmarshal(n.Ctx.Input.RequestBody, &newsdto)
  102. if err != nil {
  103. n.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  104. n.ServeJSON()
  105. return
  106. }
  107. news.Title = newsdto.Title
  108. news.Detail = newsdto.Detail
  109. news.Image = newsdto.Image
  110. news.Synopsis = newsdto.Synopsis
  111. news.IsActive = newsdto.IsActive
  112. news.Types = newsdto.Types
  113. _, err = unity.UpdateById(newsdto.ID, &news)
  114. if err != nil {
  115. n.Data["json"] = &JSON{Code: 102, Msg: err.Error()}
  116. n.ServeJSON()
  117. return
  118. } else {
  119. n.Data["json"] = &JSON{Code: 200, Msg: "修改成功"}
  120. n.ServeJSON()
  121. }
  122. }
  123. // DeleteNewsById 根据id删除新闻
  124. func (n *NewController) DeleteNewsById() {
  125. id := n.GetString("id")
  126. atoi, _ := strconv.Atoi(id)
  127. validate := validator.New()
  128. err2 := validate.Var(atoi, "required")
  129. if err2 != nil {
  130. n.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  131. n.ServeJSON()
  132. return
  133. }
  134. _, err := unity.DeleteById(atoi, models.News{})
  135. if err != nil {
  136. n.Data["json"] = map[string]interface{}{"code": 101, "msg": "删除失败"}
  137. n.ServeJSON()
  138. return
  139. } else {
  140. n.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
  141. n.ServeJSON()
  142. }
  143. }
  144. // AddNews 添加新闻
  145. func (n *NewController) AddNews() {
  146. var newsDto models.NewsDto
  147. err := json.Unmarshal(n.Ctx.Input.RequestBody, &newsDto)
  148. if err != nil {
  149. n.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  150. n.ServeJSON()
  151. return
  152. }
  153. if service.AddNews(newsDto) {
  154. n.Data["json"] = &JSON{Code: 200, Msg: "添加成功"}
  155. n.ServeJSON()
  156. } else {
  157. n.Data["json"] = &JSON{Code: 102, Msg: "添加失败"}
  158. n.ServeJSON()
  159. return
  160. }
  161. }