profile_controller.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. }
  19. // GetProfile 获取当前用户的个人资料。
  20. func (p *ProfileController) GetProfile(c *gin.Context) {
  21. // 从路径参数获取用户ID(后续可以改为从JWT token获取)
  22. userID, err := parseUintParam(c, "user_id")
  23. if err != nil {
  24. c.JSON(http.StatusBadRequest, gin.H{"error": "user_id 不合法"})
  25. return
  26. }
  27. profile, err := p.profileService.GetProfile(uint(userID))
  28. if err != nil {
  29. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  30. return
  31. }
  32. c.JSON(http.StatusOK, profile)
  33. }
  34. // UpdateProfile 更新当前用户的个人资料。
  35. func (p *ProfileController) UpdateProfile(c *gin.Context) {
  36. // 从路径参数获取用户ID(后续可以改为从JWT token获取)
  37. userID, err := parseUintParam(c, "user_id")
  38. if err != nil {
  39. c.JSON(http.StatusBadRequest, gin.H{"error": "user_id 不合法"})
  40. return
  41. }
  42. var req updateProfileRequest
  43. if err := c.ShouldBindJSON(&req); err != nil {
  44. c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数错误"})
  45. return
  46. }
  47. profile, err := p.profileService.UpdateProfile(service.UpdateProfileInput{
  48. UserID: uint(userID),
  49. Nickname: req.Nickname,
  50. Email: req.Email,
  51. })
  52. if err != nil {
  53. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  54. return
  55. }
  56. c.JSON(http.StatusOK, profile)
  57. }
  58. // UploadAvatar 上传用户头像。
  59. func (p *ProfileController) UploadAvatar(c *gin.Context) {
  60. // 从路径参数获取用户ID(后续可以改为从JWT token获取)
  61. userID, err := parseUintParam(c, "user_id")
  62. if err != nil {
  63. c.JSON(http.StatusBadRequest, gin.H{"error": "user_id 不合法"})
  64. return
  65. }
  66. // 获取上传的文件
  67. file, err := c.FormFile("avatar")
  68. if err != nil {
  69. c.JSON(http.StatusBadRequest, gin.H{"error": "请选择头像文件"})
  70. return
  71. }
  72. // 验证文件类型(只允许图片)
  73. allowedTypes := map[string]bool{
  74. "image/jpeg": true,
  75. "image/jpg": true,
  76. "image/png": true,
  77. "image/gif": true,
  78. }
  79. if !allowedTypes[file.Header.Get("Content-Type")] {
  80. c.JSON(http.StatusBadRequest, gin.H{"error": "只支持上传图片文件(jpg、png、gif)"})
  81. return
  82. }
  83. // 验证文件大小(限制10MB)
  84. if file.Size > 10*1024*1024 {
  85. c.JSON(http.StatusBadRequest, gin.H{"error": "头像文件大小不能超过10MB"})
  86. return
  87. }
  88. // 打开文件
  89. src, err := file.Open()
  90. if err != nil {
  91. c.JSON(http.StatusInternalServerError, gin.H{"error": "打开文件失败"})
  92. return
  93. }
  94. defer src.Close()
  95. // 上传头像
  96. profile, err := p.profileService.UploadAvatar(uint(userID), src, file.Filename)
  97. if err != nil {
  98. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  99. return
  100. }
  101. c.JSON(http.StatusOK, profile)
  102. }