package controllers import ( "cc-officialweb/models" "cc-officialweb/service" "cc-officialweb/unity" "encoding/json" beego "github.com/beego/beego/v2/server/web" "github.com/go-playground/validator/v10" "strconv" ) type RecruitController struct { beego.Controller } func (r *RecruitController) Get() { recruit, err := service.GetRecruit("recruit") var success []string //获取成功案例轮播图 exams := service.GetResource("example") for _, v := range exams { success = append(success, v.Url) } if err == nil { r.Data["recruit"] = recruit } r.Data["example"] = success //获取产品分类 productType := service.GetProductType() if len(productType) == 0 { r.Data["productFirst"] = "暂无产品分类" } else { r.Data["productFirst"] = productType[0].Name } r.Data["productType"] = productType r.TplName = "recruit.html" } // AddRecruit 添加招募或联系我们 func (r *RecruitController) AddRecruit() { var content models.ContentDto err := json.Unmarshal(r.Ctx.Input.RequestBody, &content) if err != nil { r.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"} r.ServeJSON() return } var cont models.Content cont.Title = content.Title cont.Synopsis = content.Synopsis cont.Detail = content.Detail cont.Types = content.Types add, err := unity.Add(cont) if err != nil { r.Data["json"] = &JSON{Code: 101, Msg: "添加失败"} r.ServeJSON() return } else { r.Data["json"] = &JSON{Code: 200, Msg: "添加成功", Data: add} r.ServeJSON() } } // UpdateRecruit 更新招募和联系我们 func (r *RecruitController) UpdateRecruit() { var content models.ContentDto err := json.Unmarshal(r.Ctx.Input.RequestBody, &content) if err != nil { r.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"} r.ServeJSON() return } var cont models.Content cont.Title = content.Title cont.Synopsis = content.Synopsis cont.Detail = content.Detail cont.Types = content.Types id, err := unity.UpdateById(content.ID, cont) if err != nil { r.Data["json"] = &JSON{Code: 101, Msg: "更新失败"} r.ServeJSON() return } else { r.Data["json"] = &JSON{Code: 200, Msg: "更新成功", Data: id} r.ServeJSON() } } // DeleteRecruit 删除招募和联系我们 func (r *RecruitController) DeleteRecruit() { getString := r.GetString("id") id, _ := strconv.Atoi(getString) validate := validator.New() err2 := validate.Var(id, "required") if err2 != nil { r.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"} r.ServeJSON() return } _, err := unity.DeleteById(id, &models.Content{}) if err != nil { r.Data["json"] = &JSON{Code: 101, Msg: "删除失败"} r.ServeJSON() return } else { r.Data["json"] = &JSON{Code: 200, Msg: "删除成功"} r.ServeJSON() } } // GetAllRecruit 获取所有招募和联系我们 func (r *RecruitController) GetAllRecruit() { var params unity.PageParams err := json.Unmarshal(r.Ctx.Input.RequestBody, ¶ms) if err != nil { r.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"} r.ServeJSON() return } result, total, current, err := unity.Paginate(params, &models.Content{}) if err != nil { r.Data["json"] = &JSON{Code: 101, Msg: "获取失败"} r.ServeJSON() return } else { r.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: JSONS{ Current: current, Size: total, Data: result, }} r.ServeJSON() return } } // GetRecruit 获取单个招募和联系我们 func (r *RecruitController) GetRecruit() { getString := r.GetString("id") id, _ := strconv.Atoi(getString) validate := validator.New() err2 := validate.Var(id, "required") if err2 != nil { r.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"} r.ServeJSON() return } result, err := unity.GetById(id, &models.Content{}) if err != nil { r.Data["json"] = &JSON{Code: 101, Msg: "获取失败"} r.ServeJSON() return } else { r.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: result} r.ServeJSON() return } }