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 UploadFileController struct { beego.Controller } // Post 上传图片 func (up *UploadFileController) 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] fileName := uuid.NewString() + "." + index if err := up.SaveToFile("file", "./static/file/"+fileName); err != nil { up.Data["json"] = &JSON{Code: 101, Msg: "上传失败"} up.ServeJSON() return } var resourse models.Resource resourse.Name = fileName resourse.Url = "/file/" + 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 *UploadFileController) 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] 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 = "/file/" + 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 *UploadFileController) 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 *UploadFileController) 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.FileResource{}) 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 *UploadFileController) 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 } }