mirror of
				https://github.com/go-nunu/nunu-layout-advanced.git
				synced 2025-10-31 12:06:20 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			129 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			129 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package handler
 | |
| 
 | |
| import (
 | |
| 	"github.com/gin-gonic/gin"
 | |
| 	"github.com/go-nunu/nunu-layout-advanced/api/v1"
 | |
| 	"github.com/go-nunu/nunu-layout-advanced/internal/service"
 | |
| 	"go.uber.org/zap"
 | |
| 	"net/http"
 | |
| )
 | |
| 
 | |
| type UserHandler struct {
 | |
| 	*Handler
 | |
| 	userService service.UserService
 | |
| }
 | |
| 
 | |
| func NewUserHandler(handler *Handler, userService service.UserService) *UserHandler {
 | |
| 	return &UserHandler{
 | |
| 		Handler:     handler,
 | |
| 		userService: userService,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| // Register godoc
 | |
| // @Summary 用户注册
 | |
| // @Schemes
 | |
| // @Description 目前只支持邮箱登录
 | |
| // @Tags 用户模块
 | |
| // @Accept json
 | |
| // @Produce json
 | |
| // @Param request body v1.RegisterRequest true "params"
 | |
| // @Success 200 {object} v1.Response
 | |
| // @Router /register [post]
 | |
| func (h *UserHandler) Register(ctx *gin.Context) {
 | |
| 	req := new(v1.RegisterRequest)
 | |
| 	if err := ctx.ShouldBindJSON(req); err != nil {
 | |
| 		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	if err := h.userService.Register(ctx, req); err != nil {
 | |
| 		h.logger.WithContext(ctx).Error("userService.Register error", zap.Error(err))
 | |
| 		v1.HandleError(ctx, http.StatusInternalServerError, err, nil)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	v1.HandleSuccess(ctx, nil)
 | |
| }
 | |
| 
 | |
| // Login godoc
 | |
| // @Summary 账号登录
 | |
| // @Schemes
 | |
| // @Description
 | |
| // @Tags 用户模块
 | |
| // @Accept json
 | |
| // @Produce json
 | |
| // @Param request body v1.LoginRequest true "params"
 | |
| // @Success 200 {object} v1.LoginResponse
 | |
| // @Router /login [post]
 | |
| func (h *UserHandler) Login(ctx *gin.Context) {
 | |
| 	var req v1.LoginRequest
 | |
| 	if err := ctx.ShouldBindJSON(&req); err != nil {
 | |
| 		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	token, err := h.userService.Login(ctx, &req)
 | |
| 	if err != nil {
 | |
| 		v1.HandleError(ctx, http.StatusUnauthorized, v1.ErrUnauthorized, nil)
 | |
| 		return
 | |
| 	}
 | |
| 	v1.HandleSuccess(ctx, v1.LoginResponseData{
 | |
| 		AccessToken: token,
 | |
| 	})
 | |
| }
 | |
| 
 | |
| // GetProfile godoc
 | |
| // @Summary 获取用户信息
 | |
| // @Schemes
 | |
| // @Description
 | |
| // @Tags 用户模块
 | |
| // @Accept json
 | |
| // @Produce json
 | |
| // @Security Bearer
 | |
| // @Success 200 {object} v1.GetProfileResponse
 | |
| // @Router /user [get]
 | |
| func (h *UserHandler) GetProfile(ctx *gin.Context) {
 | |
| 	userId := GetUserIdFromCtx(ctx)
 | |
| 	if userId == "" {
 | |
| 		v1.HandleError(ctx, http.StatusUnauthorized, v1.ErrUnauthorized, nil)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	user, err := h.userService.GetProfile(ctx, userId)
 | |
| 	if err != nil {
 | |
| 		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	v1.HandleSuccess(ctx, user)
 | |
| }
 | |
| 
 | |
| // UpdateProfile godoc
 | |
| // @Summary 修改用户信息
 | |
| // @Schemes
 | |
| // @Description
 | |
| // @Tags 用户模块
 | |
| // @Accept json
 | |
| // @Produce json
 | |
| // @Security Bearer
 | |
| // @Param request body v1.UpdateProfileRequest true "params"
 | |
| // @Success 200 {object} v1.GetProfileResponse
 | |
| // @Router /user [put]
 | |
| func (h *UserHandler) UpdateProfile(ctx *gin.Context) {
 | |
| 	userId := GetUserIdFromCtx(ctx)
 | |
| 
 | |
| 	var req v1.UpdateProfileRequest
 | |
| 	if err := ctx.ShouldBindJSON(&req); err != nil {
 | |
| 		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	if err := h.userService.UpdateProfile(ctx, userId, &req); err != nil {
 | |
| 		v1.HandleError(ctx, http.StatusInternalServerError, v1.ErrInternalServerError, nil)
 | |
| 		return
 | |
| 	}
 | |
| 
 | |
| 	v1.HandleSuccess(ctx, nil)
 | |
| }
 | 
