profile_service.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package service
  2. import (
  3. "errors"
  4. "io"
  5. "github.com/2930134478/AI-CS/backend/infra"
  6. "github.com/2930134478/AI-CS/backend/repository"
  7. "gorm.io/gorm"
  8. )
  9. // ProfileService 负责个人资料相关的业务逻辑。
  10. type ProfileService struct {
  11. users *repository.UserRepository
  12. storage infra.StorageService
  13. }
  14. // NewProfileService 创建 ProfileService 实例。
  15. func NewProfileService(users *repository.UserRepository, storage infra.StorageService) *ProfileService {
  16. return &ProfileService{
  17. users: users,
  18. storage: storage,
  19. }
  20. }
  21. // GetProfile 获取用户的个人资料。
  22. func (s *ProfileService) GetProfile(userID uint) (*ProfileResult, error) {
  23. user, err := s.users.GetByID(userID)
  24. if err != nil {
  25. if errors.Is(err, gorm.ErrRecordNotFound) {
  26. return nil, errors.New("用户不存在")
  27. }
  28. return nil, err
  29. }
  30. return &ProfileResult{
  31. ID: user.ID,
  32. Username: user.Username,
  33. Role: user.Role,
  34. Permissions: func() []string {
  35. if user.Role == "admin" {
  36. return AllPermissionKeys()
  37. }
  38. keys := DecodePermissions(user.Permissions)
  39. if len(keys) == 0 {
  40. return DefaultAgentPermissions()
  41. }
  42. return keys
  43. }(),
  44. AvatarURL: user.AvatarURL,
  45. Nickname: user.Nickname,
  46. Email: user.Email,
  47. ReceiveAIConversations: user.ReceiveAIConversations,
  48. }, nil
  49. }
  50. // UpdateProfile 更新用户的个人资料。
  51. func (s *ProfileService) UpdateProfile(input UpdateProfileInput) (*ProfileResult, error) {
  52. // 检查用户是否存在
  53. if _, err := s.users.GetByID(input.UserID); err != nil {
  54. if errors.Is(err, gorm.ErrRecordNotFound) {
  55. return nil, errors.New("用户不存在")
  56. }
  57. return nil, err
  58. }
  59. updates := make(map[string]interface{})
  60. if input.Nickname != nil {
  61. updates["nickname"] = *input.Nickname
  62. }
  63. if input.Email != nil {
  64. updates["email"] = *input.Email
  65. }
  66. if input.ReceiveAIConversations != nil {
  67. updates["receive_ai_conversations"] = *input.ReceiveAIConversations
  68. }
  69. if len(updates) > 0 {
  70. if err := s.users.UpdateFields(input.UserID, updates); err != nil {
  71. return nil, err
  72. }
  73. }
  74. return s.GetProfile(input.UserID)
  75. }
  76. // UploadAvatar 上传用户头像。
  77. func (s *ProfileService) UploadAvatar(userID uint, file io.Reader, filename string) (*ProfileResult, error) {
  78. // 检查用户是否存在
  79. user, err := s.users.GetByID(userID)
  80. if err != nil {
  81. if errors.Is(err, gorm.ErrRecordNotFound) {
  82. return nil, errors.New("用户不存在")
  83. }
  84. return nil, err
  85. }
  86. // 如果已有头像,删除旧头像
  87. if user.AvatarURL != "" {
  88. if err := s.storage.DeleteFile(user.AvatarURL); err != nil {
  89. // 删除失败不阻止更新,只记录警告
  90. // log.Printf("删除旧头像失败: %v", err)
  91. }
  92. }
  93. // 保存新头像
  94. avatarURL, err := s.storage.SaveAvatar(userID, file, filename)
  95. if err != nil {
  96. return nil, err
  97. }
  98. // 更新用户头像URL
  99. updates := map[string]interface{}{
  100. "avatar_url": avatarURL,
  101. }
  102. if err := s.users.UpdateFields(userID, updates); err != nil {
  103. return nil, err
  104. }
  105. return s.GetProfile(userID)
  106. }