Files
EasyGoAdmin_GoFrame_EleVue/internal/middleware/wire.go
T
2026-10-20 11:37:25 +08:00

88 lines
3.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package middleware
import (
"context"
"time"
"github.com/gogf/gf/v2/os/gtime"
)
// ============================================================================
// 依赖接线:中间件不直接依赖业务层,由 cmd 在启动时注册加载器与落地器。
// ============================================================================
// ============================== 依赖类型 ==============================
// Identity 用户身份扩展信息
// 包含角色编码与权限节点
type Identity struct {
Roles []string `json:"roles"` // 角色编码集合
Perms []string `json:"perms"` // 权限节点集合
DataScope int `json:"dataScope"` // 数据范围(多角色取最宽松;0=旧缓存缺字段,按全部处理)
DeptId int `json:"deptId"` // 所属部门ID(0=未分配)
}
// TenantInfo 租户摘要
// 仅保留状态与过期校验热路径所需的最小字段
type TenantInfo struct {
ID int `json:"id"` // 租户ID
Status int `json:"status"` // 状态:1-正常 2-禁用
ExpireTime *gtime.Time `json:"expireTime"` // 过期时间,nil 表示永不过期
}
// OperationLogEntry 操作日志记录
// 字段覆盖操作日志表全部列
type OperationLogEntry struct {
Title string `json:"title"` // 日志标题
Type int `json:"type"` // 操作类型
Source int `json:"source"` // 来源
Method string `json:"method"` // 路由模板
RequestMethod string `json:"requestMethod"` // HTTP 方法
URL string `json:"url"` // 请求地址
Param string `json:"param"` // 脱敏后的请求参数
Result string `json:"result"` // 响应报文(截断)
IP string `json:"ip"` // 客户端IP
Location string `json:"location"` // 归属地
OS string `json:"os"` // 操作系统
Browser string `json:"browser"` // 浏览器
ConsumeTime int64 `json:"consumeTime"` // 耗时(毫秒)
Status int `json:"status"` // 状态:0-正常 1-异常
Error string `json:"error"` // 错误信息
CreateUser string `json:"createUser"` // 操作人
}
// ============================== 依赖注册 ==============================
var (
// IdentityLoader 加载用户角色与权限
// 为 nil 时跳过加载,仅认证身份
IdentityLoader func(ctx context.Context, userID int) (*Identity, error)
// TenantLoader 加载租户状态摘要
// 为 nil 时跳过租户校验
TenantLoader func(ctx context.Context, tenantID int) (*TenantInfo, error)
// LogSink 操作日志异步落库
// 为 nil 时降级为调试日志
LogSink func(ctx context.Context, entry *OperationLogEntry) error
)
// SetIdentityLoader 注册身份加载器
func SetIdentityLoader(f func(ctx context.Context, userID int) (*Identity, error)) {
IdentityLoader = f
}
// SetTenantLoader 注册租户加载器
func SetTenantLoader(f func(ctx context.Context, tenantID int) (*TenantInfo, error)) {
TenantLoader = f
}
// SetLogSink 注册操作日志落地器
func SetLogSink(f func(ctx context.Context, entry *OperationLogEntry) error) {
LogSink = f
}
// Now 返回当前时间
// 供各中间件统一使用
func Now() time.Time { return time.Now() }