4 Commits

Author SHA1 Message Date
6cd0fa3d6c chore: 修改 401 错误信息 2025-07-08 16:47:37 +08:00
9c8460fc44 feat: 添加 MustWithData 2025-07-04 13:57:26 +08:00
5df55a364d feat: tool 添加 gin local 2025-07-04 10:27:44 +08:00
002f5f15fb fix: 兼容以前的 code 设计, 计划替换 2025-07-04 10:17:25 +08:00
5 changed files with 35 additions and 1 deletions

4
opt/opt.go Normal file
View File

@ -0,0 +1,4 @@
package opt
type Config struct {
}

View File

@ -3,7 +3,7 @@ package resp
const (
Msg200 = "操作成功"
Msg400 = "参数错误"
Msg401 = "登录信息不存在或已过期, 请重新登录"
Msg401 = "该账号登录已失效, 请重新登录"
Msg401NoMulti = "用户已在其他地方登录"
Msg403 = "权限不足"
Msg404 = "资源不存在"

View File

@ -3,6 +3,7 @@ package resp
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/spf13/cast"
)
type res struct {
@ -10,6 +11,8 @@ type res struct {
Msg string `json:"msg"`
Data any `json:"data"`
Err any `json:"err"`
// Deprecated: 200:1, other: -1
Code int `json:"code"`
}
func R200(c *gin.Context, data any, msgs ...string) {
@ -17,6 +20,7 @@ func R200(c *gin.Context, data any, msgs ...string) {
Status: 200,
Msg: Msg200,
Data: data,
Code: 1,
}
if len(msgs) > 0 && msgs[0] != "" {
@ -50,6 +54,10 @@ func _r(c *gin.Context, r *res, args ...any) {
r.Data = args[1]
case 3:
r.Err = args[2]
case 4:
if code, err := cast.ToIntE(args[3]); err == nil {
r.Code = code
}
}
if r.Msg == "" {
@ -62,6 +70,7 @@ func _r(c *gin.Context, r *res, args ...any) {
func R400(c *gin.Context, args ...any) {
r := &res{
Status: 400,
Code: -1,
}
_r(c, r, args...)
@ -70,6 +79,7 @@ func R400(c *gin.Context, args ...any) {
func R401(c *gin.Context, args ...any) {
r := &res{
Status: 401,
Code: -1,
}
_r(c, r, args...)
@ -78,6 +88,7 @@ func R401(c *gin.Context, args ...any) {
func R403(c *gin.Context, args ...any) {
r := &res{
Status: 403,
Code: -1,
}
_r(c, r, args...)
@ -86,6 +97,7 @@ func R403(c *gin.Context, args ...any) {
func R500(c *gin.Context, args ...any) {
r := &res{
Status: 500,
Code: -1,
}
_r(c, r, args...)
@ -94,6 +106,7 @@ func R500(c *gin.Context, args ...any) {
func R501(c *gin.Context, args ...any) {
r := &res{
Status: 501,
Code: -1,
}
_r(c, r, args...)

12
tool/gin.go Normal file
View File

@ -0,0 +1,12 @@
package tool
import "github.com/gin-gonic/gin"
func Local(c *gin.Context, key string) any {
data, ok := c.Get(key)
if !ok {
return nil
}
return data
}

View File

@ -11,3 +11,8 @@ func Must(errs ...error) {
}
}
}
func MustWithData[T any](data T, err error) T {
Must(err)
return data
}