This commit is contained in:
toom1996
2026-09-07 00:01:48 +08:00
parent b049104a0b
commit 74ba700598
31 changed files with 1113 additions and 606 deletions

View File

@ -18,6 +18,10 @@ type UserRepository interface {
FindByID(ctx context.Context, id uint32) (*model.User, error)
// FindByAccount 按用户名或邮箱查找(忽略已删除账号),供登录校验。
FindByAccount(ctx context.Context, account string) (*model.User, error)
// SetTier 设置用户等级(free/vip),供后台管理提级 VIP。
SetTier(ctx context.Context, id uint32, tier string) error
// List 列出全量用户(后台管理用),按 id 升序。
List(ctx context.Context) ([]model.User, error)
}
type userRepository struct {
@ -44,6 +48,22 @@ func (r *userRepository) FindByAccount(ctx context.Context, account string) (*mo
return wrapUser(&u, err)
}
func (r *userRepository) SetTier(ctx context.Context, id uint32, tier string) error {
return r.db.WithContext(ctx).
Model(&model.User{}).
Where("id = ? AND is_deleted = ?", id, 0).
Update("tier", tier).Error
}
func (r *userRepository) List(ctx context.Context) ([]model.User, error) {
var users []model.User
err := r.db.WithContext(ctx).
Where("is_deleted = ?", 0).
Order("id ASC").
Find(&users).Error
return users, err
}
func wrapUser(u *model.User, err error) (*model.User, error) {
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {