优化模块租户检查逻辑,重构数据结构并简化代码
This commit is contained in:
@@ -13,21 +13,15 @@ import (
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
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 := nats.CallRPC(r.Context(), "userService.IsSuperAdmin", nil, &isSuperAdmin); err != nil {
|
||||
SetResponseInfo(r.Context(), r, err)
|
||||
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
||||
}
|
||||
// 如果是超级管理员,则不进行模块租户检查
|
||||
if isSuperAdmin || r.Request.RequestURI == "/asset/getAssetAndSku?assetId=696b4acd1be1c8b76c4b4c15" {
|
||||
@@ -36,7 +30,7 @@ func ModuleTenantCheck(r *ghttp.Request) {
|
||||
}
|
||||
getUserInfo, err := utils.GetUserInfo(r.Context())
|
||||
if err != nil {
|
||||
SetResponseInfo(r.Context(), r, err)
|
||||
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
||||
}
|
||||
exit := gconv.Int64(time.Minute * 1)
|
||||
getEX, err := message.GetRedisClientTest("test").GetEX(r.Context(), fmt.Sprintf("module_tenant:tenantId-%v", getUserInfo.TenantId), gredis.GetEXOption{
|
||||
@@ -45,34 +39,30 @@ func ModuleTenantCheck(r *ghttp.Request) {
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
SetResponseInfo(r.Context(), r, err)
|
||||
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
||||
}
|
||||
// 获取模块key
|
||||
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 {
|
||||
SetResponseInfo(r.Context(), r, err)
|
||||
}
|
||||
var expireAt *gtime.Time
|
||||
for _, value := range list {
|
||||
if value.ModuleKey == moduleKey {
|
||||
expireAt = value.ExpireAt
|
||||
break
|
||||
}
|
||||
list := new(beans.ModuleTenant)
|
||||
if err = json.Unmarshal(getEX.Bytes(), &list); err != nil {
|
||||
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
||||
}
|
||||
// 缓存中有数据,检查是否过期
|
||||
if !g.IsEmpty(expireAt) {
|
||||
if !g.IsEmpty(list.ExpireAt) {
|
||||
gt1 := gtime.New(time.Now())
|
||||
gt2 := gtime.New(expireAt)
|
||||
gt2 := gtime.New(list.ExpireAt)
|
||||
if !gt1.Before(gt2) {
|
||||
SetResponseInfo(r.Context(), r, "您访问的模块已过期,请续期后再使用")
|
||||
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, "功能模块已到期,请续期后再使用")
|
||||
} else {
|
||||
if list.CertificationStatus != 2 {
|
||||
SetResponseInfo(r.Context(), r, http.StatusPreconditionRequired, "功能模块未认证通过,请认证后再使用")
|
||||
}
|
||||
}
|
||||
} else {
|
||||
SetResponseInfo(r.Context(), r, "您未开通此模块,请开通后再使用")
|
||||
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, "您未开通此功能模块,请开通后再使用")
|
||||
}
|
||||
} else {
|
||||
// 缓存为空,调用admin-go的Check接口检查模块开通状态
|
||||
checkRes := new(beans.ModuleTenantCheckRes)
|
||||
checkReq := beans.ModuleTenantCheckReq{
|
||||
ModuleKey: moduleKey,
|
||||
@@ -80,25 +70,27 @@ func ModuleTenantCheck(r *ghttp.Request) {
|
||||
}
|
||||
err = nats.CallRPC(r.Context(), "moduleService.Check", &checkReq, checkRes)
|
||||
if err != nil {
|
||||
SetResponseInfo(r.Context(), r, err)
|
||||
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
||||
}
|
||||
// 根据检查结果判断是否允许访问
|
||||
if checkRes.Status == "not_activated" {
|
||||
SetResponseInfo(r.Context(), r, "您未开通此模块,请开通后再使用")
|
||||
} else if checkRes.Status == "expired" {
|
||||
SetResponseInfo(r.Context(), r, "您访问的模块已过期,请续期后再使用")
|
||||
if !checkRes.Status {
|
||||
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, checkRes.Message)
|
||||
} else {
|
||||
if !checkRes.CertificationStatus {
|
||||
SetResponseInfo(r.Context(), r, http.StatusPreconditionRequired, checkRes.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
r.Middleware.Next() // 继续执行后续中间件和路由处理
|
||||
}
|
||||
|
||||
// SetResponseInfo 设置响应信息
|
||||
func SetResponseInfo(ctx context.Context, r *ghttp.Request, message any) {
|
||||
func SetResponseInfo(ctx context.Context, r *ghttp.Request, code int, message any) {
|
||||
_ = ctx
|
||||
r.Response.Status = 402
|
||||
r.Response.WriteJsonExit(map[string]interface{}{
|
||||
"success": false,
|
||||
"code": 402,
|
||||
"code": code,
|
||||
"message": fmt.Sprintf("服务不可用:%s", message),
|
||||
})
|
||||
r.Exit()
|
||||
|
||||
Reference in New Issue
Block a user