package controllers import ( "cc-officialweb/models" "cc-officialweb/service" "cc-officialweb/unity" "cc-officialweb/utils" "encoding/json" beego "github.com/beego/beego/v2/server/web" "github.com/go-playground/validator/v10" "strconv" ) type ServicesDetailController struct { beego.Controller } func (s *ServicesDetailController) Get() { getID := s.GetString(":id") var success []string //获取成功案例轮播图 exams := service.GetResource("example") for _, v := range exams { success = append(success, v.Url) } atoi, _ := strconv.Atoi(getID) detail := service.GetServiceDetail(atoi) s.Data["example"] = success s.Data["serveDetail"] = detail //获取产品分类 productType := service.GetProductType() if len(productType) == 0 { s.Data["productFirst"] = "暂无产品分类" } else { s.Data["productFirst"] = productType[0].Name } s.Data["productType"] = productType s.TplName = "services-detail.html" } // AddServices 添加服务信息 func (s *ServicesDetailController) AddServices() { var serve models.ServerDto var ser models.Server err := json.Unmarshal(s.Ctx.Input.RequestBody, &serve) if err != nil { s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"} s.ServeJSON() return } ser.Title = serve.Title ser.Synopsis = serve.Synopsis ser.Detail = serve.Detail ser.ProductId = serve.ProductId ser.Image = serve.Image add, err := unity.Add(ser) if err != nil { s.Data["json"] = &JSON{Code: 101, Msg: "添加失败"} s.ServeJSON() return } else { s.Data["json"] = &JSON{Code: 200, Msg: "添加成功", Data: add} s.ServeJSON() } } // UpdateServices 更新服务信息 func (s *ServicesDetailController) UpdateServices() { var serve models.ServerDto var ser models.Server err := json.Unmarshal(s.Ctx.Input.RequestBody, &serve) if err != nil { s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"} s.ServeJSON() return } ser.Title = serve.Title ser.Synopsis = serve.Synopsis ser.Detail = serve.Detail ser.ProductId = serve.ProductId ser.Image = serve.Image _, err = unity.UpdateById(serve.ID, &ser) if err != nil { s.Data["json"] = &JSON{Code: 101, Msg: "更新失败"} s.ServeJSON() return } else { s.Data["json"] = &JSON{Code: 200, Msg: "更新成功"} s.ServeJSON() } } // DeleteServices 删除服务信息 func (s *ServicesDetailController) DeleteServices() { getString := s.GetString("id") id, _ := strconv.Atoi(getString) validate := validator.New() err2 := validate.Var(id, "required") if err2 != nil { s.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"} s.ServeJSON() return } _, err := unity.DeleteById(id, &models.Server{}) if err != nil { s.Data["json"] = &JSON{Code: 101, Msg: "删除失败"} s.ServeJSON() return } else { s.Data["json"] = &JSON{Code: 200, Msg: "删除成功"} s.ServeJSON() } } // GetAllServices 获取所有服务信息 func (s *ServicesDetailController) GetAllServices() { var params unity.PageParams err := json.Unmarshal(s.Ctx.Input.RequestBody, ¶ms) if err != nil { s.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"} s.ServeJSON() return } result, total, current, err := unity.Paginate(params, &models.Server{}) if err != nil { s.Data["json"] = &JSON{Code: 101, Msg: "获取失败"} s.ServeJSON() return } else { s.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: JSONS{ Current: current, Size: total, Data: result, }} s.ServeJSON() return } } func (s *ServicesDetailController) GetServices() { getString := s.GetString("id") id, _ := strconv.Atoi(getString) validate := validator.New() err2 := validate.Var(id, "required") if err2 != nil { s.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"} s.ServeJSON() return } result, err := unity.GetById(id, &models.Server{}) if err != nil { s.Data["json"] = &JSON{Code: 101, Msg: "获取失败"} s.ServeJSON() return } else { s.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: result} s.ServeJSON() return } } // GetServe 获取服务类型 func (s *ServicesDetailController) GetServe() { var product []models.Products tx := utils.DB.Where("type=?", "serve").Find(&product) if tx.Error != nil { s.Data["json"] = &JSON{Code: 103, Msg: "获取失败", Data: product} s.ServeJSON() return } else { s.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: product} s.ServeJSON() return } }