exa_file_upload_download.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package v1
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/model"
  5. "gin-vue-admin/model/request"
  6. "gin-vue-admin/model/response"
  7. "gin-vue-admin/service"
  8. "gin-vue-admin/utils"
  9. "github.com/gin-gonic/gin"
  10. "go.uber.org/zap"
  11. )
  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 UploadFile(c *gin.Context) {
  21. var file model.ExaFileUploadAndDownload
  22. noSave := c.DefaultQuery("noSave", "0")
  23. _, header, err := c.Request.FormFile("file")
  24. if err != nil {
  25. global.GVA_LOG.Error("接收文件失败!", zap.Any("err", err))
  26. response.FailWithMessage("接收文件失败", c)
  27. return
  28. }
  29. err, file = service.UploadFile(header, noSave) // 文件上传后拿到文件路径
  30. if err != nil {
  31. global.GVA_LOG.Error("修改数据库链接失败!", zap.Any("err", err))
  32. response.FailWithMessage("修改数据库链接失败", c)
  33. return
  34. }
  35. response.OkWithDetailed(response.ExaFileResponse{File: file}, "上传成功", c)
  36. }
  37. // @Tags ExaFileUploadAndDownload
  38. // @Summary 删除文件
  39. // @Security ApiKeyAuth
  40. // @Produce application/json
  41. // @Param data body model.ExaFileUploadAndDownload true "传入文件里面id即可"
  42. // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
  43. // @Router /fileUploadAndDownload/deleteFile [post]
  44. func DeleteFile(c *gin.Context) {
  45. var file model.ExaFileUploadAndDownload
  46. _ = c.ShouldBindJSON(&file)
  47. if err := service.DeleteFile(file); err != nil {
  48. global.GVA_LOG.Error("删除失败!", zap.Any("err", 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 GetFileList(c *gin.Context) {
  63. var pageInfo request.PageInfo
  64. _ = c.ShouldBindJSON(&pageInfo)
  65. err, list, total := service.GetFileRecordInfoList(pageInfo)
  66. if err != nil {
  67. global.GVA_LOG.Error("获取失败!", zap.Any("err", 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. }
  78. // @Tags ExaFileUploadAndDownload
  79. // @Summary 导出Excel
  80. // @Security ApiKeyAuth
  81. // @accept application/json
  82. // @Produce application/octet-stream
  83. // @Param data body request.ExcelInfo true "导出Excel文件信息"
  84. // @Success 200
  85. // @Router /fileUploadAndDownload/exportExcel [post]
  86. func ExportExcel(c *gin.Context) {
  87. var excelInfo request.ExcelInfo
  88. c.ShouldBindJSON(&excelInfo)
  89. filePath := global.GVA_CONFIG.Excel.Dir+excelInfo.FileName
  90. err := service.ParseInfoList2Excel(excelInfo.InfoList, filePath)
  91. if err != nil {
  92. global.GVA_LOG.Error("转换Excel失败!", zap.Any("err", err))
  93. response.FailWithMessage("转换Excel失败", c)
  94. return
  95. }
  96. c.Writer.Header().Add("success", "true")
  97. c.File(filePath)
  98. }
  99. // @Tags ExaFileUploadAndDownload
  100. // @Summary 导入Excel文件
  101. // @Security ApiKeyAuth
  102. // @accept multipart/form-data
  103. // @Produce application/json
  104. // @Param file formData file true "导入Excel文件"
  105. // @Success 200 {string} string "{"success":true,"data":{},"msg":"导入成功"}"
  106. // @Router /fileUploadAndDownload/importExcel [post]
  107. func ImportExcel(c *gin.Context) {
  108. _, header, err := c.Request.FormFile("file")
  109. if err != nil {
  110. global.GVA_LOG.Error("接收文件失败!", zap.Any("err", err))
  111. response.FailWithMessage("接收文件失败", c)
  112. return
  113. }
  114. c.SaveUploadedFile(header, global.GVA_CONFIG.Excel.Dir+"ExcelImport.xlsx")
  115. response.OkWithMessage("导入成功", c)
  116. }
  117. // @Tags ExaFileUploadAndDownload
  118. // @Summary 加载Excel数据
  119. // @Security ApiKeyAuth
  120. // @Produce application/json
  121. // @Success 200 {string} string "{"success":true,"data":{},"msg":"加载数据成功"}"
  122. // @Router /fileUploadAndDownload/loadExcel [get]
  123. func LoadExcel(c *gin.Context) {
  124. menus, err := service.ParseExcel2InfoList()
  125. if err != nil {
  126. global.GVA_LOG.Error("加载数据失败", zap.Any("err", err))
  127. response.FailWithMessage("加载数据失败", c)
  128. return
  129. }
  130. response.OkWithDetailed(response.PageResult{
  131. List: menus,
  132. Total: int64(len(menus)),
  133. Page: 1,
  134. PageSize: 999,
  135. },"加载数据成功", c)
  136. }
  137. // @Tags ExaFileUploadAndDownload
  138. // @Summary 下载模板
  139. // @Security ApiKeyAuth
  140. // @accept multipart/form-data
  141. // @Produce application/json
  142. // @Param fileName query fileName true "模板名称"
  143. // @Success 200
  144. // @Router /fileUploadAndDownload/downloadTemplate [get]
  145. func DownloadTemplate(c *gin.Context) {
  146. fileName := c.Query("fileName")
  147. filePath := global.GVA_CONFIG.Excel.Dir+fileName
  148. ok, err := utils.PathExists(filePath)
  149. if !ok || err != nil {
  150. global.GVA_LOG.Error("文件不存在", zap.Any("err", err))
  151. response.FailWithMessage("文件不存在", c)
  152. return
  153. }
  154. c.Writer.Header().Add("success", "true")
  155. c.File(filePath)
  156. }