From 132ca28b3602e32b86912d5b8c8dfc081550e5e6 Mon Sep 17 00:00:00 2001 From: SugarMGP <2350745751@qq.com> Date: Tue, 17 Dec 2024 11:33:07 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E7=A9=BA=E9=97=B4=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {redis => redisHelper}/redis.go | 8 ++++---- session/session.go | 4 ++-- wechat/wechat.go | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) rename {redis => redisHelper}/redis.go (66%) diff --git a/redis/redis.go b/redisHelper/redis.go similarity index 66% rename from redis/redis.go rename to redisHelper/redis.go index 426ad42..3254b02 100644 --- a/redis/redis.go +++ b/redisHelper/redis.go @@ -1,7 +1,7 @@ -package redis +package redisHelper import ( - Redis "github.com/go-redis/redis/v8" + "github.com/go-redis/redis/v8" ) // InfoConfig Redis 配置 @@ -13,8 +13,8 @@ type InfoConfig struct { } // Init 初始化 Redis 客户端 -func Init(info *InfoConfig) *Redis.Client { - client := Redis.NewClient(&Redis.Options{ +func Init(info *InfoConfig) *redis.Client { + client := redis.NewClient(&redis.Options{ Addr: info.Host + ":" + info.Port, Password: info.Password, DB: info.DB, diff --git a/session/session.go b/session/session.go index 337c47a..d88f1ed 100644 --- a/session/session.go +++ b/session/session.go @@ -7,14 +7,14 @@ import ( "github.com/gin-contrib/sessions" sessionRedis "github.com/gin-contrib/sessions/redis" "github.com/gin-gonic/gin" - "github.com/zjutjh/WeJH-SDK/redis" + "github.com/zjutjh/WeJH-SDK/redisHelper" ) // InfoConfig 会话配置 type InfoConfig struct { Name string SecretKey string - RedisConfig *redis.InfoConfig + RedisConfig *redisHelper.InfoConfig } // Init 使用 Redis 初始化会话管理 diff --git a/wechat/wechat.go b/wechat/wechat.go index bbd474a..581ff0e 100644 --- a/wechat/wechat.go +++ b/wechat/wechat.go @@ -7,14 +7,14 @@ import ( "github.com/silenceper/wechat/v2/cache" "github.com/silenceper/wechat/v2/miniprogram" miniConfig "github.com/silenceper/wechat/v2/miniprogram/config" - "github.com/zjutjh/WeJH-SDK/redis" + "github.com/zjutjh/WeJH-SDK/redisHelper" ) // InfoConfig 微信小程序配置 type InfoConfig struct { AppId string AppSecret string - RedisConfig *redis.InfoConfig + RedisConfig *redisHelper.InfoConfig } // Init 使用 Redis 初始化微信小程序 From 772904947dcc5d081b1e5445f4fb3ac7a5d1bf15 Mon Sep 17 00:00:00 2001 From: SugarMGP <2350745751@qq.com> Date: Tue, 17 Dec 2024 11:33:26 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9EZap=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {zap => logger}/zap.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) rename {zap => logger}/zap.go (87%) diff --git a/zap/zap.go b/logger/zap.go similarity index 87% rename from zap/zap.go rename to logger/zap.go index 8eef6fa..5cf1c08 100644 --- a/zap/zap.go +++ b/logger/zap.go @@ -44,14 +44,28 @@ const ( WriterAll = "all" ) +// GetDefaultConfig 返回默认的日志配置 +func GetDefaultConfig() *InfoConfig { + return &InfoConfig{ + ConsoleLevel: "info", + DisableStacktrace: false, + LogCompress: false, + LogMaxAge: 7, + LogMaxSize: 10, + LoggerDir: "./logs", + Name: "default", + StacktraceLevel: "error", + Writer: WriterAll, + } +} + // Init 初始化 Zap 日志记录器 -// 初始化后使用 zap.L() 或 zap.S() 使用全局日志记录器 -func Init(cfg *InfoConfig) error { +func Init(cfg *InfoConfig) (*zap.Logger, error) { cfg.LoggerDir = strings.TrimRight(cfg.LoggerDir, "/ ") // 去除尾部斜杠和空格 // 创建日志目录 if err := os.MkdirAll(cfg.LoggerDir, 0750); err != nil { - return err + return nil, err } encoder := createEncoder() @@ -69,11 +83,8 @@ func Init(cfg *InfoConfig) error { }))) } - logger := zap.New(combinedCore, options...) // 创建新的 zap 日志记录器 - zap.ReplaceGlobals(logger) // 替换全局日志记录器 - - zap.L().Info("Logger initialized") - return nil + logger := zap.New(combinedCore, options...) // 创建新的日志记录器 + return logger, nil } // GetZapLevel 返回日志级别 From a8537d8b7f43fa3cf024d33c621921e7e31869d8 Mon Sep 17 00:00:00 2001 From: SugarMGP <2350745751@qq.com> Date: Tue, 17 Dec 2024 13:12:39 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E7=A9=BA=E9=97=B4=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {aes => aesHelper}/aes.go | 2 +- fetch.go | 119 -------------------------- {minio => minioHelper}/minio.go | 2 +- {minio => minioHelper}/service.go | 2 +- {session => sessionHelper}/session.go | 2 +- {wechat => wechatHelper}/wechat.go | 2 +- {logger => zapHelper}/zap.go | 0 7 files changed, 5 insertions(+), 124 deletions(-) rename {aes => aesHelper}/aes.go (99%) delete mode 100644 fetch.go rename {minio => minioHelper}/minio.go (97%) rename {minio => minioHelper}/service.go (98%) rename {session => sessionHelper}/session.go (97%) rename {wechat => wechatHelper}/wechat.go (97%) rename {logger => zapHelper}/zap.go (100%) diff --git a/aes/aes.go b/aesHelper/aes.go similarity index 99% rename from aes/aes.go rename to aesHelper/aes.go index 715778c..7adbe15 100644 --- a/aes/aes.go +++ b/aesHelper/aes.go @@ -1,4 +1,4 @@ -package aes +package aesHelper import ( "bytes" diff --git a/fetch.go b/fetch.go deleted file mode 100644 index 89bd5c3..0000000 --- a/fetch.go +++ /dev/null @@ -1,119 +0,0 @@ -//nolint:all -package sdk - -import ( - "bytes" - "crypto/tls" - "encoding/json" - "io/ioutil" - "net/http" - "net/url" - "strings" - "time" -) - -type Fetch struct { - Cookie []*http.Cookie - client *http.Client -} - -func (f *Fetch) InitUnSafe() { - f.client = &http.Client{ - CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, - Timeout: time.Second * 15, - Transport: &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - }, - } -} - -func (f *Fetch) Init() { - f.client = &http.Client{ - CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }, - Timeout: time.Second * 15, - } -} -func (f *Fetch) SkipTlsCheck() { - f.client.Transport = &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } -} - -func (f *Fetch) Get(url string) ([]byte, error) { - response, err := f.GetRaw(url) - if err != nil { - return nil, err - } - s, err := ioutil.ReadAll(response.Body) - if err != nil { - return nil, err - } - return s, nil -} - -func (f *Fetch) GetRaw(url string) (*http.Response, error) { - request, err := http.NewRequest("GET", url, nil) - if err != nil { - return nil, err - } - for _, v := range f.Cookie { - request.AddCookie(v) - } - response, err := f.client.Do(request) - if err != nil { - return nil, err - } - f.Cookie = cookieMerge(f.Cookie, response.Cookies()) - return response, err -} - -func (f *Fetch) PostFormRaw(url string, requestData url.Values) (*http.Response, error) { - request, _ := http.NewRequest("POST", url, strings.NewReader(requestData.Encode())) - request.Header.Set("Content-Type", "application/x-www-form-urlencoded") - for _, v := range f.Cookie { - request.AddCookie(v) - } - return f.client.Do(request) -} - -func (f *Fetch) PostForm(url string, requestData url.Values) ([]byte, error) { - response, err := f.PostFormRaw(url, requestData) - if err != nil { - return nil, err - } - f.Cookie = cookieMerge(f.Cookie, response.Cookies()) - return ioutil.ReadAll(response.Body) -} - -func (f *Fetch) PostJsonFormRaw(url string, requestData map[string]string) (*http.Response, error) { - bytesData, _ := json.Marshal(requestData) - request, _ := http.NewRequest("POST", url, bytes.NewReader(bytesData)) - request.Header.Set("Content-Type", "application/json") - for _, v := range f.Cookie { - request.AddCookie(v) - } - return f.client.Do(request) -} - -func (f *Fetch) PostJsonForm(url string, requestData map[string]string) ([]byte, error) { - response, err := f.PostJsonFormRaw(url, requestData) - if err != nil { - return nil, err - } - f.Cookie = cookieMerge(f.Cookie, response.Cookies()) - return ioutil.ReadAll(response.Body) -} - -func cookieMerge(cookieA []*http.Cookie, cookieB []*http.Cookie) []*http.Cookie { - - for _, v := range cookieB { - for k, v2 := range cookieA { - if v.Name == v2.Name { - cookieA = append(cookieA[:k], cookieA[k+1:]...) - break - } - } - } - cookieA = append(cookieA, cookieB...) - return cookieA -} diff --git a/minio/minio.go b/minioHelper/minio.go similarity index 97% rename from minio/minio.go rename to minioHelper/minio.go index 8cf184a..dba2fab 100644 --- a/minio/minio.go +++ b/minioHelper/minio.go @@ -1,4 +1,4 @@ -package minio +package minioHelper import ( "fmt" diff --git a/minio/service.go b/minioHelper/service.go similarity index 98% rename from minio/service.go rename to minioHelper/service.go index 038cc5d..b209a00 100644 --- a/minio/service.go +++ b/minioHelper/service.go @@ -1,4 +1,4 @@ -package minio +package minioHelper import ( "context" diff --git a/session/session.go b/sessionHelper/session.go similarity index 97% rename from session/session.go rename to sessionHelper/session.go index d88f1ed..bf87653 100644 --- a/session/session.go +++ b/sessionHelper/session.go @@ -1,4 +1,4 @@ -package session +package sessionHelper import ( "fmt" diff --git a/wechat/wechat.go b/wechatHelper/wechat.go similarity index 97% rename from wechat/wechat.go rename to wechatHelper/wechat.go index 581ff0e..c01928d 100644 --- a/wechat/wechat.go +++ b/wechatHelper/wechat.go @@ -1,4 +1,4 @@ -package wechat +package wechatHelper import ( "context" diff --git a/logger/zap.go b/zapHelper/zap.go similarity index 100% rename from logger/zap.go rename to zapHelper/zap.go