services-detail.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 ServicesDetailController struct {
  13. beego.Controller
  14. }
  15. func (s *ServicesDetailController) Get() {
  16. getID := s.GetString(":id")
  17. var success []string
  18. //获取成功案例轮播图
  19. exams := service.GetResource("example")
  20. for _, v := range exams {
  21. success = append(success, v.Url)
  22. }
  23. atoi, _ := strconv.Atoi(getID)
  24. detail := service.GetServiceDetail(atoi)
  25. s.Data["example"] = success
  26. s.Data["serveDetail"] = detail
  27. s.TplName = "services-detail.html"
  28. }
  29. // AddServices 添加服务信息
  30. func (s *ServicesDetailController) AddServices() {
  31. var serve models.ServerDto
  32. var ser models.Server
  33. err := json.Unmarshal(s.Ctx.Input.RequestBody, &serve)
  34. if err != nil {
  35. s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  36. s.ServeJSON()
  37. return
  38. }
  39. ser.Title = serve.Title
  40. ser.Synopsis = serve.Synopsis
  41. ser.Detail = serve.Detail
  42. ser.ProductId = serve.ProductId
  43. ser.Image = serve.Image
  44. add, err := unity.Add(ser)
  45. if err != nil {
  46. s.Data["json"] = &JSON{Code: 101, Msg: "添加失败"}
  47. s.ServeJSON()
  48. return
  49. } else {
  50. s.Data["json"] = &JSON{Code: 200, Msg: "添加成功", Data: add}
  51. s.ServeJSON()
  52. }
  53. }
  54. // UpdateServices 更新服务信息
  55. func (s *ServicesDetailController) UpdateServices() {
  56. var serve models.ServerDto
  57. var ser models.Server
  58. err := json.Unmarshal(s.Ctx.Input.RequestBody, &serve)
  59. if err != nil {
  60. s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  61. s.ServeJSON()
  62. return
  63. }
  64. ser.Title = serve.Title
  65. ser.Synopsis = serve.Synopsis
  66. ser.Detail = serve.Detail
  67. ser.ProductId = serve.ProductId
  68. ser.Image = serve.Image
  69. _, err = unity.UpdateById(serve.ID, &ser)
  70. if err != nil {
  71. s.Data["json"] = &JSON{Code: 101, Msg: "更新失败"}
  72. s.ServeJSON()
  73. return
  74. } else {
  75. s.Data["json"] = &JSON{Code: 200, Msg: "更新成功"}
  76. s.ServeJSON()
  77. }
  78. }
  79. // DeleteServices 删除服务信息
  80. func (s *ServicesDetailController) DeleteServices() {
  81. getString := s.GetString("id")
  82. id, _ := strconv.Atoi(getString)
  83. validate := validator.New()
  84. err2 := validate.Var(id, "required")
  85. if err2 != nil {
  86. s.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  87. s.ServeJSON()
  88. return
  89. }
  90. _, err := unity.DeleteById(id, &models.Server{})
  91. if err != nil {
  92. s.Data["json"] = &JSON{Code: 101, Msg: "删除失败"}
  93. s.ServeJSON()
  94. return
  95. } else {
  96. s.Data["json"] = &JSON{Code: 200, Msg: "删除成功"}
  97. s.ServeJSON()
  98. }
  99. }
  100. // GetAllServices 获取所有服务信息
  101. func (s *ServicesDetailController) GetAllServices() {
  102. var params unity.PageParams
  103. err := json.Unmarshal(s.Ctx.Input.RequestBody, &params)
  104. if err != nil {
  105. s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  106. s.ServeJSON()
  107. return
  108. }
  109. result, total, current, err := unity.Paginate(params, &models.Server{})
  110. if err != nil {
  111. s.Data["json"] = &JSON{Code: 101, Msg: "获取失败"}
  112. s.ServeJSON()
  113. return
  114. } else {
  115. s.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: JSONS{
  116. Current: current,
  117. Size: total,
  118. Data: result,
  119. }}
  120. s.ServeJSON()
  121. return
  122. }
  123. }
  124. func (s *ServicesDetailController) GetServices() {
  125. getString := s.GetString("id")
  126. id, _ := strconv.Atoi(getString)
  127. validate := validator.New()
  128. err2 := validate.Var(id, "required")
  129. if err2 != nil {
  130. s.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  131. s.ServeJSON()
  132. return
  133. }
  134. result, err := unity.GetById(id, &models.Server{})
  135. if err != nil {
  136. s.Data["json"] = &JSON{Code: 101, Msg: "获取失败"}
  137. s.ServeJSON()
  138. return
  139. } else {
  140. s.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: result}
  141. s.ServeJSON()
  142. return
  143. }
  144. }
  145. // GetServe 获取服务类型
  146. func (s *ServicesDetailController) GetServe() {
  147. var product []models.Products
  148. tx := utils.DB.Where("type=?", "serve").Find(&product)
  149. if tx.Error != nil {
  150. s.Data["json"] = &JSON{Code: 103, Msg: "获取失败", Data: product}
  151. s.ServeJSON()
  152. return
  153. } else {
  154. s.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: product}
  155. s.ServeJSON()
  156. return
  157. }
  158. }