zhangqian
2023-08-07 4b63908ad085bc570623f7b0c0fd397b2ae7a80d
api/v1/file.go
@@ -8,11 +8,11 @@
   "aps_crm/pkg/ecode"
   "aps_crm/pkg/httpx"
   "aps_crm/service"
   "github.com/flipped-aurora/gin-vue-admin/server/utils/upload"
   "aps_crm/utils/upload"
   "github.com/gin-gonic/gin"
   "github.com/spf13/cast"
   "os"
   "path/filepath"
   "strconv"
)
type FileApi struct{}
@@ -21,7 +21,8 @@
// @Tags      附件管理
// @Summary   添加附件
// @Produce   application/json
// @Param      object   body      request.AddFile   true   "查询参数"
// @Param      object   formData   request.AddFile   true   "查询参数"
// @Param      file   formData   file   true   "上传文件"
// @Success   200      {object}   contextx.Response{}
// @Router      /api/file/add [post]
func (s *FileApi) Add(c *gin.Context) {
@@ -44,7 +45,7 @@
      return
   }
   _, filename := filepath.Split(filePath)
   _, filename := filepath.Split(header.Filename)
   fileRecord := &model.File{
      Name:          filename,
@@ -80,17 +81,11 @@
      return
   }
   id, _ := strconv.Atoi(c.Param("id"))
   idx := c.Param("id")
   id := cast.ToUint(idx)
   file, err := model.NewFileSearch().SetId(id).First()
   if err != nil {
      ctx.FailWithMsg(ecode.ParamsErr, "查找文件失败")
      return
   }
   err = os.Remove(file.FilePath)
   if err != nil {
      ctx.FailWithMsg(ecode.ParamsErr, "删除文件失败")
      return
   }
@@ -100,36 +95,42 @@
      return
   }
   ctx.Ok()
}
// Update
// @Tags      附件管理
// @Summary   更新附件
// @Produce   application/json
// @Param      object   body      request.UpdateFile   true   "查询参数"
// @Success   200      {object}   contextx.Response{}
// @Router      /api/file/update [put]
func (s *FileApi) Update(c *gin.Context) {
   var params request.UpdateFile
   ctx, ok := contextx.NewContext(c, &params)
   if !ok {
      return
   }
   if params.Id == 0 {
      ctx.Fail(ecode.ParamsErr)
   }
   params.File.Id = params.Id
   errCode := service.NewFileService().UpdateFile(&params.File)
   if errCode != ecode.OK {
      ctx.Fail(errCode)
   err = os.Remove(file.FilePath)
   if err != nil {
      ctx.FailWithMsg(ecode.ParamsErr, "删除文件失败")
      return
   }
   ctx.Ok()
}
//// Update
//// @Tags      附件管理
//// @Summary   更新附件
//// @Produce   application/json
//// @Param      object   body      request.UpdateFile   true   "查询参数"
//// @Success   200      {object}   contextx.Response{}
//// @Router      /api/file/update [put]
//func (s *FileApi) Update(c *gin.Context) {
//   var params request.UpdateFile
//   ctx, ok := contextx.NewContext(c, &params)
//   if !ok {
//      return
//   }
//   if params.Id == 0 {
//      ctx.Fail(ecode.ParamsErr)
//   }
//   params.File.ID = params.Id
//
//   errCode := service.NewFileService().UpdateFile(&params.File)
//   if errCode != ecode.OK {
//      ctx.Fail(errCode)
//      return
//   }
//
//   ctx.Ok()
//}
// List
// @Tags      附件管理
// @Summary   获取附件列表