mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-24 15:52:15 +08:00
78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package test
|
|
|
|
import (
|
|
"github.com/gofiber/fiber/v2"
|
|
"gofiber.study.skcks.cn/common/errorx"
|
|
"gofiber.study.skcks.cn/common/response"
|
|
"gofiber.study.skcks.cn/controller/types"
|
|
"gofiber.study.skcks.cn/global"
|
|
)
|
|
|
|
type Controller struct {
|
|
*types.Controller
|
|
}
|
|
|
|
func (c *Controller) GetRouter() fiber.Router {
|
|
return c.Router
|
|
}
|
|
|
|
func NewController(app *fiber.App) *Controller {
|
|
return &Controller{
|
|
Controller: types.NewController(app, "/test"),
|
|
}
|
|
}
|
|
|
|
// JwtEncode jwt token 生成测试
|
|
//
|
|
// @Summary jwt token 生成测试
|
|
// @Description jwt token 生成测试
|
|
// @Tags Test
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param vo body global.UserClaims true "userClaims"
|
|
// @Success 200 {object} response.Response{data=string}
|
|
// @Failure default {object} errorx.CodeErrorResponse
|
|
// @Router /test/jwt [post]
|
|
func (c *Controller) JwtEncode() {
|
|
c.Router.Add(fiber.MethodPost, "/jwt", func(ctx *fiber.Ctx) error {
|
|
claims := global.UserClaims{}
|
|
err := ctx.BodyParser(&claims)
|
|
|
|
if err = errorx.ParseError(err); err != nil {
|
|
return ctx.JSON(err)
|
|
}
|
|
|
|
token, err := global.GetToken(claims)
|
|
|
|
if err = errorx.ParseError(err); err != nil {
|
|
return ctx.JSON(err)
|
|
}
|
|
|
|
return ctx.JSON(response.NewResponse(token))
|
|
})
|
|
}
|
|
|
|
// JwtDecode jwt token 解析测试
|
|
//
|
|
// @Summary jwt token 解析测试
|
|
// @Description jwt token 解析测试
|
|
// @Tags Test
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param token query string true "token"
|
|
// @Success 200 {object} response.Response{data=global.UserClaims}
|
|
// @Failure default {object} errorx.CodeErrorResponse
|
|
// @Router /test/jwt [get]
|
|
func (c *Controller) JwtDecode() {
|
|
c.Router.Add(fiber.MethodGet, "/jwt", func(ctx *fiber.Ctx) error {
|
|
token := ctx.Query("token")
|
|
claims, err := global.ParseToken(token)
|
|
|
|
if err = errorx.ParseError(err); err != nil {
|
|
return ctx.JSON(err)
|
|
}
|
|
|
|
return ctx.JSON(response.NewResponse(claims))
|
|
})
|
|
}
|