59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
type ginResponse struct {
|
|
c *gin.Context
|
|
}
|
|
|
|
func GinResponse(c *gin.Context) ginResponse {
|
|
return ginResponse{c: c}
|
|
}
|
|
|
|
func (r ginResponse) OK() {
|
|
r.c.JSON(http.StatusOK, gin.H{
|
|
"code": http.StatusOK,
|
|
"message": "success",
|
|
})
|
|
}
|
|
|
|
func (r ginResponse) Failed() {
|
|
r.c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": http.StatusBadRequest,
|
|
"message": "bad request",
|
|
})
|
|
}
|
|
|
|
func (r ginResponse) FailedWithErr(msg string, err error) {
|
|
r.c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": http.StatusBadRequest,
|
|
"message": fmt.Errorf("%s: %s", msg, err.Error()),
|
|
})
|
|
}
|
|
|
|
func (r ginResponse) FailedWithMsg(msg string) {
|
|
r.c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": http.StatusBadRequest,
|
|
"message": msg,
|
|
})
|
|
}
|
|
|
|
func (r ginResponse) AuthorizationFailed() {
|
|
r.c.JSON(http.StatusUnauthorized, gin.H{
|
|
"code": http.StatusUnauthorized,
|
|
"message": "请先登陆",
|
|
})
|
|
}
|
|
|
|
func (r ginResponse) OKWithData(data any) {
|
|
r.c.JSON(http.StatusOK, gin.H{
|
|
"code": http.StatusOK,
|
|
"message": "success",
|
|
"data": data,
|
|
})
|
|
}
|