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.

82 lines
3.0 KiB

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