profile_controller.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package controller
  2. import (
  3. "net/http"
  4. "github.com/2930134478/AI-CS/backend/service"
  5. "github.com/gin-gonic/gin"
  6. )
  7. // ProfileController 负责处理个人资料相关的 HTTP 请求。
  8. type ProfileController struct {
  9. profileService *service.ProfileService
  10. }
  11. // NewProfileController 创建 ProfileController 实例。
  12. func NewProfileController(profileService *service.ProfileService) *ProfileController {
  13. return &ProfileController{profileService: profileService}
  14. }
  15. type updateProfileRequest struct {
  16. Nickname *string `json:"nickname"`
  17. Email *string `json:"email"`
  18. ReceiveAIConversations *bool `json:"receive_ai_conversations"` // 是否接收 AI 对话(可选)
  19. }
  20. // GetProfile 获取当前用户的个人资料。
  21. func (p *ProfileController) GetProfile(c *gin.Context) {
  22. // 从路径参数获取用户ID(后续可以改为从JWT token获取)
  23. userID, err := parseUintParam(c, "user_id")
  24. if err != nil {
  25. c.JSON(http.StatusBadRequest, gin.H{"error": "user_id 不合法"})
  26. return
  27. }
  28. profile, err := p.profileService.GetProfile(uint(userID))
  29. if err != nil {
  30. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  31. return
  32. }
  33. c.JSON(http.StatusOK, profile)
  34. }
  35. // UpdateProfile 更新当前用户的个人资料。
  36. func (p *ProfileController) UpdateProfile(c *gin.Context) {
  37. // 从路径参数获取用户ID(后续可以改为从JWT token获取)
  38. userID, err := parseUintParam(c, "user_id")
  39. if err != nil {
  40. c.JSON(http.StatusBadRequest, gin.H{"error": "user_id 不合法"})
  41. return
  42. }
  43. var req updateProfileRequest
  44. if err := c.ShouldBindJSON(&req); err != nil {
  45. c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数错误"})
  46. return
  47. }
  48. profile, err := p.profileService.UpdateProfile(service.UpdateProfileInput{
  49. UserID: uint(userID),
  50. Nickname: req.Nickname,
  51. Email: req.Email,
  52. ReceiveAIConversations: req.ReceiveAIConversations,
  53. })
  54. if err != nil {
  55. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  56. return
  57. }
  58. c.JSON(http.StatusOK, profile)
  59. }
  60. // UploadAvatar 上传用户头像。
  61. func (p *ProfileController) UploadAvatar(c *gin.Context) {
  62. // 从路径参数获取用户ID(后续可以改为从JWT token获取)
  63. userID, err := parseUintParam(c, "user_id")
  64. if err != nil {
  65. c.JSON(http.StatusBadRequest, gin.H{"error": "user_id 不合法"})
  66. return
  67. }
  68. // 获取上传的文件
  69. file, err := c.FormFile("avatar")
  70. if err != nil {
  71. c.JSON(http.StatusBadRequest, gin.H{"error": "请选择头像文件"})
  72. return
  73. }
  74. // 验证文件类型(只允许图片)
  75. allowedTypes := map[string]bool{
  76. "image/jpeg": true,
  77. "image/jpg": true,
  78. "image/png": true,
  79. "image/gif": true,
  80. }
  81. if !allowedTypes[file.Header.Get("Content-Type")] {
  82. c.JSON(http.StatusBadRequest, gin.H{"error": "只支持上传图片文件(jpg、png、gif)"})
  83. return
  84. }
  85. // 验证文件大小(限制10MB)
  86. if file.Size > 10*1024*1024 {
  87. c.JSON(http.StatusBadRequest, gin.H{"error": "头像文件大小不能超过10MB"})
  88. return
  89. }
  90. // 打开文件
  91. src, err := file.Open()
  92. if err != nil {
  93. c.JSON(http.StatusInternalServerError, gin.H{"error": "打开文件失败"})
  94. return
  95. }
  96. defer src.Close()
  97. // 上传头像
  98. profile, err := p.profileService.UploadAvatar(uint(userID), src, file.Filename)
  99. if err != nil {
  100. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  101. return
  102. }
  103. c.JSON(http.StatusOK, profile)
  104. }