v0.1
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -18,7 +19,7 @@ const (
|
||||
UserTypeAdmin UserType = "admin" // 管理员
|
||||
)
|
||||
|
||||
// User 用户实体
|
||||
// User 用户聚合根
|
||||
// 系统用户的核心信息,提供基础的账户管理功能
|
||||
// 支持手机号登录,密码加密存储,实现Entity接口便于统一管理
|
||||
type User struct {
|
||||
@@ -33,6 +34,7 @@ type User struct {
|
||||
|
||||
// 管理员特有字段
|
||||
Active bool `gorm:"default:true" json:"is_active" comment:"账户是否激活"`
|
||||
IsCertified bool `gorm:"default:false" json:"is_certified" 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格式存储)"`
|
||||
@@ -44,6 +46,9 @@ type User struct {
|
||||
|
||||
// 关联关系
|
||||
EnterpriseInfo *EnterpriseInfo `gorm:"foreignKey:UserID" json:"enterprise_info,omitempty" comment:"企业信息(认证后获得)"`
|
||||
|
||||
// 领域事件 (不持久化)
|
||||
domainEvents []interface{} `gorm:"-" json:"-"`
|
||||
}
|
||||
|
||||
// BeforeCreate GORM钩子:创建前自动生成UUID
|
||||
@@ -85,14 +90,305 @@ func (u *User) Validate() error {
|
||||
return NewValidationError("手机号格式无效")
|
||||
}
|
||||
|
||||
// 验证密码强度
|
||||
if err := u.validatePasswordStrength(u.Password); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CompleteCertification 完成认证
|
||||
func (u *User) CompleteCertification() error {
|
||||
u.IsCertified = true
|
||||
return nil
|
||||
}
|
||||
|
||||
// ================ 企业信息管理方法 ================
|
||||
|
||||
// CreateEnterpriseInfo 创建企业信息
|
||||
func (u *User) CreateEnterpriseInfo(companyName, unifiedSocialCode, legalPersonName, legalPersonID, legalPersonPhone, enterpriseAddress, enterpriseEmail string) error {
|
||||
// 检查是否已有企业信息
|
||||
if u.EnterpriseInfo != nil {
|
||||
return fmt.Errorf("用户已有企业信息")
|
||||
}
|
||||
|
||||
// 创建企业信息实体
|
||||
enterpriseInfo, err := NewEnterpriseInfo(u.ID, companyName, unifiedSocialCode, legalPersonName, legalPersonID, legalPersonPhone, enterpriseAddress, enterpriseEmail)
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建企业信息失败: %w", err)
|
||||
}
|
||||
|
||||
// 设置关联关系
|
||||
u.EnterpriseInfo = enterpriseInfo
|
||||
|
||||
// 添加领域事件
|
||||
u.addDomainEvent(&UserEnterpriseInfoCreatedEvent{
|
||||
UserID: u.ID,
|
||||
EnterpriseInfoID: enterpriseInfo.ID,
|
||||
CompanyName: companyName,
|
||||
UnifiedSocialCode: unifiedSocialCode,
|
||||
CreatedAt: time.Now(),
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateEnterpriseInfo 更新企业信息
|
||||
func (u *User) UpdateEnterpriseInfo(companyName, unifiedSocialCode, legalPersonName, legalPersonID, legalPersonPhone, enterpriseAddress, enterpriseEmail string) error {
|
||||
// 检查是否有企业信息
|
||||
if u.EnterpriseInfo == nil {
|
||||
return fmt.Errorf("用户暂无企业信息")
|
||||
}
|
||||
|
||||
// 记录原始值用于事件
|
||||
oldCompanyName := u.EnterpriseInfo.CompanyName
|
||||
oldUnifiedSocialCode := u.EnterpriseInfo.UnifiedSocialCode
|
||||
|
||||
// 更新企业信息
|
||||
err := u.EnterpriseInfo.UpdateEnterpriseInfo(companyName, unifiedSocialCode, legalPersonName, legalPersonID, legalPersonPhone, enterpriseAddress, enterpriseEmail)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 添加领域事件
|
||||
u.addDomainEvent(&UserEnterpriseInfoUpdatedEvent{
|
||||
UserID: u.ID,
|
||||
EnterpriseInfoID: u.EnterpriseInfo.ID,
|
||||
OldCompanyName: oldCompanyName,
|
||||
NewCompanyName: companyName,
|
||||
OldUnifiedSocialCode: oldUnifiedSocialCode,
|
||||
NewUnifiedSocialCode: unifiedSocialCode,
|
||||
UpdatedAt: time.Now(),
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetEnterpriseInfo 获取企业信息
|
||||
func (u *User) GetEnterpriseInfo() *EnterpriseInfo {
|
||||
return u.EnterpriseInfo
|
||||
}
|
||||
|
||||
// HasEnterpriseInfo 检查是否有企业信息
|
||||
func (u *User) HasEnterpriseInfo() bool {
|
||||
return u.EnterpriseInfo != nil
|
||||
}
|
||||
|
||||
// RemoveEnterpriseInfo 移除企业信息
|
||||
func (u *User) RemoveEnterpriseInfo() error {
|
||||
if u.EnterpriseInfo == nil {
|
||||
return fmt.Errorf("用户暂无企业信息")
|
||||
}
|
||||
|
||||
enterpriseInfoID := u.EnterpriseInfo.ID
|
||||
u.EnterpriseInfo = nil
|
||||
|
||||
// 添加领域事件
|
||||
u.addDomainEvent(&UserEnterpriseInfoRemovedEvent{
|
||||
UserID: u.ID,
|
||||
EnterpriseInfoID: enterpriseInfoID,
|
||||
RemovedAt: time.Now(),
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateEnterpriseInfo 验证企业信息
|
||||
func (u *User) ValidateEnterpriseInfo() error {
|
||||
if u.EnterpriseInfo == nil {
|
||||
return fmt.Errorf("用户暂无企业信息")
|
||||
}
|
||||
|
||||
return u.EnterpriseInfo.ValidateBusinessRules()
|
||||
}
|
||||
|
||||
// ================ 聚合根核心方法 ================
|
||||
|
||||
// Register 用户注册
|
||||
func (u *User) Register() error {
|
||||
// 验证用户信息
|
||||
if err := u.Validate(); err != nil {
|
||||
return fmt.Errorf("用户信息验证失败: %w", err)
|
||||
}
|
||||
|
||||
// 添加领域事件
|
||||
u.addDomainEvent(&UserRegisteredEvent{
|
||||
UserID: u.ID,
|
||||
Phone: u.Phone,
|
||||
UserType: u.UserType,
|
||||
CreatedAt: time.Now(),
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Login 用户登录
|
||||
func (u *User) Login(ipAddress, userAgent string) error {
|
||||
// 检查用户是否可以登录
|
||||
if !u.CanLogin() {
|
||||
return fmt.Errorf("用户无法登录")
|
||||
}
|
||||
|
||||
// 更新登录信息
|
||||
u.UpdateLastLoginAt()
|
||||
u.IncrementLoginCount()
|
||||
|
||||
// 添加领域事件
|
||||
u.addDomainEvent(&UserLoggedInEvent{
|
||||
UserID: u.ID,
|
||||
Phone: u.Phone,
|
||||
IPAddress: ipAddress,
|
||||
UserAgent: userAgent,
|
||||
LoginAt: time.Now(),
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ChangePassword 修改密码
|
||||
func (u *User) ChangePassword(oldPassword, newPassword, confirmPassword string) error {
|
||||
// 验证旧密码
|
||||
if !u.CheckPassword(oldPassword) {
|
||||
return fmt.Errorf("原密码错误")
|
||||
}
|
||||
|
||||
// 验证新密码
|
||||
if newPassword != confirmPassword {
|
||||
return fmt.Errorf("两次输入的密码不一致")
|
||||
}
|
||||
|
||||
// 设置新密码
|
||||
if err := u.SetPassword(newPassword); err != nil {
|
||||
return fmt.Errorf("设置新密码失败: %w", err)
|
||||
}
|
||||
|
||||
// 添加领域事件
|
||||
u.addDomainEvent(&UserPasswordChangedEvent{
|
||||
UserID: u.ID,
|
||||
Phone: u.Phone,
|
||||
ChangedAt: time.Now(),
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ActivateUser 激活用户
|
||||
func (u *User) ActivateUser() error {
|
||||
if u.Active {
|
||||
return fmt.Errorf("用户已经是激活状态")
|
||||
}
|
||||
|
||||
u.Activate()
|
||||
|
||||
// 添加领域事件
|
||||
u.addDomainEvent(&UserActivatedEvent{
|
||||
UserID: u.ID,
|
||||
Phone: u.Phone,
|
||||
ActivatedAt: time.Now(),
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeactivateUser 停用用户
|
||||
func (u *User) DeactivateUser() error {
|
||||
if !u.Active {
|
||||
return fmt.Errorf("用户已经是停用状态")
|
||||
}
|
||||
|
||||
u.Deactivate()
|
||||
|
||||
// 添加领域事件
|
||||
u.addDomainEvent(&UserDeactivatedEvent{
|
||||
UserID: u.ID,
|
||||
Phone: u.Phone,
|
||||
DeactivatedAt: time.Now(),
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ================ 业务规则验证 ================
|
||||
|
||||
// ValidateBusinessRules 验证业务规则
|
||||
func (u *User) ValidateBusinessRules() error {
|
||||
// 1. 基础字段验证
|
||||
if err := u.validateBasicFields(); err != nil {
|
||||
return fmt.Errorf("基础字段验证失败: %w", err)
|
||||
}
|
||||
|
||||
// 2. 业务规则验证
|
||||
if err := u.validateBusinessLogic(); err != nil {
|
||||
return fmt.Errorf("业务规则验证失败: %w", err)
|
||||
}
|
||||
|
||||
// 3. 状态一致性验证
|
||||
if err := u.validateStateConsistency(); err != nil {
|
||||
return fmt.Errorf("状态一致性验证失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateBasicFields 验证基础字段
|
||||
func (u *User) validateBasicFields() error {
|
||||
if u.Phone == "" {
|
||||
return fmt.Errorf("手机号不能为空")
|
||||
}
|
||||
if u.Password == "" {
|
||||
return fmt.Errorf("密码不能为空")
|
||||
}
|
||||
|
||||
// 验证手机号格式
|
||||
if !u.IsValidPhone() {
|
||||
return fmt.Errorf("手机号格式无效")
|
||||
}
|
||||
|
||||
// 不对加密后的hash做长度校验
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateBusinessLogic 验证业务逻辑
|
||||
func (u *User) validateBusinessLogic() error {
|
||||
// 管理员用户必须有用户名
|
||||
// if u.IsAdmin() && u.Username == "" {
|
||||
// return fmt.Errorf("管理员用户必须有用户名")
|
||||
// }
|
||||
|
||||
// // 普通用户不能有用户名
|
||||
// if u.IsNormalUser() && u.Username != "" {
|
||||
// return fmt.Errorf("普通用户不能有用户名")
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// validateStateConsistency 验证状态一致性
|
||||
func (u *User) validateStateConsistency() error {
|
||||
// 如果用户被删除,不能是激活状态
|
||||
if u.IsDeleted() && u.Active {
|
||||
return fmt.Errorf("已删除用户不能是激活状态")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ================ 领域事件管理 ================
|
||||
|
||||
// addDomainEvent 添加领域事件
|
||||
func (u *User) addDomainEvent(event interface{}) {
|
||||
if u.domainEvents == nil {
|
||||
u.domainEvents = make([]interface{}, 0)
|
||||
}
|
||||
u.domainEvents = append(u.domainEvents, event)
|
||||
}
|
||||
|
||||
// GetDomainEvents 获取领域事件
|
||||
func (u *User) GetDomainEvents() []interface{} {
|
||||
return u.domainEvents
|
||||
}
|
||||
|
||||
// ClearDomainEvents 清除领域事件
|
||||
func (u *User) ClearDomainEvents() {
|
||||
u.domainEvents = make([]interface{}, 0)
|
||||
}
|
||||
|
||||
// ================ 业务方法 ================
|
||||
|
||||
// IsAdmin 检查是否为管理员
|
||||
@@ -131,81 +427,30 @@ func (u *User) Deactivate() {
|
||||
u.Active = false
|
||||
}
|
||||
|
||||
// ChangePassword 修改密码
|
||||
// 验证旧密码,检查新密码强度,更新密码
|
||||
func (u *User) ChangePassword(oldPassword, newPassword, confirmPassword string) error {
|
||||
// 1. 验证确认密码
|
||||
if newPassword != confirmPassword {
|
||||
return NewValidationError("新密码和确认新密码不匹配")
|
||||
}
|
||||
|
||||
// 2. 验证旧密码
|
||||
if !u.CheckPassword(oldPassword) {
|
||||
return NewValidationError("当前密码错误")
|
||||
}
|
||||
|
||||
// 3. 验证新密码强度
|
||||
if err := u.validatePasswordStrength(newPassword); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 4. 检查新密码不能与旧密码相同
|
||||
if u.CheckPassword(newPassword) {
|
||||
return NewValidationError("新密码不能与当前密码相同")
|
||||
}
|
||||
|
||||
// 5. 更新密码
|
||||
hashedPassword, err := u.hashPassword(newPassword)
|
||||
if err != nil {
|
||||
return fmt.Errorf("密码加密失败: %w", err)
|
||||
}
|
||||
u.Password = hashedPassword
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckPassword 验证密码是否正确
|
||||
func (u *User) CheckPassword(password string) bool {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
|
||||
return err == nil
|
||||
return u.Password == hashPassword(password)
|
||||
}
|
||||
|
||||
// SetPassword 设置密码(用于注册或重置密码)
|
||||
func (u *User) SetPassword(password string) error {
|
||||
// 验证密码强度
|
||||
// 只对明文做强度校验
|
||||
if err := u.validatePasswordStrength(password); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 加密密码
|
||||
hashedPassword, err := u.hashPassword(password)
|
||||
if err != nil {
|
||||
return fmt.Errorf("密码加密失败: %w", err)
|
||||
}
|
||||
|
||||
u.Password = hashedPassword
|
||||
u.Password = hashPassword(password)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResetPassword 重置密码(忘记密码时使用)
|
||||
func (u *User) ResetPassword(newPassword, confirmPassword string) error {
|
||||
// 1. 验证确认密码
|
||||
if newPassword != confirmPassword {
|
||||
return NewValidationError("新密码和确认新密码不匹配")
|
||||
}
|
||||
|
||||
// 2. 验证新密码强度
|
||||
if err := u.validatePasswordStrength(newPassword); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 3. 更新密码
|
||||
hashedPassword, err := u.hashPassword(newPassword)
|
||||
if err != nil {
|
||||
return fmt.Errorf("密码加密失败: %w", err)
|
||||
}
|
||||
u.Password = hashedPassword
|
||||
|
||||
u.Password = hashPassword(newPassword)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -265,16 +510,14 @@ func (u *User) GetMaskedPhone() string {
|
||||
|
||||
// ================ 私有方法 ================
|
||||
|
||||
// hashPassword 加密密码
|
||||
func (u *User) hashPassword(password string) (string, error) {
|
||||
hashedBytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(hashedBytes), nil
|
||||
// hashPassword 使用sha256+hex加密密码
|
||||
func hashPassword(password string) string {
|
||||
h := sha256.New()
|
||||
h.Write([]byte(password))
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
|
||||
// validatePasswordStrength 验证密码强度
|
||||
// validatePasswordStrength 只对明文做长度/强度校验
|
||||
func (u *User) validatePasswordStrength(password string) error {
|
||||
if len(password) < 6 {
|
||||
return NewValidationError("密码长度不能少于6位")
|
||||
@@ -347,59 +590,69 @@ func IsValidationError(err error) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
// ================ 缓存相关 ================
|
||||
// ================ 领域事件定义 ================
|
||||
|
||||
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:"创建时间"`
|
||||
UpdatedAt time.Time `json:"updated_at" comment:"更新时间"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" comment:"软删除时间"`
|
||||
// UserRegisteredEvent 用户注册事件
|
||||
type UserRegisteredEvent struct {
|
||||
UserID string `json:"user_id"`
|
||||
Phone string `json:"phone"`
|
||||
UserType string `json:"user_type"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// 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,
|
||||
}
|
||||
// UserLoggedInEvent 用户登录事件
|
||||
type UserLoggedInEvent struct {
|
||||
UserID string `json:"user_id"`
|
||||
Phone string `json:"phone"`
|
||||
IPAddress string `json:"ip_address"`
|
||||
UserAgent string `json:"user_agent"`
|
||||
LoginAt time.Time `json:"login_at"`
|
||||
}
|
||||
|
||||
// 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
|
||||
// UserPasswordChangedEvent 用户密码修改事件
|
||||
type UserPasswordChangedEvent struct {
|
||||
UserID string `json:"user_id"`
|
||||
Phone string `json:"phone"`
|
||||
ChangedAt time.Time `json:"changed_at"`
|
||||
}
|
||||
|
||||
// UserActivatedEvent 用户激活事件
|
||||
type UserActivatedEvent struct {
|
||||
UserID string `json:"user_id"`
|
||||
Phone string `json:"phone"`
|
||||
ActivatedAt time.Time `json:"activated_at"`
|
||||
}
|
||||
|
||||
// UserDeactivatedEvent 用户停用事件
|
||||
type UserDeactivatedEvent struct {
|
||||
UserID string `json:"user_id"`
|
||||
Phone string `json:"phone"`
|
||||
DeactivatedAt time.Time `json:"deactivated_at"`
|
||||
}
|
||||
|
||||
// UserEnterpriseInfoCreatedEvent 企业信息创建事件
|
||||
type UserEnterpriseInfoCreatedEvent struct {
|
||||
UserID string `json:"user_id"`
|
||||
EnterpriseInfoID string `json:"enterprise_info_id"`
|
||||
CompanyName string `json:"company_name"`
|
||||
UnifiedSocialCode string `json:"unified_social_code"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// UserEnterpriseInfoUpdatedEvent 企业信息更新事件
|
||||
type UserEnterpriseInfoUpdatedEvent struct {
|
||||
UserID string `json:"user_id"`
|
||||
EnterpriseInfoID string `json:"enterprise_info_id"`
|
||||
OldCompanyName string `json:"old_company_name"`
|
||||
NewCompanyName string `json:"new_company_name"`
|
||||
OldUnifiedSocialCode string `json:"old_unified_social_code"`
|
||||
NewUnifiedSocialCode string `json:"new_unified_social_code"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// UserEnterpriseInfoRemovedEvent 企业信息移除事件
|
||||
type UserEnterpriseInfoRemovedEvent struct {
|
||||
UserID string `json:"user_id"`
|
||||
EnterpriseInfoID string `json:"enterprise_info_id"`
|
||||
RemovedAt time.Time `json:"removed_at"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user