优化HTTP请求处理和模块租户检查逻辑:支持GET请求参数转换,简化日志消费者配置,改进RPC调用方式
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user