From 62dab53108cf802130e83f78247a535a1bc3fde1 Mon Sep 17 00:00:00 2001 From: qhd <1766646056@qq.com> Date: Mon, 2 Mar 2026 14:22:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96HTTP=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=92=8C=E6=A8=A1=E5=9D=97=E7=A7=9F=E6=88=B7?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91=EF=BC=9A=E6=94=AF=E6=8C=81?= =?UTF-8?q?GET=E8=AF=B7=E6=B1=82=E5=8F=82=E6=95=B0=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=EF=BC=8C=E7=AE=80=E5=8C=96=E6=97=A5=E5=BF=97=E6=B6=88=E8=B4=B9?= =?UTF-8?q?=E8=80=85=E9=85=8D=E7=BD=AE=EF=BC=8C=E6=94=B9=E8=BF=9BRPC?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http/http.go | 13 ++++++++++- log/consts/log_const.go | 9 ++++---- log/service/log_service.go | 3 ++- middleware/module_tenant_check.go | 38 ++++++++++++++++++++++++++----- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/http/http.go b/http/http.go index ffcb1fe..e5ba5cf 100644 --- a/http/http.go +++ b/http/http.go @@ -83,7 +83,18 @@ func doRequest(ctx context.Context, method string, url string, headers map[strin // 修复:避免data...展开导致的双重包装问题 // 当只有一个元素时,直接传递该元素,避免被包装成数组 var response *gclient.Response - if len(data) == 1 { + // 对于GET请求,将参数转换为map + if method == http.MethodGet && len(data) > 0 && len(data)%2 == 0 { + // 构建query参数map + queryParams := make(map[string]string) + for i := 0; i < len(data); i += 2 { + if key, ok := data[i].(string); ok && i+1 < len(data) { + queryParams[key] = gconv.String(data[i+1]) + } + } + g.Log().Infof(ctx, "[HTTP] GET请求构建的query参数: %+v", queryParams) + response, err = client.DoRequest(ctx, method, url, queryParams) + } else if len(data) == 1 { response, err = client.DoRequest(ctx, method, url, data[0]) } else { response, err = client.DoRequest(ctx, method, url, data...) diff --git a/log/consts/log_const.go b/log/consts/log_const.go index 8e2d0cb..af621b1 100644 --- a/log/consts/log_const.go +++ b/log/consts/log_const.go @@ -16,10 +16,9 @@ const ( ) // 消费者配置(从 Redis Stream 消费请求) -const StreamKey = "log:%s" // 请求 Stream 键名(与发消息的key一致) -const GroupName = "log:consumer:group" // 消费者组名 -const ConsumerName = "message-consumer-1" // 消费者名称(唯一标识) -const BatchSize = 1 // 批处理大小(每次读取1条) -const AutoAck = true // ACK是否自动确认(true自动确认,false不确认) +const StreamKey = "log:%s" // 请求 Stream 键名(与发消息的key一致) +const ConsumerName = "log-consumer" // 消费者名称(唯一标识) +const BatchSize = 1 // 批处理大小(每次读取1条) +const AutoAck = true // ACK是否自动确认(true自动确认,false不确认) const LogSubject = "log:subject" diff --git a/log/service/log_service.go b/log/service/log_service.go index 3e15c75..f790378 100644 --- a/log/service/log_service.go +++ b/log/service/log_service.go @@ -15,7 +15,8 @@ type operationLog struct{} // OperationLog 操作日志服务 var OperationLog = &operationLog{} -func (s *operationLog) AddOperationLog(ctx context.Context, msg map[string]interface{}) error { +func (s *operationLog) AddOperationLog(ctx context.Context, msgData any) error { + msg := gconv.MapStrStr(msgData) serviceName := gconv.String(msg["service_name"]) collection := gconv.String(msg["collection"]) collectionId := gconv.Strings(msg["collection_id"]) diff --git a/middleware/module_tenant_check.go b/middleware/module_tenant_check.go index a88a7cb..7c1b686 100644 --- a/middleware/module_tenant_check.go +++ b/middleware/module_tenant_check.go @@ -5,8 +5,7 @@ import ( "encoding/json" "fmt" "gitea.com/red-future/common/beans" - "gitea.com/red-future/common/message" - "gitea.com/red-future/common/redis" + commonHttp "gitea.com/red-future/common/http" "gitea.com/red-future/common/utils" "github.com/gogf/gf/v2/database/gredis" "github.com/gogf/gf/v2/frame/g" @@ -18,9 +17,17 @@ import ( ) func ModuleTenantCheck(r *ghttp.Request) { + //将 http.Header 转换为 map[string]string + headers := make(map[string]string) + for k, v := range r.Request.Header { + if len(v) > 0 { + headers[k] = v[0] + } + } + // 检查是否是超级管理员 - isSuperAdmin := false - if err := message.CallRPC(r.Context(), "userService.IsSuperAdmin", nil, &isSuperAdmin); err != nil { + isSuperAdmin, err := IsSuperAdmin(r.Context(), headers) + if err != nil { SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err) } // 如果是超级管理员,则不进行模块租户检查 @@ -33,7 +40,7 @@ func ModuleTenantCheck(r *ghttp.Request) { SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err) } exit := gconv.Int64(time.Minute * 1) - getEX, err := redis.GetRedisClientTest("test").GetEX(r.Context(), fmt.Sprintf("module_tenant:tenantId-%v", getUserInfo.TenantId), gredis.GetEXOption{ + getEX, err := g.Redis("test").GetEX(r.Context(), fmt.Sprintf("module_tenant:tenantId-%v", getUserInfo.TenantId), gredis.GetEXOption{ TTLOption: gredis.TTLOption{ EX: &exit, }, @@ -68,7 +75,7 @@ func ModuleTenantCheck(r *ghttp.Request) { ModuleKey: moduleKey, TenantId: gconv.Uint64(getUserInfo.TenantId), } - err = message.CallRPC(r.Context(), "moduleService.Check", &checkReq, checkRes) + checkRes, err = Check(r.Context(), headers, checkReq) if err != nil { SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err) } @@ -95,3 +102,22 @@ func SetResponseInfo(ctx context.Context, r *ghttp.Request, code int, message an }) r.Exit() } + +// Check 调用admin-go服务检查模块开通状态 +func Check(ctx context.Context, headerMap map[string]string, req beans.ModuleTenantCheckReq) (res *beans.ModuleTenantCheckRes, err error) { + if err = commonHttp.Get(ctx, "admin-go/api/v1/system/moduleTenant/check", headerMap, &res, + "moduleKey", req.ModuleKey, + "tenantId", req.TenantId, + ); err != nil { + return + } + return +} + +// IsSuperAdmin 调用admin-go服务检查是否是超级管理员 +func IsSuperAdmin(ctx context.Context, headerMap map[string]string) (res bool, err error) { + if err = commonHttp.Get(ctx, "admin-go/api/v1/system/user/checkIsSuperAdmin", headerMap, &res); err != nil { + return + } + return +}