services-detail.go 4.3 KB

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