优化用户模型和模块租户检查逻辑,新增NATS消息配置和MongoDB缓存控制
This commit is contained in:
@@ -5,8 +5,8 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"gitee.com/red-future---jilin-g/common/beans"
|
||||
"gitee.com/red-future---jilin-g/common/http"
|
||||
"gitee.com/red-future---jilin-g/common/message"
|
||||
"gitee.com/red-future---jilin-g/common/nats"
|
||||
"gitee.com/red-future---jilin-g/common/utils"
|
||||
"github.com/gogf/gf/v2/database/gredis"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
@@ -25,12 +25,12 @@ func ModuleTenantCheck(r *ghttp.Request) {
|
||||
}
|
||||
}
|
||||
// 检查是否是超级管理员
|
||||
IsSuperAdmin, err := IsSuperAdmin(r.Context(), headers)
|
||||
if err != nil {
|
||||
isSuperAdmin := false
|
||||
if err := nats.CallRPC(r.Context(), "userService.IsSuperAdmin", nil, &isSuperAdmin); err != nil {
|
||||
SetResponseInfo(r.Context(), r, err)
|
||||
}
|
||||
// 如果是超级管理员,则不进行模块租户检查
|
||||
if IsSuperAdmin.IsSuperAdmin {
|
||||
if isSuperAdmin || r.Request.RequestURI == "/asset/getAssetAndSku?assetId=696b4acd1be1c8b76c4b4c15" {
|
||||
r.Middleware.Next()
|
||||
return
|
||||
}
|
||||
@@ -48,7 +48,7 @@ func ModuleTenantCheck(r *ghttp.Request) {
|
||||
SetResponseInfo(r.Context(), r, err)
|
||||
}
|
||||
// 获取模块key
|
||||
moduleKey := g.Cfg().MustGet(context.Background(), "server.name")
|
||||
moduleKey := g.Cfg().MustGet(context.Background(), "server.name").String()
|
||||
if !g.IsEmpty(getEX.String()) {
|
||||
list := make([]beans.ModuleTenant, 0)
|
||||
if err = json.Unmarshal([]byte(getEX.String()), &list); err != nil {
|
||||
@@ -56,7 +56,7 @@ func ModuleTenantCheck(r *ghttp.Request) {
|
||||
}
|
||||
var expireAt *gtime.Time
|
||||
for _, value := range list {
|
||||
if value.ModuleKey == moduleKey.String() {
|
||||
if value.ModuleKey == moduleKey {
|
||||
expireAt = value.ExpireAt
|
||||
break
|
||||
}
|
||||
@@ -73,17 +73,19 @@ func ModuleTenantCheck(r *ghttp.Request) {
|
||||
}
|
||||
} else {
|
||||
// 缓存为空,调用admin-go的Check接口检查模块开通状态
|
||||
res, err := Check(r.Context(), headers, beans.ModuleTenantCheckReq{
|
||||
ModuleKey: moduleKey.String(),
|
||||
checkRes := new(beans.ModuleTenantCheckRes)
|
||||
checkReq := beans.ModuleTenantCheckReq{
|
||||
ModuleKey: moduleKey,
|
||||
TenantId: gconv.Uint64(getUserInfo.TenantId),
|
||||
})
|
||||
}
|
||||
err = nats.CallRPC(r.Context(), "moduleService.Check", &checkReq, checkRes)
|
||||
if err != nil {
|
||||
SetResponseInfo(r.Context(), r, err)
|
||||
}
|
||||
// 根据检查结果判断是否允许访问
|
||||
if res.Status == "not_activated" {
|
||||
if checkRes.Status == "not_activated" {
|
||||
SetResponseInfo(r.Context(), r, "您未开通此模块,请开通后再使用")
|
||||
} else if res.Status == "expired" {
|
||||
} else if checkRes.Status == "expired" {
|
||||
SetResponseInfo(r.Context(), r, "您访问的模块已过期,请续期后再使用")
|
||||
}
|
||||
}
|
||||
@@ -93,30 +95,11 @@ func ModuleTenantCheck(r *ghttp.Request) {
|
||||
// SetResponseInfo 设置响应信息
|
||||
func SetResponseInfo(ctx context.Context, r *ghttp.Request, message any) {
|
||||
_ = ctx
|
||||
r.Response.Status = 503
|
||||
r.Response.Status = 402
|
||||
r.Response.WriteJsonExit(map[string]interface{}{
|
||||
"success": false,
|
||||
"code": 503,
|
||||
"code": 402,
|
||||
"message": fmt.Sprintf("服务不可用:%s", message),
|
||||
})
|
||||
r.Exit()
|
||||
}
|
||||
|
||||
// Check 调用admin-go服务检查模块开通状态
|
||||
func Check(ctx context.Context, headerMap map[string]string, req beans.ModuleTenantCheckReq) (res *beans.ModuleTenantCheckRes, err error) {
|
||||
if err = http.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 *beans.IsSuperAdminRes, err error) {
|
||||
if err = http.Get(ctx, "admin-go/api/v1/system/user/checkIsSuperAdmin", headerMap, &res); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user