优化HTTP请求处理和模块租户检查逻辑:支持GET请求参数转换,简化日志消费者配置,改进RPC调用方式
This commit is contained in:
13
http/http.go
13
http/http.go
@@ -83,7 +83,18 @@ func doRequest(ctx context.Context, method string, url string, headers map[strin
|
|||||||
// 修复:避免data...展开导致的双重包装问题
|
// 修复:避免data...展开导致的双重包装问题
|
||||||
// 当只有一个元素时,直接传递该元素,避免被包装成数组
|
// 当只有一个元素时,直接传递该元素,避免被包装成数组
|
||||||
var response *gclient.Response
|
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])
|
response, err = client.DoRequest(ctx, method, url, data[0])
|
||||||
} else {
|
} else {
|
||||||
response, err = client.DoRequest(ctx, method, url, data...)
|
response, err = client.DoRequest(ctx, method, url, data...)
|
||||||
|
|||||||
@@ -16,10 +16,9 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// 消费者配置(从 Redis Stream 消费请求)
|
// 消费者配置(从 Redis Stream 消费请求)
|
||||||
const StreamKey = "log:%s" // 请求 Stream 键名(与发消息的key一致)
|
const StreamKey = "log:%s" // 请求 Stream 键名(与发消息的key一致)
|
||||||
const GroupName = "log:consumer:group" // 消费者组名
|
const ConsumerName = "log-consumer" // 消费者名称(唯一标识)
|
||||||
const ConsumerName = "message-consumer-1" // 消费者名称(唯一标识)
|
const BatchSize = 1 // 批处理大小(每次读取1条)
|
||||||
const BatchSize = 1 // 批处理大小(每次读取1条)
|
const AutoAck = true // ACK是否自动确认(true自动确认,false不确认)
|
||||||
const AutoAck = true // ACK是否自动确认(true自动确认,false不确认)
|
|
||||||
|
|
||||||
const LogSubject = "log:subject"
|
const LogSubject = "log:subject"
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ type operationLog struct{}
|
|||||||
// OperationLog 操作日志服务
|
// OperationLog 操作日志服务
|
||||||
var OperationLog = &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"])
|
serviceName := gconv.String(msg["service_name"])
|
||||||
collection := gconv.String(msg["collection"])
|
collection := gconv.String(msg["collection"])
|
||||||
collectionId := gconv.Strings(msg["collection_id"])
|
collectionId := gconv.Strings(msg["collection_id"])
|
||||||
|
|||||||
@@ -5,8 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gitea.com/red-future/common/beans"
|
"gitea.com/red-future/common/beans"
|
||||||
"gitea.com/red-future/common/message"
|
commonHttp "gitea.com/red-future/common/http"
|
||||||
"gitea.com/red-future/common/redis"
|
|
||||||
"gitea.com/red-future/common/utils"
|
"gitea.com/red-future/common/utils"
|
||||||
"github.com/gogf/gf/v2/database/gredis"
|
"github.com/gogf/gf/v2/database/gredis"
|
||||||
"github.com/gogf/gf/v2/frame/g"
|
"github.com/gogf/gf/v2/frame/g"
|
||||||
@@ -18,9 +17,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func ModuleTenantCheck(r *ghttp.Request) {
|
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
|
isSuperAdmin, err := IsSuperAdmin(r.Context(), headers)
|
||||||
if err := message.CallRPC(r.Context(), "userService.IsSuperAdmin", nil, &isSuperAdmin); err != nil {
|
if err != nil {
|
||||||
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
||||||
}
|
}
|
||||||
// 如果是超级管理员,则不进行模块租户检查
|
// 如果是超级管理员,则不进行模块租户检查
|
||||||
@@ -33,7 +40,7 @@ func ModuleTenantCheck(r *ghttp.Request) {
|
|||||||
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
||||||
}
|
}
|
||||||
exit := gconv.Int64(time.Minute * 1)
|
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{
|
TTLOption: gredis.TTLOption{
|
||||||
EX: &exit,
|
EX: &exit,
|
||||||
},
|
},
|
||||||
@@ -68,7 +75,7 @@ func ModuleTenantCheck(r *ghttp.Request) {
|
|||||||
ModuleKey: moduleKey,
|
ModuleKey: moduleKey,
|
||||||
TenantId: gconv.Uint64(getUserInfo.TenantId),
|
TenantId: gconv.Uint64(getUserInfo.TenantId),
|
||||||
}
|
}
|
||||||
err = message.CallRPC(r.Context(), "moduleService.Check", &checkReq, checkRes)
|
checkRes, err = Check(r.Context(), headers, checkReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
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()
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user