You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.0 KiB

3 years ago
3 years ago
3 years ago
  1. package example
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/example"
  7. exampleRes "github.com/flipped-aurora/gin-vue-admin/server/model/example/response"
  8. "github.com/gin-gonic/gin"
  9. "go.uber.org/zap"
  10. )
  11. type FileUploadAndDownloadApi struct {
  12. }
  13. // @Tags ExaFileUploadAndDownload
  14. // @Summary 上传文件示例
  15. // @Security ApiKeyAuth
  16. // @accept multipart/form-data
  17. // @Produce application/json
  18. // @Param file formData file true "上传文件示例"
  19. // @Success 200 {string} string "{"success":true,"data":{},"msg":"上传成功"}"
  20. // @Router /fileUploadAndDownload/upload [post]
  21. func (u *FileUploadAndDownloadApi) UploadFile(c *gin.Context) {
  22. var file example.ExaFileUploadAndDownload
  23. noSave := c.DefaultQuery("noSave", "0")
  24. _, header, err := c.Request.FormFile("file")
  25. if err != nil {
  26. global.GVA_LOG.Error("接收文件失败!", zap.Error(err))
  27. response.FailWithMessage("接收文件失败", c)
  28. return
  29. }
  30. err, file = fileUploadAndDownloadService.UploadFile(header, noSave) // 文件上传后拿到文件路径
  31. if err != nil {
  32. global.GVA_LOG.Error("修改数据库链接失败!", zap.Error(err))
  33. response.FailWithMessage("修改数据库链接失败", c)
  34. return
  35. }
  36. response.OkWithDetailed(exampleRes.ExaFileResponse{File: file}, "上传成功", c)
  37. }
  38. // @Tags ExaFileUploadAndDownload
  39. // @Summary 删除文件
  40. // @Security ApiKeyAuth
  41. // @Produce application/json
  42. // @Param data body example.ExaFileUploadAndDownload true "传入文件里面id即可"
  43. // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
  44. // @Router /fileUploadAndDownload/deleteFile [post]
  45. func (u *FileUploadAndDownloadApi) DeleteFile(c *gin.Context) {
  46. var file example.ExaFileUploadAndDownload
  47. _ = c.ShouldBindJSON(&file)
  48. if err := fileUploadAndDownloadService.DeleteFile(file); err != nil {
  49. global.GVA_LOG.Error("删除失败!", zap.Error(err))
  50. response.FailWithMessage("删除失败", c)
  51. return
  52. }
  53. response.OkWithMessage("删除成功", c)
  54. }
  55. // @Tags ExaFileUploadAndDownload
  56. // @Summary 分页文件列表
  57. // @Security ApiKeyAuth
  58. // @accept application/json
  59. // @Produce application/json
  60. // @Param data body request.PageInfo true "页码, 每页大小"
  61. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  62. // @Router /fileUploadAndDownload/getFileList [post]
  63. func (u *FileUploadAndDownloadApi) GetFileList(c *gin.Context) {
  64. var pageInfo request.PageInfo
  65. _ = c.ShouldBindJSON(&pageInfo)
  66. err, list, total := fileUploadAndDownloadService.GetFileRecordInfoList(pageInfo)
  67. if err != nil {
  68. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  69. response.FailWithMessage("获取失败", c)
  70. } else {
  71. response.OkWithDetailed(response.PageResult{
  72. List: list,
  73. Total: total,
  74. Page: pageInfo.Page,
  75. PageSize: pageInfo.PageSize,
  76. }, "获取成功", c)
  77. }
  78. }