product.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 ProductController struct {
  13. beego.Controller
  14. }
  15. // Get 产品详情页
  16. func (s *ProductController) Get() {
  17. var success []string
  18. ptype := s.GetString("ptype")
  19. product := service.GetProduct(ptype)
  20. //获取成功案例轮播图
  21. exams := service.GetResource("example")
  22. for _, v := range exams {
  23. success = append(success, v.Url)
  24. }
  25. //数据大屏展示
  26. data, err := service.GetData(false)
  27. if err == nil {
  28. s.Data["Datas"] = data
  29. } else {
  30. s.Data["Datas"] = "未获得数据"
  31. }
  32. s.Data["Products"] = product
  33. s.Data["example"] = success
  34. s.TplName = "product.html"
  35. }
  36. // AddProduct 添加产品
  37. func (s *ProductController) AddProduct() {
  38. var product models.ProductDto
  39. err := json.Unmarshal(s.Ctx.Input.RequestBody, &product)
  40. if err != nil {
  41. s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  42. s.ServeJSON()
  43. return
  44. }
  45. if service.AddProduct(product) {
  46. s.Data["json"] = &JSON{Code: 200, Msg: "添加成功"}
  47. s.ServeJSON()
  48. } else {
  49. s.Data["json"] = &JSON{Code: 102, Msg: "添加失败"}
  50. s.ServeJSON()
  51. return
  52. }
  53. }
  54. // DeleteProductById 根据id删除产品
  55. func (s *ProductController) DeleteProductById() {
  56. id := s.GetString("id")
  57. atoi, _ := strconv.Atoi(id)
  58. validate := validator.New()
  59. err2 := validate.Var(atoi, "required")
  60. if err2 != nil {
  61. s.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  62. s.ServeJSON()
  63. return
  64. }
  65. _, err := unity.DeleteById(atoi, models.Products{})
  66. if err != nil {
  67. s.Data["json"] = map[string]interface{}{"code": 101, "msg": "删除失败"}
  68. s.ServeJSON()
  69. return
  70. } else {
  71. s.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
  72. s.ServeJSON()
  73. }
  74. }
  75. // GetAllProduct 获取所有产品信息
  76. func (s *ProductController) GetAllProduct() {
  77. var page unity.PageParams
  78. err := json.Unmarshal(s.Ctx.Input.RequestBody, &page)
  79. if err != nil {
  80. s.Data["json"] = &JSON{Code: 101, Msg: "参数错误"}
  81. s.ServeJSON()
  82. return
  83. }
  84. result, total, current, err := unity.Paginate(page, models.Products{})
  85. if err != nil {
  86. s.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
  87. s.ServeJSON()
  88. return
  89. } else {
  90. s.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{
  91. Current: current,
  92. Size: total,
  93. Data: result,
  94. }}
  95. s.ServeJSON()
  96. return
  97. }
  98. }
  99. // UpdateProductById 根据id进行修改产品
  100. func (s *ProductController) UpdateProductById() {
  101. var productDto models.ProductDto
  102. var product models.Products
  103. err := json.Unmarshal(s.Ctx.Input.RequestBody, &productDto)
  104. if err != nil {
  105. s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  106. s.ServeJSON()
  107. return
  108. }
  109. product.Title = &productDto.Title //标题
  110. product.Synopsis = &productDto.Synopsis //简介
  111. product.Detail = &productDto.Detail //详情
  112. product.Type = &productDto.Type //类型 1.产品 2.服务
  113. product.Ptype = &productDto.Ptype //产品类型 软件 硬件
  114. product.IsIndex = &productDto.IsIndex //是否首页显示
  115. product.Url = &productDto.Url //图片链接
  116. product.ProductIntroduction = &productDto.ProductIntroduction //产品介绍
  117. product.TechnicalParameters = &productDto.TechnicalParameters //技术参数
  118. product.Instructions = &productDto.Instructions //使用说明
  119. product.SupportingSoftware = &productDto.SupportingSoftware //配套软件
  120. product.OptionalAccessories = &productDto.OptionalAccessories //可选配件
  121. product.IsActive = &productDto.IsActive
  122. _, err = unity.UpdateById(productDto.ID, product)
  123. if err != nil {
  124. s.Data["json"] = &JSON{Code: 102, Msg: err.Error()}
  125. s.ServeJSON()
  126. return
  127. } else {
  128. s.Data["json"] = &JSON{Code: 200, Msg: "修改成功"}
  129. s.ServeJSON()
  130. }
  131. }
  132. // UpdateIsIndex 根据id修改状态
  133. func (s *ProductController) UpdateIsIndex() {
  134. var productDto models.ProductDto
  135. var product models.Products
  136. err := json.Unmarshal(s.Ctx.Input.RequestBody, &productDto)
  137. if err != nil {
  138. s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  139. s.ServeJSON()
  140. return
  141. }
  142. tx := utils.DB.Model(&product).Where("id = ?", productDto.ID).Update("is_index", productDto.IsIndex)
  143. if tx.RowsAffected > 0 {
  144. s.Data["json"] = &JSON{Code: 200, Msg: "修改成功"}
  145. s.ServeJSON()
  146. return
  147. } else {
  148. s.Data["json"] = &JSON{Code: 102, Msg: "修改失败"}
  149. s.ServeJSON()
  150. }
  151. }
  152. // GetProduct 根据id获取产品信息
  153. func (s *ProductController) GetProduct() {
  154. getString := s.GetString("id")
  155. id, _ := strconv.Atoi(getString)
  156. validate := validator.New()
  157. err2 := validate.Var(id, "required")
  158. if err2 != nil {
  159. s.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  160. s.ServeJSON()
  161. return
  162. }
  163. result, err := unity.GetById(id, &models.Products{})
  164. if err != nil {
  165. s.Data["json"] = &JSON{Code: 101, Msg: "获取失败"}
  166. s.ServeJSON()
  167. return
  168. } else {
  169. s.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: result}
  170. s.ServeJSON()
  171. return
  172. }
  173. }