增加通过excel批量添加用户的功能,支持导入后自动发邮件

This commit is contained in:
wsczx
2022-10-22 20:36:10 +08:00
parent c6ef0a28b4
commit a77fe77e79
12 changed files with 168 additions and 22 deletions

View File

@@ -2,7 +2,7 @@ package admin
import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"strconv"
@@ -79,7 +79,7 @@ func GroupDetail(w http.ResponseWriter, r *http.Request) {
}
func GroupSet(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
RespError(w, RespInternalErr, err)
return

View File

@@ -2,7 +2,7 @@ package admin
import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"strconv"
@@ -59,7 +59,7 @@ func UserIpMapDetail(w http.ResponseWriter, r *http.Request) {
func UserIpMapSet(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
RespError(w, RespInternalErr, err)
return

View File

@@ -3,7 +3,7 @@ package admin
import (
"encoding/json"
"errors"
"io/ioutil"
"io"
"net/http"
"regexp"
@@ -20,7 +20,7 @@ func setOtherGet(data interface{}, w http.ResponseWriter) {
}
func setOtherEdit(data interface{}, w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
RespError(w, RespInternalErr, err)
return
@@ -73,7 +73,7 @@ func SetOtherAuditLog(w http.ResponseWriter, r *http.Request) {
}
func SetOtherAuditLogEdit(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
RespError(w, RespInternalErr, err)
return

View File

@@ -2,7 +2,7 @@ package admin
import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
"strconv"
@@ -57,7 +57,7 @@ func PolicyDetail(w http.ResponseWriter, r *http.Request) {
}
func PolicySet(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
RespError(w, RespInternalErr, err)
return

View File

@@ -5,9 +5,10 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"text/template"
@@ -80,7 +81,7 @@ func UserDetail(w http.ResponseWriter, r *http.Request) {
func UserSet(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
RespError(w, RespInternalErr, err)
return
@@ -111,7 +112,28 @@ func UserSet(w http.ResponseWriter, r *http.Request) {
sessdata.CloseUserLimittimeSession()
RespSucess(w, nil)
}
func UserUpload(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(8 << 20)
file, header, err := r.FormFile("file")
if err != nil || !strings.Contains(header.Filename, ".xlsx") || !strings.Contains(header.Filename, ".xls") {
RespError(w, RespInternalErr, "文件解析失败:仅支持xlsx或xls文件")
return
}
defer file.Close()
newFile, err := os.Create(base.Cfg.FilesPath + header.Filename)
if err != nil {
RespError(w, RespInternalErr, "创建文件失败:", err)
return
}
defer newFile.Close()
io.Copy(newFile, file)
if err = UploadUser(newFile.Name()); err != nil {
RespError(w, RespInternalErr, err)
return
}
os.Remove(base.Cfg.FilesPath + header.Filename)
RespSucess(w, "批量添加成功")
}
func UserDel(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm()
idS := r.FormValue("id")

View File

@@ -2,7 +2,7 @@ package admin
import (
"encoding/json"
"io/ioutil"
"io"
"net/http/httptest"
"testing"
@@ -15,7 +15,7 @@ func TestRespSucess(t *testing.T) {
RespSucess(w, "data")
// fmt.Println(w)
assert.Equal(w.Code, 200)
body, _ := ioutil.ReadAll(w.Body)
body, _ := io.ReadAll(w.Body)
res := Resp{}
err := json.Unmarshal(body, &res)
assert.Nil(err)
@@ -30,7 +30,7 @@ func TestRespError(t *testing.T) {
RespError(w, 10, "err-msg")
// fmt.Println(w)
assert.Equal(w.Code, 200)
body, _ := ioutil.ReadAll(w.Body)
body, _ := io.ReadAll(w.Body)
res := Resp{}
err := json.Unmarshal(body, &res)
assert.Nil(err)

View File

@@ -49,6 +49,7 @@ func StartAdmin() {
r.HandleFunc("/user/list", UserList)
r.HandleFunc("/user/detail", UserDetail)
r.HandleFunc("/user/set", UserSet)
r.HandleFunc("/user/uploaduser", UserUpload).Methods(http.MethodPost)
r.HandleFunc("/user/del", UserDel)
r.HandleFunc("/user/online", UserOnline)
r.HandleFunc("/user/offline", UserOffline)

70
server/admin/uploaduser.go Executable file
View File

@@ -0,0 +1,70 @@
package admin
import (
"fmt"
"strconv"
"time"
"github.com/bjdgyc/anylink/dbdata"
"github.com/bjdgyc/anylink/pkg/utils"
"github.com/xuri/excelize/v2"
)
func UploadUser(file string) error {
user := &dbdata.User{}
f, err := excelize.OpenFile(file)
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
return
}
}()
rows, err := f.GetRows("Sheet1")
if err != nil {
return err
}
if rows[0][0] != "id" || rows[0][1] != "username" || rows[0][2] != "nickname" || rows[0][3] != "email" || rows[0][4] != "pin_code" || rows[0][5] != "limittime" || rows[0][6] != "otp_secret" || rows[0][7] != "disable_otp" || rows[0][8] != "groups" || rows[0][9] != "status" || rows[0][10] != "send_email" {
return fmt.Errorf("批量添加失败,表格格式不正确")
}
for index, row := range rows[:] {
if index == 0 {
continue
}
id, _ := strconv.Atoi(row[0])
if len(row[4]) < 6 {
row[4] = utils.RandomRunes(8)
}
limittime, _ := time.Parse("2006-01-02 15:04:05", row[5])
disableOtp, _ := strconv.ParseBool(row[7])
group := []string{row[8]}
status, _ := strconv.Atoi(row[9])
sendmail, _ := strconv.ParseBool(row[10])
// createdAt, _ := time.Parse("2006-01-02 03:04:05", row[11])
// updatedAt, _ := time.Parse("2006-01-02 03:04:05", row[12])
user = &dbdata.User{
Id: id,
Username: row[1],
Nickname: row[2],
Email: row[3],
PinCode: row[4],
LimitTime: &limittime,
OtpSecret: row[6],
DisableOtp: disableOtp,
Groups: group,
Status: int8(status),
SendEmail: sendmail,
// CreatedAt: createdAt,
// UpdatedAt: updatedAt,
}
if err := dbdata.AddBatch(user); err != nil {
return fmt.Errorf("请检查是否导入有重复用户")
}
}
if user.SendEmail {
err := userAccountMail(user)
return err
}
return nil
}