优化结构

This commit is contained in:
Yvan
2026-08-25 00:20:29 +08:00
parent 969afee284
commit ee179d6b34
49 changed files with 289 additions and 591 deletions
+4 -93
View File
@@ -1,27 +1,21 @@
package service
// This package is a compatibility facade for the transport layer. Concrete
// implementations live under service/system, service/payment,
// service/integration, and service/task; aliases here preserve the original
// service.* API used by handlers and the composition root.
// This package is the transport layer's entry point into the service tier.
// Concrete implementations live under service/system, service/payment,
// service/integration, and service/task; the aliases here give handlers one
// import instead of four, and Wire composes the module provider sets directly.
import (
"kra/internal/biz/integration"
paymentbiz "kra/internal/biz/payment"
"kra/internal/biz/system"
taskbiz "kra/internal/biz/task"
"kra/internal/service/dto"
integrationservice "kra/internal/service/integration"
paymentservice "kra/internal/service/payment"
systemservice "kra/internal/service/system"
taskservice "kra/internal/service/task"
"kra/internal/utils/routepath"
"github.com/google/wire"
)
type AccessControlService = systemservice.AccessControlService
type AnnouncementInput = systemservice.AnnouncementInput
type AnnouncementService = systemservice.AnnouncementService
type APIService = systemservice.APIService
type AuditService = systemservice.AuditService
@@ -32,7 +26,6 @@ type DepartmentService = systemservice.DepartmentService
type DictionaryService = systemservice.DictionaryService
type EmailService = systemservice.EmailService
type ExportService = systemservice.ExportService
type ExportToken = systemservice.ExportToken
type LogViewerService = systemservice.LogViewerService
type MediaService = systemservice.MediaService
type MenuService = systemservice.MenuService
@@ -40,7 +33,6 @@ type ParameterService = systemservice.ParameterService
type PermissionService = systemservice.PermissionService
type PositionService = systemservice.PositionService
type SecurityService = systemservice.SecurityService
type PasswordPolicyError = systemservice.PasswordPolicyError
type SystemConfigService = systemservice.SystemConfigService
type TokenService = systemservice.TokenService
type VersionService = systemservice.VersionService
@@ -49,90 +41,9 @@ type IntegrationConfigService = integrationservice.IntegrationConfigService
type TaskService = taskservice.TaskService
type UserService = systemservice.UserService
func NewAccessControlService(uc *system.AccessControlUsecase) *AccessControlService {
return systemservice.NewAccessControlService(uc)
}
func NewAnnouncementService(uc *system.AnnouncementUsecase) *AnnouncementService {
return systemservice.NewAnnouncementService(uc)
}
func NewAPIService(uc *system.APIUsecase, settings system.RuntimeSettings) *APIService {
return systemservice.NewAPIService(uc, settings)
}
func NewAuditService(uc *system.AuditUsecase) *AuditService {
return systemservice.NewAuditService(uc)
}
func NewAuditRecorder(uc *system.AuditRecorderUsecase) *AuditRecorder {
return systemservice.NewAuditRecorder(uc)
}
func NewAuthService(uc *system.AuthenticationUsecase) *AuthService {
return systemservice.NewAuthService(uc)
}
func NewAuthorityService(uc *system.AuthorityUsecase) *AuthorityService {
return systemservice.NewAuthorityService(uc)
}
func NewDepartmentService(uc *system.DepartmentUsecase) *DepartmentService {
return systemservice.NewDepartmentService(uc)
}
func NewDictionaryService(uc *system.DictionaryUsecase) *DictionaryService {
return systemservice.NewDictionaryService(uc)
}
func NewEmailService(uc *system.EmailUsecase) *EmailService {
return systemservice.NewEmailService(uc)
}
func NewExportService(uc *system.ExportUsecase, cache system.Cache) *ExportService {
return systemservice.NewExportService(uc, cache)
}
func NewIntegrationConfigService(uc *integration.IntegrationConfigUsecase) *IntegrationConfigService {
return integrationservice.NewIntegrationConfigService(uc)
}
func NewLogViewerService(uc *system.LogViewerUsecase) *LogViewerService {
return systemservice.NewLogViewerService(uc)
}
func NewMediaService(uc *system.MediaUsecase, settings system.RuntimeSettings) *MediaService {
return systemservice.NewMediaService(uc, settings)
}
func NewMenuService(uc *system.MenuUsecase) *MenuService {
return systemservice.NewMenuService(uc)
}
func NewParameterService(uc *system.ParameterUsecase) *ParameterService {
return systemservice.NewParameterService(uc)
}
func NewPaymentService(uc *paymentbiz.PaymentUsecase) *PaymentService {
return paymentservice.NewPaymentService(uc)
}
func NewPermissionService(uc *system.PermissionUsecase) *PermissionService {
return systemservice.NewPermissionService(uc)
}
func NewPositionService(uc *system.PositionUsecase) *PositionService {
return systemservice.NewPositionService(uc)
}
func NewSecurityService(uc *system.SecurityUsecase) *SecurityService {
return systemservice.NewSecurityService(uc)
}
func NewSystemConfigService(uc *system.SystemConfigUsecase, settings system.RuntimeSettings) *SystemConfigService {
return systemservice.NewSystemConfigService(uc, settings)
}
func NewTaskService(uc *taskbiz.TaskApplicationUsecase) *TaskService {
return taskservice.NewTaskService(uc)
}
func NewTokenService(uc *system.TokenUsecase, issuer system.TokenIssuer) *TokenService {
return systemservice.NewTokenService(uc, issuer)
}
func NewUserService(uc *system.UserUsecase, settings *SecurityService) *UserService {
return systemservice.NewUserService(uc, settings)
}
func NewVersionService(uc *system.VersionUsecase) *VersionService {
return systemservice.NewVersionService(uc)
}
var ErrExportTokenInvalid = systemservice.ErrExportTokenInvalid
var ErrExportTokenMalformed = systemservice.ErrExportTokenMalformed
var ErrExportTokenType = systemservice.ErrExportTokenType
func NormalizeRoutePath(path, routerPrefix string) string {
return routepath.Normalize(path, routerPrefix)
}
func IsPasswordPolicyError(err error) bool { return systemservice.IsPasswordPolicyError(err) }
func DefaultPaymentCallbackAck(provider string, success bool) dto.PaymentCallbackAck {
+3 -11
View File
@@ -9,14 +9,6 @@ import (
"kra/internal/service/dto"
)
type AnnouncementInput struct {
ID uint
Title string
Content string
UserID *int
Attachments json.RawMessage
}
type AnnouncementService struct{ uc *system.AnnouncementUsecase }
func NewAnnouncementService(uc *system.AnnouncementUsecase) *AnnouncementService {
@@ -27,15 +19,15 @@ func announcementDTO(item *system.Announcement) *dto.AnnouncementResponse {
return &dto.AnnouncementResponse{ID: item.ID, CreatedAt: item.CreatedAt, UpdatedAt: item.UpdatedAt, DeletedAt: nil, Title: item.Title, Content: item.Content, UserID: item.UserID, Attachments: json.RawMessage(item.Attachments)}
}
func announcementDO(in AnnouncementInput) *system.Announcement {
func announcementDO(in *dto.AnnouncementRequest) *system.Announcement {
return &system.Announcement{ID: in.ID, Title: in.Title, Content: in.Content, UserID: in.UserID, Attachments: in.Attachments}
}
func (s *AnnouncementService) Create(ctx context.Context, in AnnouncementInput) error {
func (s *AnnouncementService) Create(ctx context.Context, in *dto.AnnouncementRequest) error {
return s.uc.Create(ctx, announcementDO(in))
}
func (s *AnnouncementService) Update(ctx context.Context, in AnnouncementInput) error {
func (s *AnnouncementService) Update(ctx context.Context, in *dto.AnnouncementRequest) error {
return s.uc.Update(ctx, announcementDO(in))
}
-6
View File
@@ -56,9 +56,6 @@ func (s *AuditService) DeleteOperations(ctx context.Context, ids []int) error {
func (s *AuditRecorder) RecordLogin(ctx context.Context, v *system.LoginLog) error {
return s.uc.RecordLogin(ctx, v)
}
func (s *AuditRecorder) RecordLoginRequest(ctx context.Context, value *dto.LoginLogRequest) error {
return s.RecordLogin(ctx, &system.LoginLog{Username: value.Username, IP: value.IP, Status: value.Status, ErrorMessage: value.ErrorMessage, Agent: value.Agent, UserID: value.UserID})
}
func (s *AuditService) LoginsFilter(ctx context.Context, page, size int, username string, status bool) ([]*dto.LoginLogResponse, int64, error) {
return s.Logins(ctx, page, size, &system.LoginLog{Username: username, Status: status, FilterByStatus: status})
}
@@ -94,9 +91,6 @@ func (s *AuditService) DeleteLogins(ctx context.Context, ids []int) error {
func (s *AuditRecorder) RecordDataAccess(ctx context.Context, v *system.DataAccessLog) error {
return s.uc.RecordDataAccess(ctx, v)
}
func (s *AuditRecorder) RecordDataAccessRequest(ctx context.Context, value *dto.DataAccessRecordRequest) error {
return s.RecordDataAccess(ctx, &system.DataAccessLog{EventType: value.EventType, Operation: value.Operation, UserID: value.UserID, AuthorityID: value.AuthorityID, RequestID: value.RequestID, Method: value.Method, Path: value.Path, Detail: value.Detail})
}
func (s *AuditService) DataAccessRequest(ctx context.Context, req *dto.DataAccessListRequest) ([]*dto.DataAccessLogResponse, int64, error) {
return s.DataAccess(ctx, req.Page, req.PageSize, &system.DataAccessLog{EventType: req.EventType, TargetTable: req.TargetTable})
}
-4
View File
@@ -14,7 +14,3 @@ func (s *EmailService) Test(ctx context.Context) error { return s.uc.Test(ctx) }
func (s *EmailService) Send(ctx context.Context, to, subject, body string) error {
return s.uc.Send(ctx, to, subject, body)
}
func (s *EmailService) Alert(ctx context.Context, subject, body string) error {
return s.uc.Alert(ctx, subject, body)
}
@@ -64,10 +64,6 @@ func (s *SecurityService) CaptchaSettings() system.CaptchaSettings {
return s.uc.CaptchaRuntimeSettings()
}
func (s *SecurityService) ActiveTokenMatches(ctx context.Context, username, token string) (bool, error) {
return s.uc.ActiveTokenMatches(ctx, username, token)
}
func (s *SecurityService) RotateActiveToken(ctx context.Context, username, oldToken, newToken string, expiration time.Duration) error {
return s.uc.RotateActiveToken(ctx, username, oldToken, newToken, expiration)
}
+27 -56
View File
@@ -7,14 +7,6 @@ import (
"kra/internal/service/dto"
)
type userInput struct {
ID uint
Username, Password, NickName, HeaderImg, Phone, Email string
AuthorityID uint
Enable int
AuthorityIDs []uint
}
type UserService struct {
uc *system.UserUsecase
settings *SecurityService
@@ -24,28 +16,39 @@ func NewUserService(uc *system.UserUsecase, settings *SecurityService) *UserServ
return &UserService{uc: uc, settings: settings}
}
func userRequestInput(value *dto.UserRequest) userInput {
return userInput{ID: value.ID, Username: value.Username, Password: value.Password, NickName: value.NickName, HeaderImg: value.HeaderImg, AuthorityID: value.AuthorityID, AuthorityIDs: value.AuthorityIDs, Enable: value.Enable, Phone: value.Phone, Email: value.Email}
}
func (s *UserService) ListUsersRequest(ctx context.Context, value *dto.UserListRequest) ([]*dto.UserResponse, int64, error) {
return s.ListUsers(ctx, value.Page, value.PageSize, &system.UserListFilter{Username: value.Username, NickName: value.NickName, Phone: value.Phone, Email: value.Email, OrderKey: value.OrderKey, Desc: value.Desc})
}
func (s *UserService) CreateUserRequest(ctx context.Context, value *dto.UserRequest) (*dto.UserResponse, error) {
return s.createUser(ctx, userRequestInput(value))
}
func (s *UserService) UpdateUserRequest(ctx context.Context, value *dto.UserRequest) error {
return s.updateUser(ctx, userRequestInput(value))
}
func (s *UserService) UpdateSelfUserRequest(ctx context.Context, id uint, value *dto.SelfUserRequest) error {
return s.updateSelfUser(ctx, userInput{ID: id, NickName: value.NickName, HeaderImg: value.HeaderImg, Phone: value.Phone, Email: value.Email, Enable: value.Enable})
users, total, err := s.uc.ListUsers(ctx, value.Page, value.PageSize, &system.UserListFilter{Username: value.Username, NickName: value.NickName, Phone: value.Phone, Email: value.Email, OrderKey: value.OrderKey, Desc: value.Desc})
if err != nil {
return nil, 0, err
}
result := make([]*dto.UserResponse, 0, len(users))
for _, user := range users {
result = append(result, convertUser(user))
}
return result, total, nil
}
func (s *UserService) User(ctx context.Context, id uint) (*dto.UserResponse, error) {
value, err := s.uc.User(ctx, id)
func (s *UserService) CreateUserRequest(ctx context.Context, value *dto.UserRequest) (*dto.UserResponse, error) {
if err := s.settings.ValidatePassword(ctx, value.Password); err != nil {
return nil, err
}
security, _ := s.settings.CurrentSecurity(ctx)
mustChange := security != nil && security.ForceNewUserChangePassword
user, err := s.uc.CreateUser(ctx, &system.User{Username: value.Username, Password: value.Password, NickName: value.NickName, HeaderImg: value.HeaderImg, AuthorityID: value.AuthorityID, Phone: value.Phone, Email: value.Email, Enable: value.Enable, MustChangePassword: mustChange}, value.AuthorityIDs)
if err != nil {
return nil, err
}
return convertUser(value), nil
return convertUser(user), nil
}
func (s *UserService) UpdateUserRequest(ctx context.Context, value *dto.UserRequest) error {
// The compatible ChangeUserInfo payload uses authorityIds for role assignment; the
// standalone authorityId field is not applied by setUserInfo.
return s.uc.UpdateUser(ctx, &system.User{ID: value.ID, NickName: value.NickName, HeaderImg: value.HeaderImg, Phone: value.Phone, Email: value.Email, Enable: value.Enable}, value.AuthorityIDs)
}
func (s *UserService) UpdateSelfUserRequest(ctx context.Context, id uint, value *dto.SelfUserRequest) error {
return s.uc.UpdateSelfUser(ctx, &system.User{ID: id, NickName: value.NickName, HeaderImg: value.HeaderImg, Phone: value.Phone, Email: value.Email, Enable: value.Enable})
}
func (s *UserService) UserByUUID(ctx context.Context, uuid string) (*dto.UserResponse, error) {
@@ -64,18 +67,6 @@ func (s *UserService) Menus(ctx context.Context, authorityID uint) ([]*dto.Dynam
return dynamicMenuResponses(menus), nil
}
func (s *UserService) ListUsers(ctx context.Context, page, pageSize int, filter *system.UserListFilter) ([]*dto.UserResponse, int64, error) {
users, total, err := s.uc.ListUsers(ctx, page, pageSize, filter)
if err != nil {
return nil, 0, err
}
result := make([]*dto.UserResponse, 0, len(users))
for _, user := range users {
result = append(result, convertUser(user))
}
return result, total, nil
}
func (s *UserService) Authorities(ctx context.Context) ([]*dto.AuthorityResponse, error) {
values, err := s.uc.Authorities(ctx)
if err != nil {
@@ -88,26 +79,6 @@ func (s *UserService) Authorities(ctx context.Context) ([]*dto.AuthorityResponse
return result, nil
}
func (s *UserService) createUser(ctx context.Context, input userInput) (*dto.UserResponse, error) {
if err := s.settings.ValidatePassword(ctx, input.Password); err != nil {
return nil, err
}
security, _ := s.settings.CurrentSecurity(ctx)
mustChange := security != nil && security.ForceNewUserChangePassword
user, err := s.uc.CreateUser(ctx, &system.User{Username: input.Username, Password: input.Password, NickName: input.NickName, HeaderImg: input.HeaderImg, AuthorityID: input.AuthorityID, Phone: input.Phone, Email: input.Email, Enable: input.Enable, MustChangePassword: mustChange}, input.AuthorityIDs)
if err != nil {
return nil, err
}
return convertUser(user), nil
}
func (s *UserService) updateUser(ctx context.Context, input userInput) error {
// The compatible ChangeUserInfo payload uses authorityIds for role assignment; the
// standalone authorityId field is not applied by setUserInfo.
return s.uc.UpdateUser(ctx, &system.User{ID: input.ID, NickName: input.NickName, HeaderImg: input.HeaderImg, Phone: input.Phone, Email: input.Email, Enable: input.Enable}, input.AuthorityIDs)
}
func (s *UserService) updateSelfUser(ctx context.Context, input userInput) error {
return s.uc.UpdateSelfUser(ctx, &system.User{ID: input.ID, NickName: input.NickName, HeaderImg: input.HeaderImg, Phone: input.Phone, Email: input.Email, Enable: input.Enable})
}
func (s *UserService) DeleteUser(ctx context.Context, id uint) error {
return s.uc.DeleteUser(ctx, id)
}