基础架构
This commit is contained in:
108
internal/domains/user/entities/enterprise_info.go
Normal file
108
internal/domains/user/entities/enterprise_info.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package entities
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// EnterpriseInfo 企业信息实体
|
||||
// 存储用户在认证过程中验证后的企业信息,认证完成后不可修改
|
||||
// 与用户是一对一关系,每个用户最多对应一个企业信息
|
||||
type EnterpriseInfo struct {
|
||||
// 基础标识
|
||||
ID string `gorm:"primaryKey;type:varchar(36)" json:"id" comment:"企业信息唯一标识"`
|
||||
UserID string `gorm:"type:varchar(36);not null;uniqueIndex" json:"user_id" comment:"关联用户ID"`
|
||||
|
||||
// 企业四要素 - 企业认证的核心信息
|
||||
CompanyName string `gorm:"type:varchar(255);not null" json:"company_name" comment:"企业名称"`
|
||||
UnifiedSocialCode string `gorm:"type:varchar(50);not null;index" json:"unified_social_code" comment:"统一社会信用代码"`
|
||||
LegalPersonName string `gorm:"type:varchar(100);not null" json:"legal_person_name" comment:"法定代表人姓名"`
|
||||
LegalPersonID string `gorm:"type:varchar(50);not null" json:"legal_person_id" comment:"法定代表人身份证号"`
|
||||
|
||||
// 认证状态 - 各环节的验证结果
|
||||
IsOCRVerified bool `gorm:"default:false" json:"is_ocr_verified" comment:"OCR验证是否通过"`
|
||||
IsFaceVerified bool `gorm:"default:false" json:"is_face_verified" comment:"人脸识别是否通过"`
|
||||
IsCertified bool `gorm:"default:false" json:"is_certified" comment:"是否已完成认证"`
|
||||
VerificationData string `gorm:"type:text" json:"verification_data,omitempty" comment:"验证数据(JSON格式)"`
|
||||
|
||||
// OCR识别结果 - 从营业执照中自动识别的信息
|
||||
OCRRawData string `gorm:"type:text" json:"ocr_raw_data,omitempty" comment:"OCR原始返回数据(JSON格式)"`
|
||||
OCRConfidence float64 `gorm:"type:decimal(5,2)" json:"ocr_confidence,omitempty" comment:"OCR识别置信度(0-1)"`
|
||||
|
||||
// 认证完成时间
|
||||
CertifiedAt *time.Time `json:"certified_at,omitempty" comment:"认证完成时间"`
|
||||
|
||||
// 时间戳字段
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at" comment:"创建时间"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at" comment:"更新时间"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-" comment:"软删除时间"`
|
||||
|
||||
// 关联关系
|
||||
User *User `gorm:"foreignKey:UserID" json:"user,omitempty" comment:"关联的用户信息"`
|
||||
}
|
||||
|
||||
// TableName 指定数据库表名
|
||||
func (EnterpriseInfo) TableName() string {
|
||||
return "enterprise_infos"
|
||||
}
|
||||
|
||||
// IsComplete 检查企业四要素是否完整
|
||||
// 验证企业名称、统一社会信用代码、法定代表人姓名、身份证号是否都已填写
|
||||
func (e *EnterpriseInfo) IsComplete() bool {
|
||||
return e.CompanyName != "" &&
|
||||
e.UnifiedSocialCode != "" &&
|
||||
e.LegalPersonName != "" &&
|
||||
e.LegalPersonID != ""
|
||||
}
|
||||
|
||||
// Validate 验证企业信息是否有效
|
||||
// 这里可以添加企业信息的业务验证逻辑
|
||||
// 比如统一社会信用代码格式验证、身份证号格式验证等
|
||||
func (e *EnterpriseInfo) Validate() error {
|
||||
if !e.IsComplete() {
|
||||
return fmt.Errorf("企业信息不完整")
|
||||
}
|
||||
// 这里可以添加企业信息的业务验证逻辑
|
||||
// 比如统一社会信用代码格式验证、身份证号格式验证等
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsFullyVerified 检查是否已完成所有验证
|
||||
func (e *EnterpriseInfo) IsFullyVerified() bool {
|
||||
return e.IsOCRVerified && e.IsFaceVerified && e.IsCertified
|
||||
}
|
||||
|
||||
// UpdateOCRVerification 更新OCR验证状态
|
||||
func (e *EnterpriseInfo) UpdateOCRVerification(isVerified bool, rawData string, confidence float64) {
|
||||
e.IsOCRVerified = isVerified
|
||||
e.OCRRawData = rawData
|
||||
e.OCRConfidence = confidence
|
||||
}
|
||||
|
||||
// UpdateFaceVerification 更新人脸识别验证状态
|
||||
func (e *EnterpriseInfo) UpdateFaceVerification(isVerified bool) {
|
||||
e.IsFaceVerified = isVerified
|
||||
}
|
||||
|
||||
// CompleteCertification 完成认证
|
||||
func (e *EnterpriseInfo) CompleteCertification() {
|
||||
e.IsCertified = true
|
||||
now := time.Now()
|
||||
e.CertifiedAt = &now
|
||||
}
|
||||
|
||||
// IsReadOnly 检查企业信息是否只读(认证完成后不可修改)
|
||||
func (e *EnterpriseInfo) IsReadOnly() bool {
|
||||
return e.IsCertified
|
||||
}
|
||||
|
||||
// BeforeCreate GORM钩子:创建前自动生成UUID
|
||||
func (e *EnterpriseInfo) BeforeCreate(tx *gorm.DB) error {
|
||||
if e.ID == "" {
|
||||
e.ID = uuid.New().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -3,32 +3,35 @@ package entities
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// SMSCode 短信验证码记录实体
|
||||
// 记录用户发送的所有短信验证码,支持多种使用场景
|
||||
// 包含验证码的有效期管理、使用状态跟踪、安全审计等功能
|
||||
// @Description 短信验证码记录实体
|
||||
type SMSCode struct {
|
||||
// 基础标识
|
||||
ID string `gorm:"primaryKey;type:varchar(36)" json:"id" comment:"短信验证码记录唯一标识"`
|
||||
Phone string `gorm:"type:varchar(20);not null;index" json:"phone" comment:"接收手机号"`
|
||||
ID string `gorm:"primaryKey;type:varchar(36)" json:"id" comment:"短信验证码记录唯一标识" example:"123e4567-e89b-12d3-a456-426614174000"`
|
||||
Phone string `gorm:"type:varchar(20);not null;index" json:"phone" comment:"接收手机号" example:"13800138000"`
|
||||
Code string `gorm:"type:varchar(10);not null" json:"-" comment:"验证码内容(不返回给前端)"`
|
||||
Scene SMSScene `gorm:"type:varchar(20);not null" json:"scene" comment:"使用场景"`
|
||||
Used bool `gorm:"default:false" json:"used" comment:"是否已使用"`
|
||||
ExpiresAt time.Time `gorm:"not null" json:"expires_at" comment:"过期时间"`
|
||||
Scene SMSScene `gorm:"type:varchar(20);not null" json:"scene" comment:"使用场景" example:"register"`
|
||||
Used bool `gorm:"default:false" json:"used" comment:"是否已使用" example:"false"`
|
||||
ExpiresAt time.Time `gorm:"not null" json:"expires_at" comment:"过期时间" example:"2024-01-01T00:05:00Z"`
|
||||
UsedAt *time.Time `json:"used_at,omitempty" comment:"使用时间"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at" comment:"创建时间"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at" comment:"更新时间"`
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at" comment:"创建时间" example:"2024-01-01T00:00:00Z"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at" comment:"更新时间" example:"2024-01-01T00:00:00Z"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-" comment:"软删除时间"`
|
||||
|
||||
// 额外信息 - 安全审计相关数据
|
||||
IP string `gorm:"type:varchar(45)" json:"ip" comment:"发送IP地址"`
|
||||
UserAgent string `gorm:"type:varchar(500)" json:"user_agent" comment:"客户端信息"`
|
||||
IP string `gorm:"type:varchar(45)" json:"ip" comment:"发送IP地址" example:"192.168.1.1"`
|
||||
UserAgent string `gorm:"type:varchar(500)" json:"user_agent" comment:"客户端信息" example:"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"`
|
||||
}
|
||||
|
||||
// SMSScene 短信验证码使用场景枚举
|
||||
// 定义系统中所有需要使用短信验证码的业务场景
|
||||
// @Description 短信验证码使用场景
|
||||
type SMSScene string
|
||||
|
||||
const (
|
||||
@@ -40,6 +43,14 @@ const (
|
||||
SMSSceneUnbind SMSScene = "unbind" // 解绑手机号 - 解绑当前手机号
|
||||
)
|
||||
|
||||
// BeforeCreate GORM钩子:创建前自动生成UUID
|
||||
func (s *SMSCode) BeforeCreate(tx *gorm.DB) error {
|
||||
if s.ID == "" {
|
||||
s.ID = uuid.New().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 实现 Entity 接口 - 提供统一的实体管理接口
|
||||
// GetID 获取实体唯一标识
|
||||
func (s *SMSCode) GetID() string {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -23,6 +24,17 @@ type User struct {
|
||||
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at" comment:"创建时间"`
|
||||
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at" comment:"更新时间"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-" comment:"软删除时间"`
|
||||
|
||||
// 关联关系
|
||||
EnterpriseInfo *EnterpriseInfo `gorm:"foreignKey:UserID" json:"enterprise_info,omitempty" comment:"企业信息(认证后获得)"`
|
||||
}
|
||||
|
||||
// BeforeCreate GORM钩子:创建前自动生成UUID
|
||||
func (u *User) BeforeCreate(tx *gorm.DB) error {
|
||||
if u.ID == "" {
|
||||
u.ID = uuid.New().String()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 实现 Entity 接口 - 提供统一的实体管理接口
|
||||
@@ -273,3 +285,39 @@ func IsValidationError(err error) bool {
|
||||
var validationErr *ValidationError
|
||||
return errors.As(err, &validationErr)
|
||||
}
|
||||
|
||||
// UserCache 用户缓存结构体
|
||||
// 专门用于缓存序列化,包含Password字段
|
||||
type UserCache struct {
|
||||
// 基础标识
|
||||
ID string `json:"id" comment:"用户唯一标识"`
|
||||
Phone string `json:"phone" comment:"手机号码(登录账号)"`
|
||||
Password string `json:"password" comment:"登录密码(加密存储)"`
|
||||
|
||||
// 时间戳字段
|
||||
CreatedAt time.Time `json:"created_at" comment:"创建时间"`
|
||||
UpdatedAt time.Time `json:"updated_at" comment:"更新时间"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at" comment:"软删除时间"`
|
||||
}
|
||||
|
||||
// ToCache 转换为缓存结构体
|
||||
func (u *User) ToCache() *UserCache {
|
||||
return &UserCache{
|
||||
ID: u.ID,
|
||||
Phone: u.Phone,
|
||||
Password: u.Password,
|
||||
CreatedAt: u.CreatedAt,
|
||||
UpdatedAt: u.UpdatedAt,
|
||||
DeletedAt: u.DeletedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// FromCache 从缓存结构体转换
|
||||
func (u *User) FromCache(cache *UserCache) {
|
||||
u.ID = cache.ID
|
||||
u.Phone = cache.Phone
|
||||
u.Password = cache.Password
|
||||
u.CreatedAt = cache.CreatedAt
|
||||
u.UpdatedAt = cache.UpdatedAt
|
||||
u.DeletedAt = cache.DeletedAt
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user