wip: 登录和认证
This commit is contained in:
104
pkg/resp/resp.go
104
pkg/resp/resp.go
@ -1 +1,105 @@
|
||||
package resp
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
type res struct {
|
||||
Status int `json:"status"`
|
||||
Msg string `json:"msg"`
|
||||
Data any `json:"data"`
|
||||
Err any `json:"err"`
|
||||
}
|
||||
|
||||
func R200(c fiber.Ctx, data any, msgs ...string) error {
|
||||
r := &res{
|
||||
Status: 200,
|
||||
Msg: Msg200,
|
||||
Data: data,
|
||||
}
|
||||
|
||||
if len(msgs) > 0 && msgs[0] != "" {
|
||||
r.Msg = msgs[0]
|
||||
}
|
||||
|
||||
return c.JSON(r)
|
||||
}
|
||||
|
||||
func RC(c fiber.Ctx, status int, args ...any) error {
|
||||
return _r(c, &res{Status: status}, args...)
|
||||
}
|
||||
|
||||
func RE(c fiber.Ctx, err error) error {
|
||||
var re *Error
|
||||
|
||||
if errors.As(err, &re) {
|
||||
return _r(c, re._r())
|
||||
}
|
||||
|
||||
return R500(c, "", nil, err)
|
||||
}
|
||||
|
||||
func _r(c fiber.Ctx, r *res, args ...any) error {
|
||||
length := len(args)
|
||||
switch length {
|
||||
case 0:
|
||||
break
|
||||
case 1:
|
||||
if msg, ok := args[0].(string); ok {
|
||||
r.Msg = msg
|
||||
} else {
|
||||
r.Data = args[0]
|
||||
}
|
||||
case 2:
|
||||
r.Data = args[1]
|
||||
case 3:
|
||||
r.Err = args[2]
|
||||
}
|
||||
|
||||
if r.Msg == "" {
|
||||
r.Msg = Msg(r.Status)
|
||||
}
|
||||
|
||||
return c.Status(r.Status).JSON(r)
|
||||
}
|
||||
|
||||
func R400(c fiber.Ctx, args ...any) error {
|
||||
r := &res{
|
||||
Status: 400,
|
||||
}
|
||||
|
||||
return _r(c, r, args...)
|
||||
}
|
||||
|
||||
func R401(c fiber.Ctx, args ...any) error {
|
||||
r := &res{
|
||||
Status: 401,
|
||||
}
|
||||
|
||||
return _r(c, r, args...)
|
||||
}
|
||||
|
||||
func R403(c fiber.Ctx, args ...any) error {
|
||||
r := &res{
|
||||
Status: 403,
|
||||
}
|
||||
|
||||
return _r(c, r, args...)
|
||||
}
|
||||
|
||||
func R500(c fiber.Ctx, args ...any) error {
|
||||
r := &res{
|
||||
Status: 500,
|
||||
}
|
||||
|
||||
return _r(c, r, args...)
|
||||
}
|
||||
|
||||
func R501(c fiber.Ctx, args ...any) error {
|
||||
r := &res{
|
||||
Status: 501,
|
||||
}
|
||||
|
||||
return _r(c, r, args...)
|
||||
}
|
||||
|
Reference in New Issue
Block a user