优化HTTP请求处理和模块租户检查逻辑:支持GET请求参数转换,简化日志消费者配置,改进RPC调用方式

This commit is contained in:
2026-03-02 14:22:08 +08:00
committed by 张斌
parent dd2cba70f6
commit 62dab53108
4 changed files with 50 additions and 13 deletions

View File

@@ -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...)

View File

@@ -17,8 +17,7 @@ const (
// 消费者配置(从 Redis Stream 消费请求)
const StreamKey = "log:%s" // 请求 Stream 键名与发消息的key一致
const GroupName = "log:consumer:group" // 消费者
const ConsumerName = "message-consumer-1" // 消费者名称(唯一标识)
const ConsumerName = "log-consumer" // 消费者名称(唯一标识)
const BatchSize = 1 // 批处理大小每次读取1条
const AutoAck = true // ACK是否自动确认true自动确认false不确认

View File

@@ -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"])

View File

@@ -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
}