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.

101 lines
3.3 KiB

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