From 76a794502ff045a6e4b15128e66f71a8cf9126f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=AA=E5=85=88=E6=A3=AE?= Date: Fri, 18 Sep 2020 17:38:14 +0800 Subject: [PATCH 1/2] =?UTF-8?q?cors.go:=20fix=E9=83=A8=E5=88=86=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E4=B8=8D=E6=94=AF=E6=8C=81*=E7=9A=84?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/middleware/cors.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/middleware/cors.go b/server/middleware/cors.go index e1e66c44..91a5ef21 100644 --- a/server/middleware/cors.go +++ b/server/middleware/cors.go @@ -9,7 +9,8 @@ import ( func Cors() gin.HandlerFunc { return func(c *gin.Context) { method := c.Request.Method - c.Header("Access-Control-Allow-Origin", "*") + origin := c.Request.Header.Get("Origin") + c.Header("Access-Control-Allow-Origin", origin) c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token,X-Token,X-User-Id\"") c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS,DELETE,PUT") c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type") From 49b9e87c20728277c0d1dd3d8376d0b9b32fff1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=AA=E5=85=88=E6=A3=AE?= Date: Fri, 18 Sep 2020 18:15:27 +0800 Subject: [PATCH 2/2] global/response/response.go: add download xlsx method --- server/global/response/response.go | 16 ++++++++++++++++ server/go.mod | 1 + 2 files changed, 17 insertions(+) diff --git a/server/global/response/response.go b/server/global/response/response.go index 35bc3fa2..e3eef036 100644 --- a/server/global/response/response.go +++ b/server/global/response/response.go @@ -1,8 +1,10 @@ package response import ( + "github.com/360EntSecGroup-Skylar/excelize" "github.com/gin-gonic/gin" "net/http" + "strconv" ) type Response struct { @@ -41,6 +43,20 @@ func OkDetailed(data interface{}, message string, c *gin.Context) { Result(SUCCESS, data, message, c) } +func OkWithXlsx(list []interface{}, title []string, fileName string, c *gin.Context) { + file := excelize.NewFile() + file.SetSheetRow("Sheet1", "A1", &title) + for i, v := range list { + // 第一行被title占用 + lint := strconv.Itoa(i + 2) + file.SetSheetRow("Sheet1", "A"+lint, v) + } + c.Header("Content-Type", "application/octet-stream") + c.Header("Content-Disposition", "attachment; filename="+fileName) + c.Header("Content-Transfer-Encoding", "binary") + _ = file.Write(c.Writer) +} + func Fail(c *gin.Context) { Result(ERROR, map[string]interface{}{}, "操作失败", c) } diff --git a/server/go.mod b/server/go.mod index 9a1da967..aefc8e36 100644 --- a/server/go.mod +++ b/server/go.mod @@ -3,6 +3,7 @@ module gin-vue-admin go 1.12 require ( + github.com/360EntSecGroup-Skylar/excelize v1.4.1 // indirect github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 github.com/casbin/casbin v1.9.1 github.com/casbin/casbin/v2 v2.11.0