123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- package v1
- import (
- "gin-vue-admin/global"
- "gin-vue-admin/model"
- "gin-vue-admin/model/request"
- "gin-vue-admin/model/response"
- "gin-vue-admin/service"
- "gin-vue-admin/utils"
- "github.com/gin-gonic/gin"
- "go.uber.org/zap"
- )
- // @Tags ExaFileUploadAndDownload
- // @Summary 上传文件示例
- // @Security ApiKeyAuth
- // @accept multipart/form-data
- // @Produce application/json
- // @Param file formData file true "上传文件示例"
- // @Success 200 {string} string "{"success":true,"data":{},"msg":"上传成功"}"
- // @Router /fileUploadAndDownload/upload [post]
- func UploadFile(c *gin.Context) {
- var file model.ExaFileUploadAndDownload
- noSave := c.DefaultQuery("noSave", "0")
- _, header, err := c.Request.FormFile("file")
- if err != nil {
- global.GVA_LOG.Error("接收文件失败!", zap.Any("err", err))
- response.FailWithMessage("接收文件失败", c)
- return
- }
- err, file = service.UploadFile(header, noSave) // 文件上传后拿到文件路径
- if err != nil {
- global.GVA_LOG.Error("修改数据库链接失败!", zap.Any("err", err))
- response.FailWithMessage("修改数据库链接失败", c)
- return
- }
- response.OkWithDetailed(response.ExaFileResponse{File: file}, "上传成功", c)
- }
- // @Tags ExaFileUploadAndDownload
- // @Summary 删除文件
- // @Security ApiKeyAuth
- // @Produce application/json
- // @Param data body model.ExaFileUploadAndDownload true "传入文件里面id即可"
- // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
- // @Router /fileUploadAndDownload/deleteFile [post]
- func DeleteFile(c *gin.Context) {
- var file model.ExaFileUploadAndDownload
- _ = c.ShouldBindJSON(&file)
- if err := service.DeleteFile(file); err != nil {
- global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
- response.FailWithMessage("删除失败", c)
- return
- }
- response.OkWithMessage("删除成功", c)
- }
- // @Tags ExaFileUploadAndDownload
- // @Summary 分页文件列表
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Param data body request.PageInfo true "页码, 每页大小"
- // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
- // @Router /fileUploadAndDownload/getFileList [post]
- func GetFileList(c *gin.Context) {
- var pageInfo request.PageInfo
- _ = c.ShouldBindJSON(&pageInfo)
- err, list, total := service.GetFileRecordInfoList(pageInfo)
- if err != nil {
- global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
- response.FailWithMessage("获取失败", c)
- } else {
- response.OkWithDetailed(response.PageResult{
- List: list,
- Total: total,
- Page: pageInfo.Page,
- PageSize: pageInfo.PageSize,
- },"获取成功", c)
- }
- }
- // @Tags ExaFileUploadAndDownload
- // @Summary 导出Excel
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/octet-stream
- // @Param data body request.ExcelInfo true "导出Excel文件信息"
- // @Success 200
- // @Router /fileUploadAndDownload/exportExcel [post]
- func ExportExcel(c *gin.Context) {
- var excelInfo request.ExcelInfo
- c.ShouldBindJSON(&excelInfo)
- filePath := global.GVA_CONFIG.Excel.Dir+excelInfo.FileName
- err := service.ParseInfoList2Excel(excelInfo.InfoList, filePath)
- if err != nil {
- global.GVA_LOG.Error("转换Excel失败!", zap.Any("err", err))
- response.FailWithMessage("转换Excel失败", c)
- return
- }
- c.Writer.Header().Add("success", "true")
- c.File(filePath)
- }
- // @Tags ExaFileUploadAndDownload
- // @Summary 导入Excel文件
- // @Security ApiKeyAuth
- // @accept multipart/form-data
- // @Produce application/json
- // @Param file formData file true "导入Excel文件"
- // @Success 200 {string} string "{"success":true,"data":{},"msg":"导入成功"}"
- // @Router /fileUploadAndDownload/importExcel [post]
- func ImportExcel(c *gin.Context) {
- _, header, err := c.Request.FormFile("file")
- if err != nil {
- global.GVA_LOG.Error("接收文件失败!", zap.Any("err", err))
- response.FailWithMessage("接收文件失败", c)
- return
- }
- c.SaveUploadedFile(header, global.GVA_CONFIG.Excel.Dir+"ExcelImport.xlsx")
- response.OkWithMessage("导入成功", c)
- }
- // @Tags ExaFileUploadAndDownload
- // @Summary 加载Excel数据
- // @Security ApiKeyAuth
- // @Produce application/json
- // @Success 200 {string} string "{"success":true,"data":{},"msg":"加载数据成功"}"
- // @Router /fileUploadAndDownload/loadExcel [get]
- func LoadExcel(c *gin.Context) {
- menus, err := service.ParseExcel2InfoList()
- if err != nil {
- global.GVA_LOG.Error("加载数据失败", zap.Any("err", err))
- response.FailWithMessage("加载数据失败", c)
- return
- }
- response.OkWithDetailed(response.PageResult{
- List: menus,
- Total: int64(len(menus)),
- Page: 1,
- PageSize: 999,
- },"加载数据成功", c)
- }
- // @Tags ExaFileUploadAndDownload
- // @Summary 下载模板
- // @Security ApiKeyAuth
- // @accept multipart/form-data
- // @Produce application/json
- // @Param fileName query fileName true "模板名称"
- // @Success 200
- // @Router /fileUploadAndDownload/downloadTemplate [get]
- func DownloadTemplate(c *gin.Context) {
- fileName := c.Query("fileName")
- filePath := global.GVA_CONFIG.Excel.Dir+fileName
- ok, err := utils.PathExists(filePath)
- if !ok || err != nil {
- global.GVA_LOG.Error("文件不存在", zap.Any("err", err))
- response.FailWithMessage("文件不存在", c)
- return
- }
- c.Writer.Header().Add("success", "true")
- c.File(filePath)
- }
|