自己封装统一api接口响应格式,使用Gin框架的项目可直接使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
package resp
import ( "go-vueadmin/pkg/errcode" "github.com/gin-gonic/gin" )
type Response struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` }
type PaginationParam struct { List interface{} `json:"list"` Count int64 `json:"count"` PageIndex int `json:"page_index"` PageSize int `json:"page_size"` }
type PageResponse struct { Code int `json:"code"` Msg string `json:"msg"` Data PaginationParam `json:"data"` }
func Correct(c *gin.Context, httpCode int, errCode int, data interface{}) { response := &Response{ Code: errCode, Msg: errcode.GetErrorMsg(errCode), Data: data, } c.JSON(httpCode, response) }
func CorrectOK(c *gin.Context, httpCode int, errCode int, data interface{}, count int64, pageIndex int, pageSize int) { pagination := PaginationParam{ List: data, Count: count, PageIndex: pageIndex, PageSize: pageSize, } response := &PageResponse{ Code: errCode, Msg: errcode.GetErrorMsg(errCode), Data: pagination, } c.JSON(httpCode, response) }
func ErrorCode(c *gin.Context, httpCode int, errCode int) { response := &Response{ Code: errCode, Msg: errcode.GetErrorMsg(errCode), Data: nil, } c.JSON(httpCode, response) }
|
公共错误码标准化定义
/pkg/errcode/errcode.go
/pkg/errcode/errmsg.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
package errcode
const ( ErrCodeSuccess = 2000 ErrCodeError = 5000 ErrCodeInvalidParam = 4000 ErrBannerList = 10000 ErrBannerExist = 10001 ErrBannerNotFound = 10002 ErrBannerCreate = 10003 ErrBannerUpdate = 10004 ErrBannerUpdateStatus = 10005 ErrBannerDelete = 10006 )
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
package errcode
var ErrorMsg = map[int]string{ ErrCodeSuccess: "ok", ErrCodeError: "fail", ErrCodeInvalidParam: "请求参数错误!", ErrBannerList: "获取轮播图列表失败!", ErrBannerExist: "轮播图已经存在!", ErrBannerNotFound: "轮播图不存在!", ErrBannerCreate: "添加轮播图失败!", ErrBannerUpdate: "更新轮播图失败!", ErrBannerUpdateStatus: "修改轮播图状态失败!", ErrBannerDelete: "删除轮播图失败!", }
func GetErrorMsg(code int) string { msg, ok := ErrorMsg[code] if ok { return msg } return ErrorMsg[ErrCodeError] }
|
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package admin
import ( "go-vueadmin/models" "go-vueadmin/pkg/errcode" "go-vueadmin/pkg/resp" "go-vueadmin/service" "go-vueadmin/util" "net/http" "github.com/gin-gonic/gin" )
type BannerImageController struct { BaseController }
func (b BannerImageController) GetBannerImageList(c *gin.Context) { bannerService := service.NewBannerImageService() pageIndex := bannerService.GetPageIndex(c) pageSize := bannerService.GetPageSize(c) list, count, err := bannerService.BannerList(c, pageIndex, pageSize) if err != nil { resp.ErrorCode(c, http.StatusOK, errcode.ErrBannerList) return } resp.CorrectOK(c, http.StatusOK, errcode.ErrCodeSuccess, list, count, pageIndex, pageSize) }
|