2026-01-08 19:09:47 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2026-06-10 15:01:13 +08:00
|
|
|
"gitea.redpowerfuture.com/red-future/common/beans"
|
|
|
|
|
commonHttp "gitea.redpowerfuture.com/red-future/common/http"
|
|
|
|
|
"gitea.redpowerfuture.com/red-future/common/utils"
|
2026-01-08 19:09:47 +08:00
|
|
|
"github.com/gogf/gf/v2/database/gredis"
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2026-01-22 15:04:13 +08:00
|
|
|
"net/http"
|
2026-01-08 19:09:47 +08:00
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2026-01-14 18:34:56 +08:00
|
|
|
func ModuleTenantCheck(r *ghttp.Request) {
|
2026-03-02 14:22:08 +08:00
|
|
|
//将 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]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 18:34:56 +08:00
|
|
|
// 检查是否是超级管理员
|
2026-03-02 14:22:08 +08:00
|
|
|
isSuperAdmin, err := IsSuperAdmin(r.Context(), headers)
|
|
|
|
|
if err != nil {
|
2026-01-22 15:04:13 +08:00
|
|
|
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
2026-01-14 18:34:56 +08:00
|
|
|
}
|
|
|
|
|
// 如果是超级管理员,则不进行模块租户检查
|
2026-01-21 16:33:37 +08:00
|
|
|
if isSuperAdmin || r.Request.RequestURI == "/asset/getAssetAndSku?assetId=696b4acd1be1c8b76c4b4c15" {
|
2026-01-14 18:34:56 +08:00
|
|
|
r.Middleware.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-01-08 19:09:47 +08:00
|
|
|
getUserInfo, err := utils.GetUserInfo(r.Context())
|
|
|
|
|
if err != nil {
|
2026-01-22 15:04:13 +08:00
|
|
|
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
2026-01-08 19:09:47 +08:00
|
|
|
}
|
|
|
|
|
exit := gconv.Int64(time.Minute * 1)
|
2026-03-02 14:22:08 +08:00
|
|
|
getEX, err := g.Redis("test").GetEX(r.Context(), fmt.Sprintf("module_tenant:tenantId-%v", getUserInfo.TenantId), gredis.GetEXOption{
|
2026-01-08 19:09:47 +08:00
|
|
|
TTLOption: gredis.TTLOption{
|
|
|
|
|
EX: &exit,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2026-01-22 15:04:13 +08:00
|
|
|
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
2026-01-08 19:09:47 +08:00
|
|
|
}
|
|
|
|
|
// 获取模块key
|
2026-01-21 16:33:37 +08:00
|
|
|
moduleKey := g.Cfg().MustGet(context.Background(), "server.name").String()
|
2026-01-08 19:09:47 +08:00
|
|
|
if !g.IsEmpty(getEX.String()) {
|
2026-01-22 15:04:13 +08:00
|
|
|
list := new(beans.ModuleTenant)
|
|
|
|
|
if err = json.Unmarshal(getEX.Bytes(), &list); err != nil {
|
|
|
|
|
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
2026-01-08 19:09:47 +08:00
|
|
|
}
|
|
|
|
|
// 缓存中有数据,检查是否过期
|
2026-01-22 15:04:13 +08:00
|
|
|
if !g.IsEmpty(list.ExpireAt) {
|
2026-01-08 19:09:47 +08:00
|
|
|
gt1 := gtime.New(time.Now())
|
2026-01-22 15:04:13 +08:00
|
|
|
gt2 := gtime.New(list.ExpireAt)
|
2026-01-08 19:09:47 +08:00
|
|
|
if !gt1.Before(gt2) {
|
2026-01-22 15:04:13 +08:00
|
|
|
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, "功能模块已到期,请续期后再使用")
|
|
|
|
|
} else {
|
|
|
|
|
if list.CertificationStatus != 2 {
|
|
|
|
|
SetResponseInfo(r.Context(), r, http.StatusPreconditionRequired, "功能模块未认证通过,请认证后再使用")
|
|
|
|
|
}
|
2026-01-08 19:09:47 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-01-22 15:04:13 +08:00
|
|
|
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, "您未开通此功能模块,请开通后再使用")
|
2026-01-08 19:09:47 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-01-21 16:33:37 +08:00
|
|
|
checkRes := new(beans.ModuleTenantCheckRes)
|
|
|
|
|
checkReq := beans.ModuleTenantCheckReq{
|
|
|
|
|
ModuleKey: moduleKey,
|
2026-01-08 19:09:47 +08:00
|
|
|
TenantId: gconv.Uint64(getUserInfo.TenantId),
|
2026-01-21 16:33:37 +08:00
|
|
|
}
|
2026-03-02 14:22:08 +08:00
|
|
|
checkRes, err = Check(r.Context(), headers, checkReq)
|
2026-01-08 19:09:47 +08:00
|
|
|
if err != nil {
|
2026-01-22 15:04:13 +08:00
|
|
|
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, err)
|
2026-01-08 19:09:47 +08:00
|
|
|
}
|
|
|
|
|
// 根据检查结果判断是否允许访问
|
2026-01-22 15:04:13 +08:00
|
|
|
if !checkRes.Status {
|
|
|
|
|
SetResponseInfo(r.Context(), r, http.StatusPaymentRequired, checkRes.Message)
|
|
|
|
|
} else {
|
|
|
|
|
if !checkRes.CertificationStatus {
|
|
|
|
|
SetResponseInfo(r.Context(), r, http.StatusPreconditionRequired, checkRes.Message)
|
|
|
|
|
}
|
2026-01-08 19:09:47 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-22 15:04:13 +08:00
|
|
|
|
2026-01-08 19:09:47 +08:00
|
|
|
r.Middleware.Next() // 继续执行后续中间件和路由处理
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-14 18:34:56 +08:00
|
|
|
// SetResponseInfo 设置响应信息
|
2026-01-22 15:04:13 +08:00
|
|
|
func SetResponseInfo(ctx context.Context, r *ghttp.Request, code int, message any) {
|
2026-01-14 18:34:56 +08:00
|
|
|
_ = ctx
|
|
|
|
|
r.Response.WriteJsonExit(map[string]interface{}{
|
|
|
|
|
"success": false,
|
2026-01-22 15:04:13 +08:00
|
|
|
"code": code,
|
2026-01-14 18:34:56 +08:00
|
|
|
"message": fmt.Sprintf("服务不可用:%s", message),
|
|
|
|
|
})
|
|
|
|
|
r.Exit()
|
|
|
|
|
}
|
2026-03-02 14:22:08 +08:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|