Merge branch 'develop' of https://github.com/flipped-aurora/gin-vue-admin into gva_gormv2_dev

 Conflicts:
	web/package.json
This commit is contained in:
pixel
2021-02-14 21:41:34 +08:00
15 changed files with 403 additions and 143 deletions
+84
View File
@@ -6,6 +6,7 @@ import (
"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"
)
@@ -78,3 +79,86 @@ func GetFileList(c *gin.Context) {
},"获取成功", 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)
}
+5 -1
View File
@@ -76,4 +76,8 @@ qiniu:
use-https: false
access-key: '25j8dYBZ2wuiy0yhwShytjZDTX662b8xiFguwxzZ'
secret-key: 'pgdbqEsf7ooZh7W3xokP833h3dZ_VecFXPDeG5JY'
use-cdn-domains: false
use-cdn-domains: false
# excel configuration
excel:
dir: './resource/excel/'
+1
View File
@@ -13,4 +13,5 @@ type Server struct {
// oss
Local Local `mapstructure:"local" json:"local" yaml:"local"`
Qiniu Qiniu `mapstructure:"qiniu" json:"qiniu" yaml:"qiniu"`
Excel Excel `mapstructure:"excel" json:"excel" yaml:"excel"`
}
+5
View File
@@ -0,0 +1,5 @@
package config
type Excel struct {
Dir string `mapstructure:"dir" json:"dir" yaml:"dir"`
}
+2 -2
View File
@@ -3,6 +3,7 @@ module gin-vue-admin
go 1.14
require (
github.com/360EntSecGroup-Skylar/excelize/v2 v2.3.2
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/casbin/casbin v1.9.1
@@ -47,8 +48,7 @@ require (
github.com/tebeka/strftime v0.1.3 // indirect
github.com/unrolled/secure v1.0.7
go.uber.org/zap v1.10.0
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
golang.org/x/sys v0.0.0-20200610111108-226ff32320da // indirect
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
golang.org/x/tools v0.0.0-20200324003944-a576cf524670 // indirect
google.golang.org/protobuf v1.24.0 // indirect
gopkg.in/ini.v1 v1.55.0 // indirect
@@ -0,0 +1,8 @@
package request
import "gin-vue-admin/model"
type ExcelInfo struct {
FileName string `json:"fileName"`
InfoList []model.SysBaseMenu `json:"infoList"`
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -15,5 +15,9 @@ func InitFileUploadAndDownloadRouter(Router *gin.RouterGroup) {
FileUploadAndDownloadGroup.GET("/findFile", v1.FindFile) // 查询当前文件成功的切片
FileUploadAndDownloadGroup.POST("/breakpointContinueFinish", v1.BreakpointContinueFinish) // 查询当前文件成功的切片
FileUploadAndDownloadGroup.POST("/removeChunk", v1.RemoveChunk) // 查询当前文件成功的切片
FileUploadAndDownloadGroup.POST("/importExcel", v1.ImportExcel) // 导入Excel
FileUploadAndDownloadGroup.GET("/loadExcel", v1.LoadExcel) // 加载Excel数据
FileUploadAndDownloadGroup.POST("/exportExcel", v1.ExportExcel) // 导出Excel
FileUploadAndDownloadGroup.GET("/downloadTemplate", v1.DownloadTemplate) // 下载模板文件
}
}
+91
View File
@@ -0,0 +1,91 @@
package service
import (
"errors"
"fmt"
"gin-vue-admin/global"
"gin-vue-admin/model"
"github.com/360EntSecGroup-Skylar/excelize/v2"
"strconv"
)
func ParseInfoList2Excel(infoList []model.SysBaseMenu, filePath string) error {
excel := excelize.NewFile()
excel.SetSheetRow("Sheet1","A1",&[]string{"ID","路由Name","路由Path","是否隐藏","父节点","排序","文件名称"})
for i, menu := range infoList {
axis := fmt.Sprintf("A%d",i+2)
excel.SetSheetRow("Sheet1",axis,&[]interface{}{
menu.ID,
menu.Name,
menu.Path,
menu.Hidden,
menu.ParentId,
menu.Sort,
menu.Component,
})
}
excel.SaveAs(filePath)
return nil
}
func ParseExcel2InfoList() ([]model.SysBaseMenu, error) {
skipHeader := true
fixedHeader := []string{"ID","路由Name","路由Path","是否隐藏","父节点","排序","文件名称"}
file, err := excelize.OpenFile(global.GVA_CONFIG.Excel.Dir+"ExcelImport.xlsx")
if err != nil {
return nil, err
}
menus := make([]model.SysBaseMenu, 0)
rows, err := file.Rows("Sheet1")
if err != nil {
return nil, err
}
for rows.Next() {
row, err := rows.Columns()
if err != nil {
return nil, err
}
if skipHeader {
if compareStrSlice(row, fixedHeader) {
skipHeader = false
continue
} else {
return nil, errors.New("Excel格式错误")
}
}
if len(row) != len(fixedHeader) {
continue
}
id, _ := strconv.Atoi(row[0])
hidden, _ := strconv.ParseBool(row[3])
sort, _ := strconv.Atoi(row[5])
menu := model.SysBaseMenu{
GVA_MODEL: global.GVA_MODEL{
ID: uint(id),
},
Name: row[1],
Path: row[2],
Hidden: hidden,
ParentId: row[4],
Sort: sort,
Component: row[6],
}
menus = append(menus, menu)
}
return menus, nil
}
func compareStrSlice(a, b []string) bool {
if len(a) != len(b) {
return false
}
if (b == nil) != (a == nil) {
return false
}
for key, value := range a {
if value != b[key] {
return false
}
}
return true
}