123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- package response
- import (
- "net/http"
- "github.com/gin-gonic/gin"
- )
- type Response struct {
- Code int `json:"code"`
- Data interface{} `json:"data"`
- Msg string `json:"msg"`
- }
- const (
- ERROR = 201
- SUCCESS = 200
- )
- func Result(code int, data interface{}, msg string, c *gin.Context) {
- // 开始时间
- c.JSON(http.StatusOK, Response{
- code,
- data,
- msg,
- })
- }
- func Ok(c *gin.Context) {
- Result(SUCCESS, map[string]interface{}{}, "操作成功", c)
- }
- func ReturnErr(err error, c *gin.Context) {
- if err != nil {
- FailWithError(err, c)
- } else {
- OkWithMessage("操作成功", c)
- }
- }
- func ReturnErrData(err error, data interface{}, c *gin.Context) {
- if err != nil {
- FailWithError(err, c)
- } else {
- OkWithData(data, c)
- }
- }
- func OkWithMessage(message string, c *gin.Context) {
- Result(SUCCESS, map[string]interface{}{}, message, c)
- }
- func OkWithData(data interface{}, c *gin.Context) {
- Result(SUCCESS, data, "操作成功", c)
- }
- func OkWithDetailed(data interface{}, message string, c *gin.Context) {
- Result(SUCCESS, data, message, c)
- }
- func Fail(c *gin.Context) {
- Result(ERROR, map[string]interface{}{}, "操作失败", c)
- }
- func FailWithError(err error, c *gin.Context) {
- Result(ERROR, map[string]interface{}{}, err.Error(), c)
- }
- func FailWithMessage(message string, c *gin.Context) {
- Result(ERROR, map[string]interface{}{}, message, c)
- }
- func FailWithDetailed(data interface{}, message string, c *gin.Context) {
- Result(ERROR, data, message, c)
- }
|