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 NewController struct { beego.Controller } func (n *NewController) Get() { types := n.GetString("types") news := service.GetNews(types) n.Data["News"] = news //获取产品分类 productType := service.GetProductType() if len(productType) == 0 { n.Data["productFirst"] = "暂无产品分类" } else { n.Data["productFirst"] = productType[0].Name } n.Data["productType"] = productType n.TplName = "news.html" } // NewDetail 根据id获取对应新闻详情 func (n *NewController) NewDetail() { var success []string param := n.Ctx.Input.Param(":id") atoi, _ := strconv.Atoi(param) id, err := unity.GetById(atoi, &models.News{}) if err == nil { n.Data["detail"] = id } exams := service.GetResource("example") for _, v := range exams { success = append(success, v.Url) } n.Data["example"] = success n.TplName = "news-detail.html" } // GetAllNew 获得所有新闻消息 func (n *NewController) GetAllNew() { var page unity.PageParams err := json.Unmarshal(n.Ctx.Input.RequestBody, &page) if err != nil { n.Data["json"] = &JSON{Code: 101, Msg: "参数错误"} n.ServeJSON() return } result, total, current, err := unity.Paginate(page, models.News{}) if err != nil { n.Data["json"] = &JSON{Code: 101, Msg: "查询失败"} n.ServeJSON() return } else { n.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{ Current: current, Size: total, Data: result, }} n.ServeJSON() return } } // GetNewDetail 获得详细新闻 func (n *NewController) GetNewDetail() { getString := n.GetString("id") id, _ := strconv.Atoi(getString) validate := validator.New() err2 := validate.Var(id, "required") if err2 != nil { n.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"} n.ServeJSON() return } result, err := unity.GetById(id, &models.News{}) if err != nil { n.Data["json"] = &JSON{Code: 101, Msg: "获取失败"} n.ServeJSON() return } else { n.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: result} n.ServeJSON() return } } // GetNewsType 获取新闻类型 func (n *NewController) GetNewsType() { var news []models.News tx := utils.DB.Find(&news) if tx.RowsAffected > 0 { n.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: news} n.ServeJSON() return } else { n.Data["json"] = &JSON{Code: 101, Msg: "获取失败"} n.ServeJSON() return } } // UpdateNews 修改新闻 func (n *NewController) UpdateNews() { var newsdto models.NewsDto var news models.News err := json.Unmarshal(n.Ctx.Input.RequestBody, &newsdto) if err != nil { n.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"} n.ServeJSON() return } news.Title = newsdto.Title news.Detail = newsdto.Detail news.Image = newsdto.Image news.Synopsis = newsdto.Synopsis news.IsActive = newsdto.IsActive news.Types = newsdto.Types _, err = unity.UpdateById(newsdto.ID, &news) if err != nil { n.Data["json"] = &JSON{Code: 102, Msg: err.Error()} n.ServeJSON() return } else { n.Data["json"] = &JSON{Code: 200, Msg: "修改成功"} n.ServeJSON() } } // DeleteNewsById 根据id删除新闻 func (n *NewController) DeleteNewsById() { id := n.GetString("id") atoi, _ := strconv.Atoi(id) validate := validator.New() err2 := validate.Var(atoi, "required") if err2 != nil { n.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"} n.ServeJSON() return } _, err := unity.DeleteById(atoi, models.News{}) if err != nil { n.Data["json"] = map[string]interface{}{"code": 101, "msg": "删除失败"} n.ServeJSON() return } else { n.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"} n.ServeJSON() } } // AddNews 添加新闻 func (n *NewController) AddNews() { var newsDto models.NewsDto err := json.Unmarshal(n.Ctx.Input.RequestBody, &newsDto) if err != nil { n.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"} n.ServeJSON() return } if service.AddNews(newsDto) { n.Data["json"] = &JSON{Code: 200, Msg: "添加成功"} n.ServeJSON() } else { n.Data["json"] = &JSON{Code: 102, Msg: "添加失败"} n.ServeJSON() return } }