修复发送邮件只能发一次的问题,整合代码到一个文件

This commit is contained in:
wsczx 2022-10-25 14:59:13 +08:00
parent 9fe31212e4
commit 7032ebdc85
2 changed files with 35 additions and 30 deletions

View File

@ -2,17 +2,44 @@ package admin
import ( import (
"fmt" "fmt"
"io"
"net/http"
"os"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/bjdgyc/anylink/base"
"github.com/bjdgyc/anylink/dbdata" "github.com/bjdgyc/anylink/dbdata"
"github.com/bjdgyc/anylink/pkg/utils" "github.com/bjdgyc/anylink/pkg/utils"
"github.com/spf13/cast" "github.com/spf13/cast"
"github.com/xuri/excelize/v2" "github.com/xuri/excelize/v2"
) )
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)
os.Remove(base.Cfg.FilesPath + header.Filename)
return
}
os.Remove(base.Cfg.FilesPath + header.Filename)
RespSucess(w, "批量添加成功")
}
func UploadUser(file string) error { func UploadUser(file string) error {
user := &dbdata.User{}
f, err := excelize.OpenFile(file) f, err := excelize.OpenFile(file)
if err != nil { if err != nil {
return err return err
@ -44,7 +71,7 @@ func UploadUser(file string) error {
sendmail, _ := strconv.ParseBool(row[10]) sendmail, _ := strconv.ParseBool(row[10])
// createdAt, _ := time.ParseInLocation("2006-01-02 15:04:05", row[11], time.Local) // createdAt, _ := time.ParseInLocation("2006-01-02 15:04:05", row[11], time.Local)
// updatedAt, _ := time.ParseInLocation("2006-01-02 15:04:05", row[12], time.Local) // updatedAt, _ := time.ParseInLocation("2006-01-02 15:04:05", row[12], time.Local)
user = &dbdata.User{ user := &dbdata.User{
Id: id, Id: id,
Username: row[1], Username: row[1],
Nickname: row[2], Nickname: row[2],
@ -62,10 +89,11 @@ func UploadUser(file string) error {
if err := dbdata.AddBatch(user); err != nil { if err := dbdata.AddBatch(user); err != nil {
return fmt.Errorf("请检查是否导入有重复用户") return fmt.Errorf("请检查是否导入有重复用户")
} }
}
if user.SendEmail { if user.SendEmail {
err := userAccountMail(user) if err := userAccountMail(user); err != nil {
return err return err
} }
}
}
return nil return nil
} }

View File

@ -8,7 +8,6 @@ import (
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
"os"
"strconv" "strconv"
"strings" "strings"
"text/template" "text/template"
@ -112,29 +111,7 @@ func UserSet(w http.ResponseWriter, r *http.Request) {
sessdata.CloseUserLimittimeSession() sessdata.CloseUserLimittimeSession()
RespSucess(w, nil) 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)
os.Remove(base.Cfg.FilesPath + header.Filename)
return
}
os.Remove(base.Cfg.FilesPath + header.Filename)
RespSucess(w, "批量添加成功")
}
func UserDel(w http.ResponseWriter, r *http.Request) { func UserDel(w http.ResponseWriter, r *http.Request) {
_ = r.ParseForm() _ = r.ParseForm()
idS := r.FormValue("id") idS := r.FormValue("id")