temp
This commit is contained in:
@@ -107,11 +107,6 @@ func (s *EnterpriseService) UpdateEnterpriseInfo(ctx context.Context, userID, co
|
||||
return nil, fmt.Errorf("企业信息不存在: %w", err)
|
||||
}
|
||||
|
||||
// 检查企业信息是否已认证完成(认证完成后不可修改)
|
||||
if enterpriseInfo.IsReadOnly() {
|
||||
return nil, fmt.Errorf("企业信息已认证完成,不可修改")
|
||||
}
|
||||
|
||||
// 检查统一社会信用代码是否已被其他用户使用
|
||||
if unifiedSocialCode != enterpriseInfo.UnifiedSocialCode {
|
||||
exists, err := s.enterpriseInfoRepo.CheckUnifiedSocialCodeExists(ctx, unifiedSocialCode, userID)
|
||||
@@ -142,55 +137,6 @@ func (s *EnterpriseService) UpdateEnterpriseInfo(ctx context.Context, userID, co
|
||||
return enterpriseInfo, nil
|
||||
}
|
||||
|
||||
// UpdateOCRVerification 更新OCR验证状态
|
||||
func (s *EnterpriseService) UpdateOCRVerification(ctx context.Context, userID string, isVerified bool, rawData string, confidence float64) error {
|
||||
// 获取企业信息
|
||||
enterpriseInfo, err := s.enterpriseInfoRepo.GetByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("企业信息不存在: %w", err)
|
||||
}
|
||||
|
||||
// 更新OCR验证状态
|
||||
enterpriseInfo.UpdateOCRVerification(isVerified, rawData, confidence)
|
||||
|
||||
if err := s.enterpriseInfoRepo.Update(ctx, *enterpriseInfo); err != nil {
|
||||
s.logger.Error("更新OCR验证状态失败", zap.Error(err))
|
||||
return fmt.Errorf("更新OCR验证状态失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("OCR验证状态更新成功",
|
||||
zap.String("user_id", userID),
|
||||
zap.Bool("is_verified", isVerified),
|
||||
zap.Float64("confidence", confidence),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateFaceVerification 更新人脸识别验证状态
|
||||
func (s *EnterpriseService) UpdateFaceVerification(ctx context.Context, userID string, isVerified bool) error {
|
||||
// 获取企业信息
|
||||
enterpriseInfo, err := s.enterpriseInfoRepo.GetByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("企业信息不存在: %w", err)
|
||||
}
|
||||
|
||||
// 更新人脸识别验证状态
|
||||
enterpriseInfo.UpdateFaceVerification(isVerified)
|
||||
|
||||
if err := s.enterpriseInfoRepo.Update(ctx, *enterpriseInfo); err != nil {
|
||||
s.logger.Error("更新人脸识别验证状态失败", zap.Error(err))
|
||||
return fmt.Errorf("更新人脸识别验证状态失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("人脸识别验证状态更新成功",
|
||||
zap.String("user_id", userID),
|
||||
zap.Bool("is_verified", isVerified),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CompleteEnterpriseCertification 完成企业认证
|
||||
func (s *EnterpriseService) CompleteEnterpriseCertification(ctx context.Context, userID string) error {
|
||||
// 获取企业信息
|
||||
@@ -199,14 +145,6 @@ func (s *EnterpriseService) CompleteEnterpriseCertification(ctx context.Context,
|
||||
return fmt.Errorf("企业信息不存在: %w", err)
|
||||
}
|
||||
|
||||
// 检查是否已完成所有验证
|
||||
if !enterpriseInfo.IsOCRVerified || !enterpriseInfo.IsFaceVerified {
|
||||
return fmt.Errorf("企业信息验证未完成,无法完成认证")
|
||||
}
|
||||
|
||||
// 完成认证
|
||||
enterpriseInfo.CompleteCertification()
|
||||
|
||||
if err := s.enterpriseInfoRepo.Update(ctx, *enterpriseInfo); err != nil {
|
||||
s.logger.Error("完成企业认证失败", zap.Error(err))
|
||||
return fmt.Errorf("完成企业认证失败: %w", err)
|
||||
@@ -264,17 +202,6 @@ func (s *EnterpriseService) GetEnterpriseInfoByUnifiedSocialCode(ctx context.Con
|
||||
return s.enterpriseInfoRepo.GetByUnifiedSocialCode(ctx, unifiedSocialCode)
|
||||
}
|
||||
|
||||
// IsEnterpriseCertified 检查用户是否已完成企业认证
|
||||
func (s *EnterpriseService) IsEnterpriseCertified(ctx context.Context, userID string) (bool, error) {
|
||||
enterpriseInfo, err := s.enterpriseInfoRepo.GetByUserID(ctx, userID)
|
||||
if err != nil {
|
||||
// 没有企业信息,认为未认证
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return enterpriseInfo.IsFullyVerified(), nil
|
||||
}
|
||||
|
||||
// GetEnterpriseCertificationStatus 获取企业认证状态
|
||||
func (s *EnterpriseService) GetEnterpriseCertificationStatus(ctx context.Context, userID string) (map[string]interface{}, error) {
|
||||
enterpriseInfo, err := s.enterpriseInfoRepo.GetByUserID(ctx, userID)
|
||||
@@ -288,11 +215,6 @@ func (s *EnterpriseService) GetEnterpriseCertificationStatus(ctx context.Context
|
||||
|
||||
status := map[string]interface{}{
|
||||
"has_enterprise_info": true,
|
||||
"is_certified": enterpriseInfo.IsFullyVerified(),
|
||||
"is_readonly": enterpriseInfo.IsReadOnly(),
|
||||
"ocr_verified": enterpriseInfo.IsOCRVerified,
|
||||
"face_verified": enterpriseInfo.IsFaceVerified,
|
||||
"certified_at": enterpriseInfo.CertifiedAt,
|
||||
"company_name": enterpriseInfo.CompanyName,
|
||||
"unified_social_code": enterpriseInfo.UnifiedSocialCode,
|
||||
"legal_person_name": enterpriseInfo.LegalPersonName,
|
||||
|
||||
130
internal/domains/user/services/user_auth_service.go
Normal file
130
internal/domains/user/services/user_auth_service.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"tyapi-server/internal/domains/user/entities"
|
||||
"tyapi-server/internal/domains/user/repositories"
|
||||
)
|
||||
|
||||
// UserAuthService 用户认证领域服务
|
||||
// 负责用户认证相关的业务逻辑,包括密码验证、登录状态管理等
|
||||
type UserAuthService struct {
|
||||
userRepo repositories.UserRepository
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewUserAuthService 创建用户认证领域服务
|
||||
func NewUserAuthService(
|
||||
userRepo repositories.UserRepository,
|
||||
logger *zap.Logger,
|
||||
) *UserAuthService {
|
||||
return &UserAuthService{
|
||||
userRepo: userRepo,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// ValidatePassword 验证用户密码
|
||||
func (s *UserAuthService) ValidatePassword(ctx context.Context, phone, password string) (*entities.User, error) {
|
||||
user, err := s.userRepo.GetByPhone(ctx, phone)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("用户名或密码错误")
|
||||
}
|
||||
|
||||
if !user.CanLogin() {
|
||||
return nil, fmt.Errorf("用户状态异常,无法登录")
|
||||
}
|
||||
|
||||
if !user.CheckPassword(password) {
|
||||
return nil, fmt.Errorf("用户名或密码错误")
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// ValidateUserLogin 验证用户登录状态
|
||||
func (s *UserAuthService) ValidateUserLogin(ctx context.Context, phone string) (*entities.User, error) {
|
||||
user, err := s.userRepo.GetByPhone(ctx, phone)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("用户不存在")
|
||||
}
|
||||
|
||||
if !user.CanLogin() {
|
||||
return nil, fmt.Errorf("用户状态异常,无法登录")
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// ChangePassword 修改用户密码
|
||||
func (s *UserAuthService) ChangePassword(ctx context.Context, userID, oldPassword, newPassword string) error {
|
||||
user, err := s.userRepo.GetByID(ctx, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("用户不存在: %w", err)
|
||||
}
|
||||
|
||||
if err := user.ChangePassword(oldPassword, newPassword, newPassword); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.userRepo.Update(ctx, user); err != nil {
|
||||
s.logger.Error("密码修改失败", zap.Error(err))
|
||||
return fmt.Errorf("密码修改失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("密码修改成功",
|
||||
zap.String("user_id", userID),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResetPassword 重置用户密码
|
||||
func (s *UserAuthService) ResetPassword(ctx context.Context, phone, newPassword string) error {
|
||||
user, err := s.userRepo.GetByPhone(ctx, phone)
|
||||
if err != nil {
|
||||
return fmt.Errorf("用户不存在: %w", err)
|
||||
}
|
||||
|
||||
if err := user.ResetPassword(newPassword, newPassword); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.userRepo.Update(ctx, *user); err != nil {
|
||||
s.logger.Error("密码重置失败", zap.Error(err))
|
||||
return fmt.Errorf("密码重置失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("密码重置成功",
|
||||
zap.String("user_id", user.ID),
|
||||
zap.String("phone", user.Phone),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserPermissions 获取用户权限
|
||||
func (s *UserAuthService) GetUserPermissions(ctx context.Context, user *entities.User) ([]string, error) {
|
||||
if !user.IsAdmin() {
|
||||
return []string{}, nil
|
||||
}
|
||||
|
||||
// 这里可以根据用户角色返回不同的权限
|
||||
// 目前返回默认的管理员权限
|
||||
permissions := []string{
|
||||
"user:read",
|
||||
"user:write",
|
||||
"product:read",
|
||||
"product:write",
|
||||
"certification:read",
|
||||
"certification:write",
|
||||
"finance:read",
|
||||
"finance:write",
|
||||
}
|
||||
|
||||
return permissions, nil
|
||||
}
|
||||
128
internal/domains/user/services/user_management_service.go
Normal file
128
internal/domains/user/services/user_management_service.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"tyapi-server/internal/domains/user/entities"
|
||||
"tyapi-server/internal/domains/user/repositories"
|
||||
)
|
||||
|
||||
// UserManagementService 用户管理领域服务
|
||||
// 负责用户的基本管理操作,包括创建、查询、更新等
|
||||
type UserManagementService struct {
|
||||
userRepo repositories.UserRepository
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewUserManagementService 创建用户管理领域服务
|
||||
func NewUserManagementService(
|
||||
userRepo repositories.UserRepository,
|
||||
logger *zap.Logger,
|
||||
) *UserManagementService {
|
||||
return &UserManagementService{
|
||||
userRepo: userRepo,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateUser 创建用户
|
||||
func (s *UserManagementService) CreateUser(ctx context.Context, phone, password string) (*entities.User, error) {
|
||||
// 检查手机号是否已注册
|
||||
exists, err := s.IsPhoneRegistered(ctx, phone)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("检查手机号失败: %w", err)
|
||||
}
|
||||
if exists {
|
||||
return nil, fmt.Errorf("手机号已注册")
|
||||
}
|
||||
|
||||
// 创建用户
|
||||
user, err := entities.NewUser(phone, password)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建用户失败: %w", err)
|
||||
}
|
||||
|
||||
createdUser, err := s.userRepo.Create(ctx, *user)
|
||||
if err != nil {
|
||||
s.logger.Error("创建用户失败", zap.Error(err))
|
||||
return nil, fmt.Errorf("创建用户失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("用户创建成功",
|
||||
zap.String("user_id", user.ID),
|
||||
zap.String("phone", user.Phone),
|
||||
)
|
||||
|
||||
return &createdUser, nil
|
||||
}
|
||||
|
||||
// GetUserByID 根据ID获取用户信息
|
||||
func (s *UserManagementService) GetUserByID(ctx context.Context, userID string) (*entities.User, error) {
|
||||
user, err := s.userRepo.GetByID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("用户不存在: %w", err)
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
// GetUserByPhone 根据手机号获取用户信息
|
||||
func (s *UserManagementService) GetUserByPhone(ctx context.Context, phone string) (*entities.User, error) {
|
||||
user, err := s.userRepo.GetByPhone(ctx, phone)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("用户不存在: %w", err)
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// UpdateUser 更新用户信息
|
||||
func (s *UserManagementService) UpdateUser(ctx context.Context, user *entities.User) error {
|
||||
if err := s.userRepo.Update(ctx, *user); err != nil {
|
||||
s.logger.Error("更新用户信息失败", zap.Error(err))
|
||||
return fmt.Errorf("更新用户信息失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("用户信息更新成功",
|
||||
zap.String("user_id", user.ID),
|
||||
zap.String("phone", user.Phone),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsPhoneRegistered 检查手机号是否已注册
|
||||
func (s *UserManagementService) IsPhoneRegistered(ctx context.Context, phone string) (bool, error) {
|
||||
_, err := s.userRepo.GetByPhone(ctx, phone)
|
||||
if err != nil {
|
||||
return false, nil // 用户不存在,可以注册
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// ValidateUser 验证用户信息
|
||||
func (s *UserManagementService) ValidateUser(ctx context.Context, userID string) error {
|
||||
user, err := s.userRepo.GetByID(ctx, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("用户不存在: %w", err)
|
||||
}
|
||||
|
||||
// 这里可以添加更多的用户验证逻辑
|
||||
if user.Phone == "" {
|
||||
return fmt.Errorf("用户手机号不能为空")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateLoginStats 更新登录统计
|
||||
func (s *UserManagementService) UpdateLoginStats(ctx context.Context, userID string) error {
|
||||
if err := s.userRepo.UpdateLoginStats(ctx, userID); err != nil {
|
||||
s.logger.Error("更新登录统计失败", zap.Error(err))
|
||||
return fmt.Errorf("更新登录统计失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("登录统计更新成功", zap.String("user_id", userID))
|
||||
return nil
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"tyapi-server/internal/domains/user/entities"
|
||||
"tyapi-server/internal/domains/user/repositories"
|
||||
)
|
||||
|
||||
// UserService 用户领域服务
|
||||
type UserService struct {
|
||||
userRepo repositories.UserRepository
|
||||
enterpriseService *EnterpriseService
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewUserService 创建用户领域服务
|
||||
func NewUserService(
|
||||
userRepo repositories.UserRepository,
|
||||
enterpriseService *EnterpriseService,
|
||||
logger *zap.Logger,
|
||||
) *UserService {
|
||||
return &UserService{
|
||||
userRepo: userRepo,
|
||||
enterpriseService: enterpriseService,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// IsPhoneRegistered 检查手机号是否已注册
|
||||
func (s *UserService) IsPhoneRegistered(ctx context.Context, phone string) (bool, error) {
|
||||
_, err := s.userRepo.GetByPhone(ctx, phone)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GetUserWithEnterpriseInfo 获取用户信息(包含企业信息)
|
||||
func (s *UserService) GetUserWithEnterpriseInfo(ctx context.Context, userID string) (*entities.User, error) {
|
||||
// 通过企业服务获取用户信息(包含企业信息)
|
||||
return s.enterpriseService.GetUserWithEnterpriseInfo(ctx, userID)
|
||||
}
|
||||
|
||||
// GetUserByID 根据ID获取用户信息
|
||||
func (s *UserService) GetUserByID(ctx context.Context, userID string) (*entities.User, error) {
|
||||
user, err := s.userRepo.GetByID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("用户不存在: %w", err)
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
// GetUserByPhone 根据手机号获取用户信息
|
||||
func (s *UserService) GetUserByPhone(ctx context.Context, phone string) (*entities.User, error) {
|
||||
user, err := s.userRepo.GetByPhone(ctx, phone)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("用户不存在: %w", err)
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// UpdateUser 更新用户信息
|
||||
func (s *UserService) UpdateUser(ctx context.Context, user *entities.User) error {
|
||||
if err := s.userRepo.Update(ctx, *user); err != nil {
|
||||
s.logger.Error("更新用户信息失败", zap.Error(err))
|
||||
return fmt.Errorf("更新用户信息失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("用户信息更新成功",
|
||||
zap.String("user_id", user.ID),
|
||||
zap.String("phone", user.Phone),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ChangePassword 修改用户密码
|
||||
func (s *UserService) ChangePassword(ctx context.Context, userID, oldPassword, newPassword string) error {
|
||||
user, err := s.userRepo.GetByID(ctx, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("用户不存在: %w", err)
|
||||
}
|
||||
|
||||
if err := user.ChangePassword(oldPassword, newPassword, newPassword); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.userRepo.Update(ctx, user); err != nil {
|
||||
s.logger.Error("密码修改失败", zap.Error(err))
|
||||
return fmt.Errorf("密码修改失败: %w", err)
|
||||
}
|
||||
|
||||
s.logger.Info("密码修改成功",
|
||||
zap.String("user_id", userID),
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateUser 验证用户信息
|
||||
func (s *UserService) ValidateUser(ctx context.Context, userID string) error {
|
||||
user, err := s.userRepo.GetByID(ctx, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("用户不存在: %w", err)
|
||||
}
|
||||
|
||||
// 这里可以添加更多的用户验证逻辑
|
||||
if user.Phone == "" {
|
||||
return fmt.Errorf("用户手机号不能为空")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetUserStats 获取用户统计信息
|
||||
func (s *UserService) GetUserStats(ctx context.Context) (map[string]interface{}, error) {
|
||||
// 这里可以添加用户统计逻辑
|
||||
stats := map[string]interface{}{
|
||||
"total_users": 0, // 需要实现具体的统计逻辑
|
||||
"active_users": 0,
|
||||
"new_users_today": 0,
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
Reference in New Issue
Block a user