diff --git a/pkg/controller/userhandler.go b/pkg/controller/userhandler.go index 0721e4491a27fadd2e15980041b4c9b54e5af70c..b91f002eda363248a96045dd9a4a29a5a9137d1c 100644 --- a/pkg/controller/userhandler.go +++ b/pkg/controller/userhandler.go @@ -11,6 +11,7 @@ import ( "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" + "github.com/tealeg/xlsx" "openeluer.org/PilotGo/PilotGo/pkg/common" "openeluer.org/PilotGo/PilotGo/pkg/common/dto" "openeluer.org/PilotGo/PilotGo/pkg/common/response" @@ -186,3 +187,49 @@ func UpdateUser(c *gin.Context) { response.Fail(c, nil, "No user found!") } } + +//一键导入用户数据 +func ImportUser(c *gin.Context) { + form, _ := c.MultipartForm() + + files := form.File["upload"] + if files == nil { + response.Fail(c, nil, "Please select a file first!") + return + } + filePath := "static/" + for _, file := range files { + name := file.Filename + filename := filePath + name + + // c.SaveUploadedFile(file, filename) + xlFile, error := xlsx.OpenFile(filename) + if error != nil { + return + } + for _, sheet := range xlFile.Sheets { + for rowIndex, row := range sheet.Rows { + //跳过第一行表头信息 + if rowIndex == 0 { + continue + } + user := model.User{} + user.Username = row.Cells[0].Value + // 设置默认密码为123456 + hasedPassword, err := common.HashAndSalt("123456") + if err != nil { + response.Response(c, http.StatusInternalServerError, 500, nil, "Hased password error!") + return + } + user.Password = hasedPassword + user.Phone = row.Cells[1].Value + user.Email = row.Cells[2].Value + mysqlmanager.DB.Create(&user) + } + } + } + response.Response(c, http.StatusUnprocessableEntity, + 200, + nil, + "import success") +} diff --git a/pkg/router/router.go b/pkg/router/router.go index 3275422b3f57d44647fae32084d2463e66b3a246..bfb5e2589d05aef89d61db9e535905facbeee8ab 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -38,7 +38,7 @@ func SetupRouter() *gin.Engine { user.GET("/refresh", controller.UserRefresh) user.POST("/delete", controller.DeleteUser) user.POST("/update", controller.UpdateUser) - // user.POST("/import", controller.ImportUser) + user.POST("/import", controller.ImportUser) } machinemanager := router.Group("machinemanager") {