From 20962f00b62c9ff210b5a1c00a716c7c4ef085a3 Mon Sep 17 00:00:00 2001 From: Havoc412 <2993167370@qq.com> Date: Sun, 20 Oct 2024 11:45:26 +0800 Subject: [PATCH] add GetStringSlice --- .../core/data_transfer/data_transfer.go | 37 +++++++++++++++++++ app/service/upload_file/image_file.go | 1 + config/config.yml | 1 + routers/web.go | 2 + 4 files changed, 41 insertions(+) create mode 100644 app/service/upload_file/image_file.go diff --git a/app/http/validator/core/data_transfer/data_transfer.go b/app/http/validator/core/data_transfer/data_transfer.go index ff97a52..9f10d50 100644 --- a/app/http/validator/core/data_transfer/data_transfer.go +++ b/app/http/validator/core/data_transfer/data_transfer.go @@ -1,9 +1,11 @@ package data_transfer import ( + "catface/app/global/consts" "catface/app/global/variable" "catface/app/http/validator/core/interf" "encoding/json" + "reflect" "time" "github.com/gin-gonic/gin" @@ -36,3 +38,38 @@ func DataAddContext(validatorInterface interf.ValidatorInterface, extraAddDataPr } return nil } + +// getSlice 是一个通用的辅助函数,用于从 context 中获取切片。 +func getSlice(context *gin.Context, ValidatorPrefix string, key string, elemType reflect.Type) interface{} { + if val, ok := context.Get(ValidatorPrefix + key); ok && val != nil { + if slice, ok := val.([]interface{}); ok { + result := reflect.MakeSlice(reflect.SliceOf(elemType), 0, len(slice)) + for _, item := range slice { + if reflect.TypeOf(item) == elemType { + result = reflect.Append(result, reflect.ValueOf(item)) + } + } + return result.Interface() + } + } + return nil +} + +// GetStringSlice 从 context 中获取字符串切片。 +func GetStringSlice(context *gin.Context, key string) (ss []string) { + if val := getSlice(context, consts.ValidatorPrefix, key, reflect.TypeOf("")); val != nil { + ss = val.([]string) + } + // INFO 同时重新装载。 + context.Set(consts.ValidatorPrefix+key, ss) + return +} + +// GetIntSlice 从 context 中获取整数切片。 +func GetIntSlice(context *gin.Context, key string) (ss []int) { + if val := getSlice(context, consts.ValidatorPrefix, key, reflect.TypeOf(0)); val != nil { + ss = val.([]int) + } + context.Set(consts.ValidatorPrefix+key, ss) + return +} diff --git a/app/service/upload_file/image_file.go b/app/service/upload_file/image_file.go new file mode 100644 index 0000000..c6ff72d --- /dev/null +++ b/app/service/upload_file/image_file.go @@ -0,0 +1 @@ +package upload_file diff --git a/config/config.yml b/config/config.yml index fad66c2..23a67e8 100644 --- a/config/config.yml +++ b/config/config.yml @@ -69,6 +69,7 @@ FileUploadSetting: - "text/plain; charset=utf-8" #txt log json等文本文件 # - "video/mp4" #视频文件,例如:mp4 # - "audio/mpeg" #音频文件,例如: mp3 + AvatarWidth: 200 # casbin 权限控制api接口 Casbin: diff --git a/routers/web.go b/routers/web.go index 10e772e..c6748d5 100644 --- a/routers/web.go +++ b/routers/web.go @@ -3,10 +3,12 @@ package routers import ( "catface/app/global/consts" "catface/app/global/variable" + // "catface/app/http/controller/captcha" // 验证码组件 // "catface/app/http/middleware/authorization" "catface/app/http/middleware/cors" validatorFactory "catface/app/http/validator/core/factory" + // TODO validatorFactory "catface/app/http/validator/core/factory" "catface/app/utils/gin_release" "net/http"