2025-12-18 13:50:39 +08:00
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"sub2api/internal/model"
|
|
|
|
|
|
"sub2api/internal/pkg/response"
|
|
|
|
|
|
"sub2api/internal/service"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// UserHandler handles user-related requests
|
|
|
|
|
|
type UserHandler struct {
|
|
|
|
|
|
userService *service.UserService
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewUserHandler creates a new UserHandler
|
|
|
|
|
|
func NewUserHandler(userService *service.UserService) *UserHandler {
|
|
|
|
|
|
return &UserHandler{
|
|
|
|
|
|
userService: userService,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ChangePasswordRequest represents the change password request payload
|
|
|
|
|
|
type ChangePasswordRequest struct {
|
|
|
|
|
|
OldPassword string `json:"old_password" binding:"required"`
|
|
|
|
|
|
NewPassword string `json:"new_password" binding:"required,min=6"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-23 11:26:22 +08:00
|
|
|
|
// UpdateProfileRequest represents the update profile request payload
|
|
|
|
|
|
type UpdateProfileRequest struct {
|
|
|
|
|
|
Username *string `json:"username"`
|
|
|
|
|
|
Wechat *string `json:"wechat"`
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
|
// GetProfile handles getting user profile
|
|
|
|
|
|
// GET /api/v1/users/me
|
|
|
|
|
|
func (h *UserHandler) GetProfile(c *gin.Context) {
|
|
|
|
|
|
userValue, exists := c.Get("user")
|
|
|
|
|
|
if !exists {
|
|
|
|
|
|
response.Unauthorized(c, "User not authenticated")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
user, ok := userValue.(*model.User)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
response.InternalError(c, "Invalid user context")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
userData, err := h.userService.GetByID(c.Request.Context(), user.ID)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
response.InternalError(c, "Failed to get user profile: "+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-23 11:26:22 +08:00
|
|
|
|
// 清空notes字段,普通用户不应看到备注
|
|
|
|
|
|
userData.Notes = ""
|
|
|
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
|
response.Success(c, userData)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ChangePassword handles changing user password
|
|
|
|
|
|
// POST /api/v1/users/me/password
|
|
|
|
|
|
func (h *UserHandler) ChangePassword(c *gin.Context) {
|
|
|
|
|
|
userValue, exists := c.Get("user")
|
|
|
|
|
|
if !exists {
|
|
|
|
|
|
response.Unauthorized(c, "User not authenticated")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
user, ok := userValue.(*model.User)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
response.InternalError(c, "Invalid user context")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var req ChangePasswordRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
response.BadRequest(c, "Invalid request: "+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
svcReq := service.ChangePasswordRequest{
|
|
|
|
|
|
CurrentPassword: req.OldPassword,
|
|
|
|
|
|
NewPassword: req.NewPassword,
|
|
|
|
|
|
}
|
|
|
|
|
|
err := h.userService.ChangePassword(c.Request.Context(), user.ID, svcReq)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
response.BadRequest(c, "Failed to change password: "+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response.Success(c, gin.H{"message": "Password changed successfully"})
|
|
|
|
|
|
}
|
2025-12-23 11:26:22 +08:00
|
|
|
|
|
|
|
|
|
|
// UpdateProfile handles updating user profile
|
|
|
|
|
|
// PUT /api/v1/users/me
|
|
|
|
|
|
func (h *UserHandler) UpdateProfile(c *gin.Context) {
|
|
|
|
|
|
userValue, exists := c.Get("user")
|
|
|
|
|
|
if !exists {
|
|
|
|
|
|
response.Unauthorized(c, "User not authenticated")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
user, ok := userValue.(*model.User)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
response.InternalError(c, "Invalid user context")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var req UpdateProfileRequest
|
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
response.BadRequest(c, "Invalid request: "+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
svcReq := service.UpdateProfileRequest{
|
|
|
|
|
|
Username: req.Username,
|
|
|
|
|
|
Wechat: req.Wechat,
|
|
|
|
|
|
}
|
|
|
|
|
|
updatedUser, err := h.userService.UpdateProfile(c.Request.Context(), user.ID, svcReq)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
response.BadRequest(c, "Failed to update profile: "+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清空notes字段,普通用户不应看到备注
|
|
|
|
|
|
updatedUser.Notes = ""
|
|
|
|
|
|
|
|
|
|
|
|
response.Success(c, updatedUser)
|
|
|
|
|
|
}
|