new.go 4.3 KB

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