feat 包命名规范
This commit is contained in:
47
net/http/httpresp/response.go
Normal file
47
net/http/httpresp/response.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package httpresp
|
||||
|
||||
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(MethodNotAllowed))
|
||||
}
|
||||
|
||||
func JsonNotFound(c *gin.Context) {
|
||||
c.JSON(http.StatusNotFound, Error(NotFound))
|
||||
}
|
||||
|
||||
func JsonUnauthorized(c *gin.Context) {
|
||||
c.JSON(http.StatusUnauthorized, Error(TokenInvalid))
|
||||
}
|
||||
Reference in New Issue
Block a user