109 lines
3.1 KiB
Go
109 lines
3.1 KiB
Go
|
|
package middleware
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"context"
|
|||
|
|
"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/utils"
|
|||
|
|
"github.com/gogf/gf/v2/database/gredis"
|
|||
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
|||
|
|
"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"
|
|||
|
|
"time"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
type moduleTenant struct{}
|
|||
|
|
|
|||
|
|
var ModuleTenant = new(moduleTenant)
|
|||
|
|
|
|||
|
|
// ModuleTenantInfo 别名,引用admin-go的entity.ModuleTenant
|
|||
|
|
type ModuleTenantInfo = beans.ModuleTenant
|
|||
|
|
|
|||
|
|
func (s *moduleTenant) ModuleTenantCheck(r *ghttp.Request) {
|
|||
|
|
getUserInfo, err := utils.GetUserInfo(r.Context())
|
|||
|
|
if err != nil {
|
|||
|
|
r.Response.WriteJson(err)
|
|||
|
|
r.Exit()
|
|||
|
|
}
|
|||
|
|
exit := gconv.Int64(time.Minute * 1)
|
|||
|
|
getEX, err := message.GetRedisClientTest("test").GetEX(r.Context(), fmt.Sprintf("module_tenant:tenantId-%v", getUserInfo.TenantId), gredis.GetEXOption{
|
|||
|
|
TTLOption: gredis.TTLOption{
|
|||
|
|
EX: &exit,
|
|||
|
|
},
|
|||
|
|
})
|
|||
|
|
if err != nil {
|
|||
|
|
r.Response.WriteJson(err)
|
|||
|
|
r.Exit()
|
|||
|
|
}
|
|||
|
|
// 获取模块key
|
|||
|
|
moduleKey := g.Cfg().MustGet(context.Background(), "server.name")
|
|||
|
|
if !g.IsEmpty(getEX.String()) {
|
|||
|
|
list := make([]ModuleTenantInfo, 0)
|
|||
|
|
if err = json.Unmarshal([]byte(getEX.String()), &list); err != nil {
|
|||
|
|
r.Response.WriteJson(err)
|
|||
|
|
r.Exit()
|
|||
|
|
}
|
|||
|
|
var expireAt *gtime.Time
|
|||
|
|
for _, value := range list {
|
|||
|
|
if value.ModuleKey == moduleKey.String() {
|
|||
|
|
expireAt = value.ExpireAt
|
|||
|
|
break
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// 缓存中有数据,检查是否过期
|
|||
|
|
if !g.IsEmpty(expireAt) {
|
|||
|
|
gt1 := gtime.New(time.Now())
|
|||
|
|
gt2 := gtime.New(expireAt)
|
|||
|
|
if !gt1.Before(gt2) {
|
|||
|
|
r.Response.WriteJson(gerror.New("您访问的模块已过期,请续期后再使用"))
|
|||
|
|
r.Exit()
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
r.Response.WriteJson(gerror.New("您未开通此模块,请开通后再使用"))
|
|||
|
|
r.Exit()
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
//将 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]
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// 缓存为空,调用admin-go的Check接口检查模块开通状态
|
|||
|
|
res, err := s.Check(r.Context(), headers, beans.ModuleTenantCheckReq{
|
|||
|
|
ModuleKey: moduleKey.String(),
|
|||
|
|
TenantId: gconv.Uint64(getUserInfo.TenantId),
|
|||
|
|
})
|
|||
|
|
if err != nil {
|
|||
|
|
r.Response.WriteJson(err)
|
|||
|
|
r.Exit()
|
|||
|
|
}
|
|||
|
|
// 根据检查结果判断是否允许访问
|
|||
|
|
if res.Status == "not_activated" {
|
|||
|
|
r.Response.WriteJson(gerror.New("您未开通此模块,请开通后再使用"))
|
|||
|
|
r.Exit()
|
|||
|
|
} else if res.Status == "expired" {
|
|||
|
|
r.Response.WriteJson(gerror.New("您访问的模块已过期,请续期后再使用"))
|
|||
|
|
r.Exit()
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
r.Middleware.Next() // 继续执行后续中间件和路由处理
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Check 调用admin-go服务检查模块开通状态
|
|||
|
|
func (s *moduleTenant) Check(ctx context.Context, headers map[string]string, req beans.ModuleTenantCheckReq) (res *beans.ModuleTenantCheckRes, err error) {
|
|||
|
|
if err = http.Get(ctx, "admin-go/api/v1/system/moduleTenant/check", headers, &res,
|
|||
|
|
"moduleKey", req.ModuleKey,
|
|||
|
|
"tenantId", req.TenantId,
|
|||
|
|
); err != nil {
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
return
|
|||
|
|
}
|