temp
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"time"
|
||||
@@ -11,6 +10,14 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// UserType 用户类型枚举
|
||||
type UserType string
|
||||
|
||||
const (
|
||||
UserTypeNormal UserType = "user" // 普通用户
|
||||
UserTypeAdmin UserType = "admin" // 管理员
|
||||
)
|
||||
|
||||
// User 用户实体
|
||||
// 系统用户的核心信息,提供基础的账户管理功能
|
||||
// 支持手机号登录,密码加密存储,实现Entity接口便于统一管理
|
||||
@@ -20,6 +27,16 @@ type User struct {
|
||||
Phone string `gorm:"uniqueIndex;type:varchar(20);not null" json:"phone" comment:"手机号码(登录账号)"`
|
||||
Password string `gorm:"type:varchar(255);not null" json:"-" comment:"登录密码(加密存储,不返回前端)"`
|
||||
|
||||
// 用户类型和基本信息
|
||||
UserType string `gorm:"type:varchar(20);not null;default:'user'" json:"user_type" comment:"用户类型(user/admin)"`
|
||||
Username string `gorm:"type:varchar(100)" json:"username" comment:"用户名(管理员专用)"`
|
||||
|
||||
// 管理员特有字段
|
||||
Active bool `gorm:"default:true" json:"is_active" comment:"账户是否激活"`
|
||||
LastLoginAt *time.Time `json:"last_login_at" comment:"最后登录时间"`
|
||||
LoginCount int `gorm:"default:0" json:"login_count" comment:"登录次数统计"`
|
||||
Permissions string `gorm:"type:text" json:"permissions" comment:"权限列表(JSON格式存储)"`
|
||||
|
||||
// 时间戳字段
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at" comment:"创建时间"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at" comment:"更新时间"`
|
||||
@@ -78,6 +95,42 @@ func (u *User) Validate() error {
|
||||
|
||||
// ================ 业务方法 ================
|
||||
|
||||
// IsAdmin 检查是否为管理员
|
||||
func (u *User) IsAdmin() bool {
|
||||
return u.UserType == string(UserTypeAdmin)
|
||||
}
|
||||
|
||||
// IsNormalUser 检查是否为普通用户
|
||||
func (u *User) IsNormalUser() bool {
|
||||
return u.UserType == string(UserTypeNormal)
|
||||
}
|
||||
|
||||
// SetUserType 设置用户类型
|
||||
func (u *User) SetUserType(userType UserType) {
|
||||
u.UserType = string(userType)
|
||||
}
|
||||
|
||||
// UpdateLastLoginAt 更新最后登录时间
|
||||
func (u *User) UpdateLastLoginAt() {
|
||||
now := time.Now()
|
||||
u.LastLoginAt = &now
|
||||
}
|
||||
|
||||
// IncrementLoginCount 增加登录次数
|
||||
func (u *User) IncrementLoginCount() {
|
||||
u.LoginCount++
|
||||
}
|
||||
|
||||
// Activate 激活用户账户
|
||||
func (u *User) Activate() {
|
||||
u.Active = true
|
||||
}
|
||||
|
||||
// Deactivate 停用用户账户
|
||||
func (u *User) Deactivate() {
|
||||
u.Active = false
|
||||
}
|
||||
|
||||
// ChangePassword 修改密码
|
||||
// 验证旧密码,检查新密码强度,更新密码
|
||||
func (u *User) ChangePassword(oldPassword, newPassword, confirmPassword string) error {
|
||||
@@ -168,6 +221,11 @@ func (u *User) CanLogin() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// 如果是管理员,检查是否激活
|
||||
if u.IsAdmin() && !u.Active {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -205,7 +263,7 @@ func (u *User) GetMaskedPhone() string {
|
||||
return u.Phone[:3] + "****" + u.Phone[len(u.Phone)-4:]
|
||||
}
|
||||
|
||||
// ================ 私有辅助方法 ================
|
||||
// ================ 私有方法 ================
|
||||
|
||||
// hashPassword 加密密码
|
||||
func (u *User) hashPassword(password string) (string, error) {
|
||||
@@ -218,61 +276,30 @@ func (u *User) hashPassword(password string) (string, error) {
|
||||
|
||||
// validatePasswordStrength 验证密码强度
|
||||
func (u *User) validatePasswordStrength(password string) error {
|
||||
if len(password) < 8 {
|
||||
return NewValidationError("密码长度至少8位")
|
||||
if len(password) < 6 {
|
||||
return NewValidationError("密码长度不能少于6位")
|
||||
}
|
||||
|
||||
if len(password) > 128 {
|
||||
return NewValidationError("密码长度不能超过128位")
|
||||
if len(password) > 20 {
|
||||
return NewValidationError("密码长度不能超过20位")
|
||||
}
|
||||
|
||||
// 检查是否包含数字
|
||||
hasDigit := regexp.MustCompile(`[0-9]`).MatchString(password)
|
||||
if !hasDigit {
|
||||
return NewValidationError("密码必须包含数字")
|
||||
}
|
||||
|
||||
// 检查是否包含字母
|
||||
hasLetter := regexp.MustCompile(`[a-zA-Z]`).MatchString(password)
|
||||
if !hasLetter {
|
||||
return NewValidationError("密码必须包含字母")
|
||||
}
|
||||
|
||||
// 检查是否包含特殊字符(可选,可以根据需求调整)
|
||||
hasSpecial := regexp.MustCompile(`[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]`).MatchString(password)
|
||||
if !hasSpecial {
|
||||
return NewValidationError("密码必须包含特殊字符")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ================ 静态工具方法 ================
|
||||
|
||||
// IsValidPhoneFormat 验证手机号格式(静态方法)
|
||||
// IsValidPhoneFormat 验证手机号格式
|
||||
func IsValidPhoneFormat(phone string) bool {
|
||||
if phone == "" {
|
||||
return false
|
||||
}
|
||||
// 中国手机号验证(11位数字,以1开头)
|
||||
pattern := `^1[3-9]\d{9}$`
|
||||
matched, _ := regexp.MatchString(pattern, phone)
|
||||
return matched
|
||||
}
|
||||
|
||||
// NewUser 创建新用户(工厂方法)
|
||||
// NewUser 创建新用户
|
||||
func NewUser(phone, password string) (*User, error) {
|
||||
user := &User{
|
||||
ID: "", // 由数据库或调用方设置
|
||||
Phone: phone,
|
||||
Phone: phone,
|
||||
UserType: string(UserTypeNormal), // 默认为普通用户
|
||||
Active: true,
|
||||
}
|
||||
|
||||
// 验证手机号
|
||||
if err := user.SetPhone(phone); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 设置密码
|
||||
if err := user.SetPassword(password); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -280,41 +307,60 @@ func NewUser(phone, password string) (*User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// TableName 指定表名
|
||||
// NewAdminUser 创建新管理员用户
|
||||
func NewAdminUser(phone, password, username string) (*User, error) {
|
||||
user := &User{
|
||||
Phone: phone,
|
||||
Username: username,
|
||||
UserType: string(UserTypeAdmin),
|
||||
Active: true,
|
||||
}
|
||||
|
||||
if err := user.SetPassword(password); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
func (User) TableName() string {
|
||||
return "users"
|
||||
}
|
||||
|
||||
// ValidationError 验证错误
|
||||
// 自定义验证错误类型,提供结构化的错误信息
|
||||
// ================ 错误处理 ================
|
||||
|
||||
type ValidationError struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
// Error 实现error接口
|
||||
func (e *ValidationError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
// NewValidationError 创建新的验证错误
|
||||
// 工厂方法,用于创建验证错误实例
|
||||
func NewValidationError(message string) *ValidationError {
|
||||
return &ValidationError{Message: message}
|
||||
}
|
||||
|
||||
// IsValidationError 检查是否为验证错误
|
||||
func IsValidationError(err error) bool {
|
||||
var validationErr *ValidationError
|
||||
return errors.As(err, &validationErr)
|
||||
_, ok := err.(*ValidationError)
|
||||
return ok
|
||||
}
|
||||
|
||||
// UserCache 用户缓存结构体
|
||||
// 专门用于缓存序列化,包含Password字段
|
||||
// ================ 缓存相关 ================
|
||||
|
||||
type UserCache struct {
|
||||
// 基础标识
|
||||
ID string `json:"id" comment:"用户唯一标识"`
|
||||
Phone string `json:"phone" comment:"手机号码(登录账号)"`
|
||||
Password string `json:"password" comment:"登录密码(加密存储)"`
|
||||
UserType string `json:"user_type" comment:"用户类型"`
|
||||
Username string `json:"username" comment:"用户名"`
|
||||
|
||||
Active bool `gorm:"default:true" json:"is_active" comment:"账户是否激活"`
|
||||
LastLoginAt *time.Time `json:"last_login_at" comment:"最后登录时间"`
|
||||
LoginCount int `gorm:"default:0" json:"login_count" comment:"登录次数统计"`
|
||||
Permissions string `gorm:"type:text" json:"permissions" comment:"权限列表(JSON格式存储)"`
|
||||
|
||||
// 时间戳字段
|
||||
CreatedAt time.Time `json:"created_at" comment:"创建时间"`
|
||||
@@ -322,24 +368,38 @@ type UserCache struct {
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" comment:"软删除时间"`
|
||||
}
|
||||
|
||||
// ToCache 转换为缓存结构体
|
||||
// ToCache 转换为缓存结构
|
||||
func (u *User) ToCache() *UserCache {
|
||||
return &UserCache{
|
||||
ID: u.ID,
|
||||
Phone: u.Phone,
|
||||
Password: u.Password,
|
||||
UserType: u.UserType,
|
||||
Username: u.Username,
|
||||
CreatedAt: u.CreatedAt,
|
||||
UpdatedAt: u.UpdatedAt,
|
||||
DeletedAt: u.DeletedAt,
|
||||
// 补充所有字段
|
||||
// 管理员特有字段
|
||||
Active: u.Active,
|
||||
LastLoginAt: u.LastLoginAt,
|
||||
LoginCount: u.LoginCount,
|
||||
Permissions: u.Permissions,
|
||||
}
|
||||
}
|
||||
|
||||
// FromCache 从缓存结构体转换
|
||||
// FromCache 从缓存结构恢复
|
||||
func (u *User) FromCache(cache *UserCache) {
|
||||
u.ID = cache.ID
|
||||
u.Phone = cache.Phone
|
||||
u.Password = cache.Password
|
||||
u.UserType = cache.UserType
|
||||
u.Username = cache.Username
|
||||
u.CreatedAt = cache.CreatedAt
|
||||
u.UpdatedAt = cache.UpdatedAt
|
||||
u.DeletedAt = cache.DeletedAt
|
||||
u.Active = cache.Active
|
||||
u.LastLoginAt = cache.LastLoginAt
|
||||
u.LoginCount = cache.LoginCount
|
||||
u.Permissions = cache.Permissions
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user