leaf-library-3/internal/api/http/v1/test.go
2024-12-06 23:38:22 +08:00

57 lines
1.4 KiB
Go

package v1
import (
"net/http"
"github.com/gofiber/fiber/v2"
"leafdev.top/Leaf/leaf-library-3/internal/pkg/validator"
"leafdev.top/Leaf/leaf-library-3/internal/services/auth"
"leafdev.top/Leaf/leaf-library-3/internal/types/dto"
)
type UserController struct {
authService *auth.Service
}
func NewUserController(authService *auth.Service) *UserController {
return &UserController{authService}
}
// Test godoc
// @Summary Greet
// @Description 测试接口,将会返回当前用户的信息
// @Tags ping
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @deprecated true
// @Success 200 {object} response.Body{data=schema.CurrentUserResponse}
// @Failure 400 {object} response.Body
// @Router /api/v1/ping [get]
func (u *UserController) Test(c *fiber.Ctx) error {
user := u.authService.GetUser(c)
// bind
var testRequest = &dto.TestRequest{}
err := c.QueryParser(testRequest)
if err != nil {
return err
}
// 验证
validationErrors, err := validator.Struct(testRequest)
if err != nil {
return dto.Ctx(c).Error(err).Data(validationErrors).Send()
}
var currentUserResponse = &dto.CurrentUserResponse{
IP: c.IP(),
Valid: user.Valid,
UserEmail: user.Token.Email,
UserId: user.Token.Sub,
UserName: user.Token.Name,
}
return dto.Ctx(c).Status(http.StatusOK).Data(currentUserResponse).Send()
}