Files
panel/internal/biz/user.go
T
耗子andClaude Opus 4.8 443516a2cf refactor!: 迁移至 samber/do 依赖注入与三层架构
依赖注入:
- 移除 google/wire(含 wire.go/wire_gen.go 与全部 ProviderSet),
  改用 samber/do v2 单注入器 + 双入口惰性构建
- 贡献模型替代命令式注册:路由 routes:、命令 commands:、
  任务 jobs: 前缀经 internal/registry 收集与校验
- 构造函数统一 func NewXxx(i do.Injector) (T, error)

三层架构:
- 补全用例层:每个 biz.XxxRepo 配 XxxUsecase,
  service/command/job 表现层只依赖用例,不再直接引用仓储

路由与文档:
- route/http.go 按域拆为声明式 Endpoint 贡献,
  Endpoint 承载登录白名单与端点限流语义
- 调试模式下提供 OpenAPI 3.1 文档:/openapi.json 与 /docs(Scalar),
  从 validate 标签生成

目录与依赖:
- internal/http/{middleware,request,rule} 拍平至 internal/*
- CLI 命令拆至 internal/command
- 日志轮转 timberjack 换为 libtnb/logrotate
- 升级 validator/cron/sessions/gormstore/sqlite/securecookie
- 数据库迁移保持 gormigrate 不变

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-09 21:54:22 +08:00

89 lines
2.7 KiB
Go

package biz
import (
"context"
"image"
"time"
"gorm.io/gorm"
)
type User struct {
ID uint `gorm:"primaryKey" json:"id"`
Username string `gorm:"not null;default:'';unique" json:"username"`
Password string `gorm:"not null;default:''" json:"password"`
Email string `gorm:"not null;default:''" json:"email"`
TwoFA string `gorm:"not null;default:''" json:"two_fa"` // 2FA secret,为空表示未开启
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
Tokens []*UserToken `gorm:"foreignKey:UserID" json:"-"`
}
type UserRepo interface {
List(page, limit uint) ([]*User, int64, error)
Get(id uint) (*User, error)
Create(ctx context.Context, username, password, email string) (*User, error)
UpdateUsername(ctx context.Context, id uint, username string) error
UpdatePassword(ctx context.Context, id uint, password string) error
UpdateEmail(ctx context.Context, id uint, email string) error
Delete(ctx context.Context, id uint) error
CheckPassword(username, password string) (*User, error)
IsTwoFA(username string) (bool, error)
GenerateTwoFA(id uint) (image.Image, string, string, error)
UpdateTwoFA(id uint, code, secret string) error
}
type UserUsecase struct {
repo UserRepo
}
func NewUserUsecase(repo UserRepo) *UserUsecase {
return &UserUsecase{repo: repo}
}
func (uc *UserUsecase) List(page, limit uint) ([]*User, int64, error) {
return uc.repo.List(page, limit)
}
func (uc *UserUsecase) Get(id uint) (*User, error) {
return uc.repo.Get(id)
}
func (uc *UserUsecase) Create(ctx context.Context, username, password, email string) (*User, error) {
return uc.repo.Create(ctx, username, password, email)
}
func (uc *UserUsecase) UpdateUsername(ctx context.Context, id uint, username string) error {
return uc.repo.UpdateUsername(ctx, id, username)
}
func (uc *UserUsecase) UpdatePassword(ctx context.Context, id uint, password string) error {
return uc.repo.UpdatePassword(ctx, id, password)
}
func (uc *UserUsecase) UpdateEmail(ctx context.Context, id uint, email string) error {
return uc.repo.UpdateEmail(ctx, id, email)
}
func (uc *UserUsecase) Delete(ctx context.Context, id uint) error {
return uc.repo.Delete(ctx, id)
}
func (uc *UserUsecase) CheckPassword(username, password string) (*User, error) {
return uc.repo.CheckPassword(username, password)
}
func (uc *UserUsecase) IsTwoFA(username string) (bool, error) {
return uc.repo.IsTwoFA(username)
}
func (uc *UserUsecase) GenerateTwoFA(id uint) (image.Image, string, string, error) {
return uc.repo.GenerateTwoFA(id)
}
func (uc *UserUsecase) UpdateTwoFA(id uint, code, secret string) error {
return uc.repo.UpdateTwoFA(id, code, secret)
}