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.

100 lines
3.4 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/response"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/example"
  6. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  7. "github.com/gin-gonic/gin"
  8. "go.uber.org/zap"
  9. )
  10. type ExcelApi struct{}
  11. // /excel/importExcel 接口,与upload接口作用类似,只是把文件存到resource/excel目录下,用于导入Excel时存放Excel文件(ExcelImport.xlsx)
  12. // /excel/loadExcel接口,用于读取resource/excel目录下的文件((ExcelImport.xlsx)并加载为[]model.SysBaseMenu类型的示例数据
  13. // /excel/exportExcel 接口,用于读取前端传来的tableData,生成Excel文件并返回
  14. // /excel/downloadTemplate 接口,用于下载resource/excel目录下的 ExcelTemplate.xlsx 文件,作为导入的模板
  15. // @Tags excel
  16. // @Summary 导出Excel
  17. // @Security ApiKeyAuth
  18. // @accept application/json
  19. // @Produce application/octet-stream
  20. // @Param data body example.ExcelInfo true "导出Excel文件信息"
  21. // @Success 200
  22. // @Router /excel/exportExcel [post]
  23. func (e *ExcelApi) ExportExcel(c *gin.Context) {
  24. var excelInfo example.ExcelInfo
  25. _ = c.ShouldBindJSON(&excelInfo)
  26. filePath := global.GVA_CONFIG.Excel.Dir + excelInfo.FileName
  27. err := excelService.ParseInfoList2Excel(excelInfo.InfoList, filePath)
  28. if err != nil {
  29. global.GVA_LOG.Error("转换Excel失败!", zap.Error(err))
  30. response.FailWithMessage("转换Excel失败", c)
  31. return
  32. }
  33. c.Writer.Header().Add("success", "true")
  34. c.File(filePath)
  35. }
  36. // @Tags excel
  37. // @Summary 导入Excel文件
  38. // @Security ApiKeyAuth
  39. // @accept multipart/form-data
  40. // @Produce application/json
  41. // @Param file formData file true "导入Excel文件"
  42. // @Success 200 {string} string "{"success":true,"data":{},"msg":"导入成功"}"
  43. // @Router /excel/importExcel [post]
  44. func (e *ExcelApi) ImportExcel(c *gin.Context) {
  45. _, header, err := c.Request.FormFile("file")
  46. if err != nil {
  47. global.GVA_LOG.Error("接收文件失败!", zap.Error(err))
  48. response.FailWithMessage("接收文件失败", c)
  49. return
  50. }
  51. _ = c.SaveUploadedFile(header, global.GVA_CONFIG.Excel.Dir+"ExcelImport.xlsx")
  52. response.OkWithMessage("导入成功", c)
  53. }
  54. // @Tags excel
  55. // @Summary 加载Excel数据
  56. // @Security ApiKeyAuth
  57. // @Produce application/json
  58. // @Success 200 {string} string "{"success":true,"data":{},"msg":"加载数据成功"}"
  59. // @Router /excel/loadExcel [get]
  60. func (e *ExcelApi) LoadExcel(c *gin.Context) {
  61. menus, err := excelService.ParseExcel2InfoList()
  62. if err != nil {
  63. global.GVA_LOG.Error("加载数据失败!", zap.Error(err))
  64. response.FailWithMessage("加载数据失败", c)
  65. return
  66. }
  67. response.OkWithDetailed(response.PageResult{
  68. List: menus,
  69. Total: int64(len(menus)),
  70. Page: 1,
  71. PageSize: 999,
  72. }, "加载数据成功", c)
  73. }
  74. // @Tags excel
  75. // @Summary 下载模板
  76. // @Security ApiKeyAuth
  77. // @accept multipart/form-data
  78. // @Produce application/json
  79. // @Param fileName query string true "模板名称"
  80. // @Success 200
  81. // @Router /excel/downloadTemplate [get]
  82. func (e *ExcelApi) DownloadTemplate(c *gin.Context) {
  83. fileName := c.Query("fileName")
  84. filePath := global.GVA_CONFIG.Excel.Dir + fileName
  85. ok, err := utils.PathExists(filePath)
  86. if !ok || err != nil {
  87. global.GVA_LOG.Error("文件不存在!", zap.Error(err))
  88. response.FailWithMessage("文件不存在", c)
  89. return
  90. }
  91. c.Writer.Header().Add("success", "true")
  92. c.File(filePath)
  93. }