first commit
This commit is contained in:
34
internal/router/manage/common/captcha.go
Normal file
34
internal/router/manage/common/captcha.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"management/internal/config"
|
||||
captchaservice "management/internal/service/captcha"
|
||||
"management/internal/tpl"
|
||||
)
|
||||
|
||||
type CaptchaResponse struct {
|
||||
CaptchaID string `json:"captcha_id"`
|
||||
PicPath string `json:"pic_path"`
|
||||
CaptchaLength int `json:"captcha_length"`
|
||||
OpenCaptcha int `json:"open_captcha"`
|
||||
}
|
||||
|
||||
func Captcha(w http.ResponseWriter, r *http.Request) {
|
||||
keyLong := config.File.Captcha.KeyLong
|
||||
oc := config.File.Captcha.OpenCaptcha
|
||||
id, b64s, _, err := captchaservice.Generate(config.File.Captcha.ImgHeight, config.File.Captcha.ImgWidth, keyLong, 0.7, 80)
|
||||
if err != nil {
|
||||
tpl.JSON(w, tpl.Response{Success: false, Message: "获取验证码失败"})
|
||||
return
|
||||
}
|
||||
|
||||
rsp := CaptchaResponse{
|
||||
CaptchaID: id,
|
||||
PicPath: b64s,
|
||||
CaptchaLength: keyLong,
|
||||
OpenCaptcha: oc,
|
||||
}
|
||||
tpl.JSON(w, tpl.Response{Success: true, Message: "ok", Data: rsp})
|
||||
}
|
||||
97
internal/router/manage/common/upload.go
Normal file
97
internal/router/manage/common/upload.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
|
||||
"management/internal/tpl"
|
||||
|
||||
fileutil "management/internal/pkg/file"
|
||||
)
|
||||
|
||||
const maxImageSize = 100 << 20 // 100 MB
|
||||
|
||||
func UploadImg(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
_, fh, err := r.FormFile("files")
|
||||
if err != nil {
|
||||
tpl.JSON(w, tpl.Response{Success: false, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
path, err := fileutil.UploadFile(fh, fileutil.IMG)
|
||||
if err != nil {
|
||||
tpl.JSON(w, tpl.Response{Success: false, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
tpl.JSON(w, tpl.Response{Success: true, Message: "ok", Data: path})
|
||||
}
|
||||
|
||||
func UploadFile(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
|
||||
_, fh, err := r.FormFile("files")
|
||||
if err != nil {
|
||||
tpl.JSON(w, tpl.Response{Success: false, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
path, err := fileutil.UploadFile(fh, fileutil.ALL)
|
||||
if err != nil {
|
||||
tpl.JSON(w, tpl.Response{Success: false, Message: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
tpl.JSON(w, tpl.Response{Success: true, Message: "ok", Data: path})
|
||||
}
|
||||
|
||||
type UploadFileRes struct {
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
func UploadMutilFiles(w http.ResponseWriter, r *http.Request) {
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(r.Body)
|
||||
|
||||
err := r.ParseMultipartForm(int64(maxImageSize))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
files := r.MultipartForm.File["files"]
|
||||
|
||||
var res []UploadFileRes
|
||||
c := make(chan UploadFileRes, 2)
|
||||
defer close(c)
|
||||
|
||||
for i, file := range files {
|
||||
go func(item *multipart.FileHeader, key int) {
|
||||
ufr := UploadFileRes{Name: item.Filename}
|
||||
|
||||
filePath, err := fileutil.UploadFile(item, fileutil.ALL)
|
||||
if err != nil {
|
||||
tpl.JSONERR(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
ufr.Path = filePath
|
||||
c <- ufr
|
||||
}(file, i)
|
||||
}
|
||||
|
||||
for {
|
||||
v, ok := <-c
|
||||
if ok {
|
||||
res = append(res, v)
|
||||
}
|
||||
if len(files) == len(res) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
tpl.JSON(w, tpl.Response{Success: true, Message: "ok", Data: res})
|
||||
}
|
||||
Reference in New Issue
Block a user