2024-11-21 11:25:32 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/labstack/echo/v4"
|
2024-11-22 17:55:15 +00:00
|
|
|
"leafdev.top/Leaf/api-platform/internal/handler/http/response"
|
|
|
|
"leafdev.top/Leaf/api-platform/internal/schema"
|
|
|
|
"leafdev.top/Leaf/api-platform/internal/service/auth"
|
2024-11-21 11:25:32 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2024-11-22 17:53:33 +00:00
|
|
|
// @Success 200 {object} response.Body{data=schema.CurrentUserResponse}
|
|
|
|
// @Failure 400 {object} response.Body
|
2024-11-21 11:25:32 +00:00
|
|
|
// @Router /api/v1/ping [get]
|
|
|
|
func (u *UserController) Test(c echo.Context) error {
|
|
|
|
user, ok := u.authService.GetUser(c)
|
|
|
|
if !ok {
|
|
|
|
return response.Ctx(c).Status(http.StatusUnauthorized).Send()
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentUserResponse = &schema.CurrentUserResponse{
|
|
|
|
IP: c.Request().RemoteAddr,
|
|
|
|
Valid: user.Valid,
|
|
|
|
UserEmail: user.Token.Email,
|
|
|
|
UserId: user.Token.Sub,
|
|
|
|
UserName: user.Token.Name,
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Ctx(c).Data(currentUserResponse).Send()
|
|
|
|
}
|