/* * @desc:token功能 * * * @Date: 2022/3/8 15:54 */ package service import ( "context" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/os/gctx" "github.com/tiger1103/gfast-token/gftoken" "sync" "tyj_admin/internal/consts" "tyj_admin/internal/model" "tyj_admin/library/liberr" ) type IGfToken interface { GenerateToken(ctx context.Context, key string, data interface{}) (keys string, err error) Middleware(group *ghttp.RouterGroup) error ParseToken(r *ghttp.Request) (*gftoken.CustomClaims, error) IsLogin(r *ghttp.Request) (b bool, failed *gftoken.AuthFailed) GetRequestToken(r *ghttp.Request) (token string) RemoveToken(ctx context.Context, token string) (err error) } type gfTokenImpl struct { *gftoken.GfToken } var gT = gfTokenImpl{ GfToken: gftoken.NewGfToken(), } func GfToken(options *model.TokenOptions) IGfToken { var fun gftoken.OptionFunc if options.CacheModel == consts.CacheModelRedis { fun = gftoken.WithGRedis() } else { fun = gftoken.WithGCache() } gT.GfToken = gftoken.NewGfToken( gftoken.WithCacheKey(options.CacheKey), gftoken.WithTimeout(options.Timeout), gftoken.WithMaxRefresh(options.MaxRefresh), gftoken.WithMultiLogin(options.MultiLogin), gftoken.WithExcludePaths(options.ExcludePaths), fun, ) return &gT } // =======================system========================== type gft struct { options *model.TokenOptions gT IGfToken lock *sync.Mutex } var gftService = &gft{ options: nil, gT: nil, lock: &sync.Mutex{}, } func SysGfToken() IGfToken { if gftService.gT == nil { gftService.lock.Lock() defer gftService.lock.Unlock() if gftService.gT == nil { ctx := gctx.New() err := g.Cfg().MustGet(ctx, "gfToken").Struct(&gftService.options) liberr.ErrIsNil(ctx, err) gftService.gT = GfToken(gftService.options) } } return gftService.gT }