123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- 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.PaginateResource(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
- }
- }
|