增加检查租户模块开通状态的中间件。修改mongo查询list中多字段排序。utils中增加结构体转换
This commit is contained in:
31
beans/module_tenant.go
Normal file
31
beans/module_tenant.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package beans
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
type ModuleTenantCheckReq struct {
|
||||
ModuleKey string `p:"moduleKey" v:"required#模块Key不能为空"`
|
||||
TenantId uint64 `p:"tenantId" v:"required#租户ID不能为空"`
|
||||
}
|
||||
|
||||
// ModuleTenantCheckRes 调用admin-go设置模块租户关系的响应
|
||||
type ModuleTenantCheckRes struct {
|
||||
Status string `json:"status"` // 开通状态:activated(已开通)、expired(已到期)、not_activated(未开通)
|
||||
Message string `json:"message"` // 状态描述
|
||||
OpenStatus bool `json:"openStatus"` // 开通状态
|
||||
}
|
||||
|
||||
// ModuleTenant 模块租户关系实体(引用自admin-go)
|
||||
type ModuleTenant struct {
|
||||
Id uint64 `json:"id" description:""`
|
||||
CreateBy uint64 `json:"createBy" description:"创建者"`
|
||||
UpdateBy uint64 `json:"updateBy" description:"更新者"`
|
||||
CreatedAt *gtime.Time `json:"createdAt" description:"创建时间"`
|
||||
UpdatedAt *gtime.Time `json:"updatedAt" description:"更新时间"`
|
||||
ModuleKey string `json:"moduleKey" description:"模块Key"`
|
||||
TenantId uint64 `json:"tenantId" description:"租户ID"`
|
||||
ExpireAt *gtime.Time `json:"expireAt" description:"到期时间"`
|
||||
AssetId string `json:"assetId" description:"资产ID"`
|
||||
AssetSkuId string `json:"assetSkuId" description:"资产SKU ID"`
|
||||
}
|
||||
108
middleware/module_tenant_check.go
Normal file
108
middleware/module_tenant_check.go
Normal file
@@ -0,0 +1,108 @@
|
||||
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
|
||||
}
|
||||
@@ -279,12 +279,12 @@ func (m *MongoDB) Find(ctx context.Context, filter bson.M, result interface{}, c
|
||||
if orderBy == nil {
|
||||
opt.SetSort(bson.M{"createdAt": -1})
|
||||
} else {
|
||||
orderBson := bson.M{}
|
||||
orderBson := bson.D{}
|
||||
for _, v := range orderBy {
|
||||
if v.Order == beans.Asc {
|
||||
orderBson[v.Field] = 1
|
||||
orderBson = append(orderBson, bson.E{Key: v.Field, Value: 1}) // 1 表示升序
|
||||
} else {
|
||||
orderBson[v.Field] = -1
|
||||
orderBson = append(orderBson, bson.E{Key: v.Field, Value: -1}) // -1 表示降序
|
||||
}
|
||||
}
|
||||
opt.SetSort(orderBson)
|
||||
|
||||
@@ -2,6 +2,7 @@ package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
@@ -100,7 +101,6 @@ func GetUserInfo(ctx context.Context) (user beans.User, err error) {
|
||||
} else {
|
||||
user.TenantId = ctx.Value("tenantId")
|
||||
user.UserName = ctx.Value("userName")
|
||||
fmt.Println("user.UserName==================", user.UserName)
|
||||
}
|
||||
if user.TenantId == nil {
|
||||
return user, gerror.New("租户信息为空")
|
||||
@@ -297,3 +297,15 @@ func HexDigit(c byte) byte {
|
||||
return 0xFF
|
||||
}
|
||||
}
|
||||
|
||||
func Struct(params any, pointer any) error {
|
||||
b, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = json.Unmarshal(b, &pointer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user