gin-vue-admin 2.0代码重构
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"gin-vue-admin/global"
|
||||
"github.com/dchest/captcha"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 这里需要自行实现captcha 的gin模式
|
||||
func GinCaptchaServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
dir, file := path.Split(r.URL.Path)
|
||||
ext := path.Ext(file)
|
||||
id := file[:len(file)-len(ext)]
|
||||
if ext == "" || id == "" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
fmt.Println("reload : " + r.FormValue("reload"))
|
||||
if r.FormValue("reload") != "" {
|
||||
captcha.Reload(id)
|
||||
}
|
||||
lang := strings.ToLower(r.FormValue("lang"))
|
||||
download := path.Base(dir) == "download"
|
||||
if Serve(w, r, id, ext, lang, download, global.GVA_CONFIG.Captcha.ImgWidth, global.GVA_CONFIG.Captcha.ImgHeight) == captcha.ErrNotFound {
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func Serve(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool, width, height int) error {
|
||||
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||
w.Header().Set("Pragma", "no-cache")
|
||||
w.Header().Set("Expires", "0")
|
||||
var content bytes.Buffer
|
||||
switch ext {
|
||||
case ".png":
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
_ = captcha.WriteImage(&content, id, width, height)
|
||||
case ".wav":
|
||||
w.Header().Set("Content-Type", "audio/x-wav")
|
||||
_ = captcha.WriteAudio(&content, id, lang)
|
||||
default:
|
||||
return captcha.ErrNotFound
|
||||
}
|
||||
|
||||
if download {
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
}
|
||||
http.ServeContent(w, r, id+ext, time.Time{}, bytes.NewReader(content.Bytes()))
|
||||
return nil
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package tools
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package tools
|
||||
package utils
|
||||
|
||||
import "os"
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// 空值校验工具 仅用于检验空字符串 其余类型请勿使用
|
||||
|
||||
package tools
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package tools
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
import "reflect"
|
||||
|
||||
// 利用反射将结构体转化为map
|
||||
func StructToMap(obj interface{}) map[string]interface{} {
|
||||
obj1 := reflect.TypeOf(obj)
|
||||
obj2 := reflect.ValueOf(obj)
|
||||
|
||||
var data = make(map[string]interface{})
|
||||
for i := 0; i < obj1.NumField(); i++ {
|
||||
data[obj1.Field(i).Name] = obj2.Field(i).Interface()
|
||||
}
|
||||
return data
|
||||
}
|
||||
@@ -1,17 +1,17 @@
|
||||
package servers
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"gin-vue-admin/config"
|
||||
"gin-vue-admin/global"
|
||||
"github.com/qiniu/api.v7/auth/qbox"
|
||||
"github.com/qiniu/api.v7/storage"
|
||||
"mime/multipart"
|
||||
"time"
|
||||
)
|
||||
|
||||
var accessKey string = config.GinVueAdminconfig.Qiniu.AccessKey // 你在七牛云的accessKey 这里是我个人测试号的key 仅供测试使用 恳请大家不要乱传东西
|
||||
var secretKey string = config.GinVueAdminconfig.Qiniu.SecretKey // 你在七牛云的secretKey 这里是我个人测试号的key 仅供测试使用 恳请大家不要乱传东西
|
||||
var accessKey string = global.GVA_CONFIG.Qiniu.AccessKey // 你在七牛云的accessKey 这里是我个人测试号的key 仅供测试使用 恳请大家不要乱传东西
|
||||
var secretKey string = global.GVA_CONFIG.Qiniu.SecretKey // 你在七牛云的secretKey 这里是我个人测试号的key 仅供测试使用 恳请大家不要乱传东西
|
||||
|
||||
// 接收两个参数 一个文件流 一个 bucket 你的七牛云标准空间的名字
|
||||
func Upload(file *multipart.FileHeader, bucket string, urlPath string) (err error, path string, key string) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package tools
|
||||
package utils
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
|
||||
Reference in New Issue
Block a user