1.修改basedo字段类型
2、提供获取authen中用户信息的方法
This commit is contained in:
@@ -1,16 +1,21 @@
|
||||
package do
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
type MongoBaseDO struct {
|
||||
Id bson.ObjectID `bson:"_id,omitempty" json:"id"` // MongoDB 默认 ID
|
||||
Creator bson.ObjectID `bson:"creator,omitempty" json:"creator"`
|
||||
Creator interface{} `bson:"creator,omitempty" json:"creator"`
|
||||
CreatedAt time.Time `bson:"createdAt,omitempty" json:"createdAt"`
|
||||
Updater bson.ObjectID `bson:"updater,omitempty" json:"updater"`
|
||||
Updater interface{} `bson:"updater,omitempty" json:"updater"`
|
||||
UpdatedAt time.Time `bson:"updatedAt,omitempty" json:"updatedAt"`
|
||||
TenantId bson.ObjectID `bson:"tenantId" json:"tenantId" default:"1"` // 租户ID
|
||||
TenantId interface{} `bson:"tenantId" json:"tenantId" default:"1"` // 租户ID
|
||||
IsDeleted bool `bson:"isDeleted" json:"isDeleted" default:"false"`
|
||||
}
|
||||
type User struct {
|
||||
Id interface{} `bson:"_id,omitempty" json:"id"` // MongoDB 默认 ID
|
||||
TenantId interface{} `bson:"tenantId" json:"tenantId" default:"1"` // 租户ID
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
@@ -35,6 +36,7 @@ func Limiter(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
}
|
||||
func Auth(r *ghttp.Request) {
|
||||
//utils.GetUserInfo(r.GetCtx())
|
||||
token := r.Header.Get("Authorization")
|
||||
if token == "" || !gstr.HasPrefix(token, "Bearer ") {
|
||||
r.Response.WriteStatusExit(401, "Unauthorized")
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"gitee.com/red-future---jilin-g/common/do"
|
||||
"github.com/gogf/gf/v2/database/gredis"
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/tiger1103/gfast-token/gftoken"
|
||||
"reflect"
|
||||
"time"
|
||||
)
|
||||
@@ -51,3 +57,20 @@ func GetMonthToday(t time.Time, month int) time.Time {
|
||||
}
|
||||
return target.AddDate(0, 0, t.Day()-1)
|
||||
}
|
||||
func GetUserInfo(ctx context.Context) (user do.User, err error) {
|
||||
redisAddr := g.Cfg().MustGet(ctx, "redis.default.address").String()
|
||||
gft := gftoken.NewGfToken(
|
||||
gftoken.WithCacheKey("gfToken:"),
|
||||
gftoken.WithTimeout(20),
|
||||
gftoken.WithMaxRefresh(10),
|
||||
gftoken.WithMultiLogin(true),
|
||||
//gftoken.WithExcludePaths(g.SliceStr{"/excludeDemo"}),
|
||||
gftoken.WithGRedisConfig(&gredis.Config{
|
||||
Address: redisAddr,
|
||||
Db: 1,
|
||||
}))
|
||||
data, _ := gft.ParseToken(g.RequestFromCtx(ctx))
|
||||
user.Id = gconv.Map(data.Data)["id"]
|
||||
user.TenantId = gconv.Map(data.Data)["tenantId"]
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user