profile_service.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. AvatarURL: user.AvatarURL,
  35. Nickname: user.Nickname,
  36. Email: user.Email,
  37. ReceiveAIConversations: user.ReceiveAIConversations,
  38. }, nil
  39. }
  40. // UpdateProfile 更新用户的个人资料。
  41. func (s *ProfileService) UpdateProfile(input UpdateProfileInput) (*ProfileResult, error) {
  42. // 检查用户是否存在
  43. if _, err := s.users.GetByID(input.UserID); err != nil {
  44. if errors.Is(err, gorm.ErrRecordNotFound) {
  45. return nil, errors.New("用户不存在")
  46. }
  47. return nil, err
  48. }
  49. updates := make(map[string]interface{})
  50. if input.Nickname != nil {
  51. updates["nickname"] = *input.Nickname
  52. }
  53. if input.Email != nil {
  54. updates["email"] = *input.Email
  55. }
  56. if input.ReceiveAIConversations != nil {
  57. updates["receive_ai_conversations"] = *input.ReceiveAIConversations
  58. }
  59. if len(updates) > 0 {
  60. if err := s.users.UpdateFields(input.UserID, updates); err != nil {
  61. return nil, err
  62. }
  63. }
  64. return s.GetProfile(input.UserID)
  65. }
  66. // UploadAvatar 上传用户头像。
  67. func (s *ProfileService) UploadAvatar(userID uint, file io.Reader, filename string) (*ProfileResult, error) {
  68. // 检查用户是否存在
  69. user, err := s.users.GetByID(userID)
  70. if err != nil {
  71. if errors.Is(err, gorm.ErrRecordNotFound) {
  72. return nil, errors.New("用户不存在")
  73. }
  74. return nil, err
  75. }
  76. // 如果已有头像,删除旧头像
  77. if user.AvatarURL != "" {
  78. if err := s.storage.DeleteFile(user.AvatarURL); err != nil {
  79. // 删除失败不阻止更新,只记录警告
  80. // log.Printf("删除旧头像失败: %v", err)
  81. }
  82. }
  83. // 保存新头像
  84. avatarURL, err := s.storage.SaveAvatar(userID, file, filename)
  85. if err != nil {
  86. return nil, err
  87. }
  88. // 更新用户头像URL
  89. updates := map[string]interface{}{
  90. "avatar_url": avatarURL,
  91. }
  92. if err := s.users.UpdateFields(userID, updates); err != nil {
  93. return nil, err
  94. }
  95. return s.GetProfile(userID)
  96. }