🆕接入验证码组件
This commit is contained in:
44
utils/gin_response.go
Normal file
44
utils/gin_response.go
Normal file
@@ -0,0 +1,44 @@
|
||||
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) OKWithData(data any) {
|
||||
r.c.JSON(http.StatusOK, gin.H{
|
||||
"code": http.StatusOK,
|
||||
"message": "success",
|
||||
"data": data,
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user