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" "github.com/google/uuid" "strconv" "strings" ) type UploadImageController struct { beego.Controller } type JSON struct { Code int Msg any Data any } type JSONS struct { Current int Size int64 Data any } // Post 上传图片 func (up *UploadImageController) Post() { file, header, err := up.GetFile("file") types := up.GetString("types") if err != nil { up.Data["json"] = &JSON{Code: 101, Msg: "上传失败"} up.ServeJSON() return } defer file.Close() index := strings.Split(header.Filename, ".")[1] if index != "jpg" && index != "png" && index != "jpeg" { up.Data["json"] = &JSON{Code: 102, Msg: "文件类型错误"} up.ServeJSON() return } fileName := uuid.NewString() + "." + index if err := up.SaveToFile("file", "./static/upload/"+fileName); err != nil { up.Data["json"] = &JSON{Code: 101, Msg: "上传失败"} up.ServeJSON() return } var resourse models.Resource resourse.Name = fileName resourse.Url = "/image/" + fileName resourse.Type = types tx := utils.DB.Create(&resourse) if tx.RowsAffected > 0 { up.Data["json"] = &JSON{Code: 200, Msg: "上传成功", Data: resourse.Url} up.ServeJSON() return } } // UpdateResource 修改图片 func (up *UploadImageController) UpdateResource() { getString := up.GetString("id") atoi, _ := strconv.Atoi(getString) file, header, err := up.GetFile("file") types := up.GetString("types") if err != nil { up.Data["json"] = &JSON{Code: 101, Msg: "上传失败"} up.ServeJSON() return } defer file.Close() index := strings.Split(header.Filename, ".")[1] if index != "jpg" && index != "png" && index != "jpeg" { up.Data["json"] = &JSON{Code: 102, Msg: "文件类型错误"} up.ServeJSON() return } fileName := uuid.NewString() + "." + index if err := up.SaveToFile("file", "./static/upload/"+fileName); err != nil { up.Data["json"] = &JSON{Code: 101, Msg: "上传失败"} up.ServeJSON() return } var resource models.Resource resource.ID = uint(atoi) resource.Name = fileName resource.Url = "/image/" + fileName resource.Type = types resources := service.UpdateResource(resource) if len(resources.Url) == 0 { up.Data["json"] = &JSON{Code: 101, Msg: "修改失败"} up.ServeJSON() return } else { up.Data["json"] = &JSON{Code: 200, Msg: "修改成功", Data: resource.Url} up.ServeJSON() } } // DeleteResourceById 删除图片 func (up *UploadImageController) DeleteResourceById() { getString := up.GetString("id") atoi, _ := strconv.Atoi(getString) validate := validator.New() err2 := validate.Var(atoi, "required") if err2 != nil { up.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"} up.ServeJSON() return } id, err := unity.DeleteById(atoi, models.Resource{}) if err != nil { up.Data["json"] = &JSON{Code: 101, Msg: "删除失败"} up.ServeJSON() return } else { up.Data["json"] = &JSON{Code: 200, Msg: "删除成功", Data: id} up.ServeJSON() } } // GetResource 获取所有图片 func (up *UploadImageController) GetResource() { var page unity.PageParams err := json.Unmarshal(up.Ctx.Input.RequestBody, &page) if err != nil { up.Data["json"] = &JSON{Code: 101, Msg: "参数错误"} up.ServeJSON() return } result, total, current, err := unity.Paginate(page, models.Resource{}) if err != nil { up.Data["json"] = &JSON{Code: 101, Msg: "查询失败"} up.ServeJSON() return } else { up.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{ Current: current, Size: total, Data: result, }} up.ServeJSON() return } } func (up *UploadImageController) GetResourceById() { getString := up.GetString("id") id, _ := strconv.Atoi(getString) validate := validator.New() err2 := validate.Var(id, "required") if err2 != nil { up.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"} up.ServeJSON() return } result, err := unity.GetById(id, &models.Resource{}) if err != nil { up.Data["json"] = &JSON{Code: 101, Msg: "获取失败"} up.ServeJSON() return } else { up.Data["json"] = &JSON{Code: 200, Msg: "获取成功", Data: result} up.ServeJSON() return } }