From 1868c8dd15e6e874fe235b90da96de5be392593a Mon Sep 17 00:00:00 2001 From: what Date: Wed, 22 Jul 2026 09:11:26 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84:=20User=20=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=8B=86=E5=88=86=E5=87=BA=20user=5Faccessor?= =?UTF-8?q?.go/user=5Fruntime.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit UserAccessor、UserRuntime 从 user.go 拆成独立文件,user.go 只保留 User 接口本身。 --- user.go | 29 ------------------ user_accessor.go | 26 ++++++++++++++++ user_runtime.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 29 deletions(-) mode change 100644 => 100755 user.go create mode 100644 user_accessor.go create mode 100644 user_runtime.go diff --git a/user.go b/user.go old mode 100644 new mode 100755 index 4776884..f153d25 --- a/user.go +++ b/user.go @@ -19,32 +19,3 @@ type User interface { // 是否匿名用户 IsAnonymous() bool } - -type UserRuntime struct { - platform string - saas string -} - -// 账号运行平台 -func (this UserRuntime) Platform() string { - return this.platform -} - -// 账号运行租户 -func (this UserRuntime) SaaS() string { - return this.saas -} - -type UserRuntimeOption func(option *UserRuntime) - -func UserRuntimePlatform(value string) UserRuntimeOption { - return func(option *UserRuntime) { - option.platform = value - } -} - -func UserRuntimeSaaS(value string) UserRuntimeOption { - return func(option *UserRuntime) { - option.saas = value - } -} diff --git a/user_accessor.go b/user_accessor.go new file mode 100644 index 0000000..b82579e --- /dev/null +++ b/user_accessor.go @@ -0,0 +1,26 @@ +package req + +import "git.fsdpf.net/go/reflux/valuex" + +type UserAccessor interface { + valuex.Accessor + User() User +} + +type uAccessor struct { + valuex.Accessor + user User +} + +func (u uAccessor) User() User { + return u.user +} + +// NewUserAccessor 创建一个 UserAccessor 实例 +// accessor 为 valuex.Accessor, user 为当前用户 +func NewUserAccessor(accessor valuex.Accessor, user User) UserAccessor { + return &uAccessor{ + Accessor: accessor, + user: user, + } +} diff --git a/user_runtime.go b/user_runtime.go new file mode 100644 index 0000000..aa54cb3 --- /dev/null +++ b/user_runtime.go @@ -0,0 +1,77 @@ +package req + +import "context" + +// UserRuntime 请求级别的运行时信息,随 req.User 一起传递 +type UserRuntime interface { + // Platform 账号运行平台 + Platform() string + // SaaS 账号运行租户 + SaaS() string + // TraceId 请求追踪ID + TraceId() string + // Ctx 请求级别的上下文,用于挂载不确定/未来才需要的跨切面数据(如 ResWatcher 级联深度), + // 不用于传递 platform/saas/traceId 这类已知的核心参数 + Ctx() context.Context +} + +type uRuntime struct { + platform string + saas string + traceId string + ctx context.Context +} + +func (this *uRuntime) Platform() string { + return this.platform +} + +func (this *uRuntime) SaaS() string { + return this.saas +} + +func (this *uRuntime) TraceId() string { + return this.traceId +} + +func (this *uRuntime) Ctx() context.Context { + if this.ctx == nil { + this.ctx = context.Background() + } + return this.ctx +} + +type UserRuntimeOption func(option *uRuntime) + +func UserRuntimePlatform(value string) UserRuntimeOption { + return func(option *uRuntime) { + option.platform = value + } +} + +func UserRuntimeSaaS(value string) UserRuntimeOption { + return func(option *uRuntime) { + option.saas = value + } +} + +func UserRuntimeTraceId(value string) UserRuntimeOption { + return func(option *uRuntime) { + option.traceId = value + } +} + +func UserRuntimeCtx(ctx context.Context) UserRuntimeOption { + return func(option *uRuntime) { + option.ctx = ctx + } +} + +// NewUserRuntime 构造一个 UserRuntime +func NewUserRuntime(opts ...UserRuntimeOption) UserRuntime { + rt := &uRuntime{} + for _, opt := range opts { + opt(rt) + } + return rt +}