🎨 encapsulation response
This commit is contained in:
31
common/result/error_code.go
Normal file
31
common/result/error_code.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package result
|
||||||
|
|
||||||
|
type ErrorCode int
|
||||||
|
|
||||||
|
const (
|
||||||
|
SystemError ErrorCode = 1001
|
||||||
|
ParamsError ErrorCode = 1002
|
||||||
|
ParamsNullError ErrorCode = 1003
|
||||||
|
ParamsFormatError ErrorCode = 1004
|
||||||
|
ParamsValueError ErrorCode = 1005
|
||||||
|
ParamsRangeError ErrorCode = 1006
|
||||||
|
ParamsRepeatError ErrorCode = 1007
|
||||||
|
ParamsMatchError ErrorCode = 1008
|
||||||
|
ParamsNotUniqueError ErrorCode = 1009
|
||||||
|
FileSizeExceeded ErrorCode = 1010
|
||||||
|
)
|
||||||
|
|
||||||
|
type ErrorMap map[ErrorCode]string
|
||||||
|
|
||||||
|
var ErrMap = ErrorMap{
|
||||||
|
1001: "系统错误",
|
||||||
|
1002: "参数错误",
|
||||||
|
1003: "参数为空",
|
||||||
|
1004: "参数格式错误",
|
||||||
|
1005: "参数值错误",
|
||||||
|
1006: "参数值范围错误",
|
||||||
|
1007: "参数值重复",
|
||||||
|
1008: "参数值不匹配",
|
||||||
|
1009: "参数值不唯一",
|
||||||
|
1010: "超出文件上传大小限制",
|
||||||
|
}
|
55
common/result/result.go
Normal file
55
common/result/result.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package result
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Response struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Data interface{} `json:"data"`
|
||||||
|
Success bool `json:"success"`
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
SUCCESS = 0
|
||||||
|
FAIL = -1
|
||||||
|
)
|
||||||
|
|
||||||
|
func Result(code int, msg string, data any, success bool, c *gin.Context) {
|
||||||
|
c.JSON(http.StatusOK, Response{
|
||||||
|
Code: code,
|
||||||
|
Message: msg,
|
||||||
|
Data: data,
|
||||||
|
Success: success,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
func OK(msg string, data any, c *gin.Context) {
|
||||||
|
Result(SUCCESS, msg, data, true, c)
|
||||||
|
}
|
||||||
|
func OkWithData(data any, c *gin.Context) {
|
||||||
|
Result(SUCCESS, "success", data, true, c)
|
||||||
|
}
|
||||||
|
func OkWithMessage(msg string, c *gin.Context) {
|
||||||
|
Result(SUCCESS, msg, nil, true, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fail(msg string, data any, c *gin.Context) {
|
||||||
|
Result(FAIL, msg, data, false, c)
|
||||||
|
}
|
||||||
|
func FailWithMessage(msg string, c *gin.Context) {
|
||||||
|
Result(FAIL, msg, nil, false, c)
|
||||||
|
}
|
||||||
|
func FailWithData(data any, c *gin.Context) {
|
||||||
|
Result(FAIL, "fail", data, false, c)
|
||||||
|
|
||||||
|
}
|
||||||
|
func FailWithCode(code ErrorCode, c *gin.Context) {
|
||||||
|
msg, ok := ErrMap[code]
|
||||||
|
if ok {
|
||||||
|
Result(int(code), msg, nil, false, c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Result(FAIL, "未匹配到对应的error code", nil, false, c)
|
||||||
|
}
|
@@ -1,10 +1,13 @@
|
|||||||
package modules
|
package modules
|
||||||
|
|
||||||
import "github.com/gin-gonic/gin"
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"schisandra-cloud-album/common/result"
|
||||||
|
)
|
||||||
|
|
||||||
func AuthRouter(router *gin.RouterGroup) {
|
func AuthRouter(router *gin.RouterGroup) {
|
||||||
group := router.Group("auth")
|
group := router.Group("auth")
|
||||||
group.GET("/user", func(c *gin.Context) {
|
group.GET("/user", func(c *gin.Context) {
|
||||||
|
result.FailWithCode(result.SystemError, c)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user