// 生成代码, 不要在这里添加任何逻辑 package {{.PackageName}} import ( "context" "strings" "sync" "fusen-basic/basic" "fusen-basic/env" "fusen-basic/utils/log" "{{.ProjectName}}/gen/go/service" "{{.ProjectName}}/server/config" "google.golang.org/grpc/metadata" ) type {{.StructName}} struct { ctx context.Context cfg *config.Config svcCtx *SharedContext } func New{{.StructName}}(ctx context.Context) *{{.StructName}} { l := &{{.StructName}}{} l.ctx = ctx // 初始化上下文 l.cfg = ctx.Value(env.CtxServiceConfig{}).(*config.Config) // 初始化拿到的配置结构 l.svcCtx = svcCtx onceSharedContext.Do(func() { svcCtx.Init(ctx, l.cfg) }) l.svcCtx.Handler(ctx, l.cfg) return l } var onceSharedContext sync.Once var svcCtx = &SharedContext{} type {{.StructName}}Grpc struct { {{.UnimplementedStructName}} } {{range .Methods}} type {{.MethodName}}Handler struct { {{.MethodName}}HandlerMust } type {{.MethodName}}HandlerMust struct{} func (must *{{.MethodName}}HandlerMust) ValidUserInfoHandler(ctx context.Context) *basic.UserInfo { return defaultValidUserInfoHandler(ctx) } var __hide_handler{{.MethodName}} *{{.MethodName}}Handler = &{{.MethodName}}Handler{} func (lgrpc *{{.StructName}}Grpc) {{.MethodName}}(ctx {{.ParamCtx}}, req {{.ParamReq}}) ({{.MethodReturn}}, error) { return New{{.StructName}}(ctx).{{.MethodName}}Logic(req, __hide_handler{{.MethodName}}.ValidUserInfoHandler(ctx)).PassMetaResponse(ctx) } {{end}} func DefaultValidToken(ctx context.Context) *basic.UserInfo { var ( err error ui = &basic.UserInfo{} ) // log.Println(ctx) if md, ok := metadata.FromIncomingContext(ctx); ok { var authtoken, debugtoken *string var vresult *service.ValidTokenResponse if a, ok := md["authorization"]; ok { token := a[0] if len(token) > 15 { if strings.HasPrefix(token[1:], "earer ") { token = token[7:] } authtoken = &token defer func() { if vresult != nil { userinfo := vresult.UserInfo.AsMap() if len(userinfo) != 0 { ui.UserId = int64(userinfo["user_id"].(float64)) ui.GuestId = int64(userinfo["guest_id"].(float64)) ui.Exp = int64(userinfo["exp"].(float64)) } } }() } } if a, ok := md["debug-token"]; ok { token := a[0] if len(token) > 15 { if strings.HasPrefix(token[1:], "earer ") { token = token[7:] } debugtoken = &token defer func() { if vresult != nil { debug := vresult.DebugInfo.AsMap() if len(debug) != 0 { ui.Debug = &basic.Debug{} if dexp, ok := debug["exp"]; ok { dexpint64 := int64(dexp.(float64)) ui.Debug.Exp = &dexpint64 } ui.Debug.IsAllTemplateTag = int64(debug["is_all_template_tag"].(float64)) ui.Debug.IsCache = int64(debug["is_cache"].(float64)) } } }() } } vresult, err = service.AutoAuthClient(ctx).ValidToken(context.TODO(), &service.ValidTokenRequest{ UserToken: authtoken, DebugToken: debugtoken, }) if err != nil { log.Println(err.Error()) } } return ui }