mirror of https://github.com/Xhofe/alist
49 lines
806 B
Go
49 lines
806 B
Go
![]() |
package common
|
||
|
|
||
|
import (
|
||
|
"github.com/gin-gonic/gin"
|
||
|
log "github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
type Resp struct {
|
||
|
Code int `json:"code"`
|
||
|
Message string `json:"message"`
|
||
|
Data interface{} `json:"data"`
|
||
|
}
|
||
|
|
||
|
func ErrorResp(c *gin.Context, err error, code int) {
|
||
|
log.Error(err.Error())
|
||
|
c.JSON(200, Resp{
|
||
|
Code: code,
|
||
|
Message: err.Error(),
|
||
|
Data: nil,
|
||
|
})
|
||
|
c.Abort()
|
||
|
}
|
||
|
|
||
|
func ErrorStrResp(c *gin.Context, str string, code int) {
|
||
|
log.Error(str)
|
||
|
c.JSON(200, Resp{
|
||
|
Code: code,
|
||
|
Message: str,
|
||
|
Data: nil,
|
||
|
})
|
||
|
c.Abort()
|
||
|
}
|
||
|
|
||
|
func SuccessResp(c *gin.Context, data ...interface{}) {
|
||
|
if len(data) == 0 {
|
||
|
c.JSON(200, Resp{
|
||
|
Code: 200,
|
||
|
Message: "success",
|
||
|
Data: nil,
|
||
|
})
|
||
|
return
|
||
|
}
|
||
|
c.JSON(200, Resp{
|
||
|
Code: 200,
|
||
|
Message: "success",
|
||
|
Data: data[0],
|
||
|
})
|
||
|
}
|