feat 初次提交
This commit is contained in:
47
net/http/http_resp/code.go
Normal file
47
net/http/http_resp/code.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package http_resp
|
||||
|
||||
import (
|
||||
"common/proto/ss/ss_common"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
var (
|
||||
OK = NewCode(0, "OK")
|
||||
Failed = NewCode(1, "Failed")
|
||||
TokenInvalid = NewCode(2, "Token无效")
|
||||
ParamError = NewCode(1001, "参数错误")
|
||||
NameEmpty = NewCode(1002, "名称不能为空")
|
||||
NameDuplicate = NewCode(1003, "名称或编号不能重复")
|
||||
ListEmpty = NewCode(1004, "列表不能为空")
|
||||
RepeatCommit = NewCode(1005, "请勿重复提交")
|
||||
)
|
||||
|
||||
type Code struct {
|
||||
code int
|
||||
error string
|
||||
}
|
||||
|
||||
func NewCode(code int, error string) *Code {
|
||||
return &Code{
|
||||
code: code,
|
||||
error: error,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Code) Code() int {
|
||||
return c.code
|
||||
}
|
||||
|
||||
func (c *Code) Error() string {
|
||||
return c.error
|
||||
}
|
||||
|
||||
func (c *Code) Wrap() error {
|
||||
st := status.New(codes.Unknown, c.Error())
|
||||
st, _ = st.WithDetails(&ss_common.ErrorInfo{
|
||||
Code: int32(c.Code()),
|
||||
Msg: c.Error(),
|
||||
})
|
||||
return st.Err()
|
||||
}
|
||||
47
net/http/http_resp/response.go
Normal file
47
net/http/http_resp/response.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package http_resp
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type RespJsonData struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func Success(data interface{}) *RespJsonData {
|
||||
return &RespJsonData{
|
||||
Code: OK.Code(),
|
||||
Msg: OK.Error(),
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func Error(code *Code) *RespJsonData {
|
||||
return &RespJsonData{
|
||||
Code: code.Code(),
|
||||
Msg: code.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
func JsonOK(c *gin.Context, data *RespJsonData) {
|
||||
c.JSON(http.StatusOK, data)
|
||||
}
|
||||
|
||||
func JsonBadRequest(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, Error(ParamError))
|
||||
}
|
||||
|
||||
func JsonMethodNotAllowed(c *gin.Context) {
|
||||
c.JSON(http.StatusMethodNotAllowed, Error(NewCode(Failed.Code(), "Method Not Allowed")))
|
||||
}
|
||||
|
||||
func JsonNotFound(c *gin.Context) {
|
||||
c.JSON(http.StatusNotFound, Error(NewCode(Failed.Code(), "Endpoint Not Found")))
|
||||
}
|
||||
|
||||
func AbortUnauthorized(c *gin.Context) {
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, Error(NewCode(Failed.Code(), "Invalid Authorization")))
|
||||
}
|
||||
Reference in New Issue
Block a user