Go语言设计统一接口响应格式

自己封装统一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
/**
* @Author: hqd8080
* @Description: response,响应处理
* @Date: 2022-03-28 10:53
*/

package resp

import (
"go-vueadmin/pkg/errcode"
"github.com/gin-gonic/gin"
)

// Response 统一格式返回
type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}

// PaginationParam 分页参数
type PaginationParam struct {
List interface{} `json:"list"`
Count int64 `json:"count"`
PageIndex int `json:"page_index"`
PageSize int `json:"page_size"`
}

// PageResponse 分页统一格式
type PageResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data PaginationParam `json:"data"`
}

// Correct 正常返回数据,无分页
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)
}

// CorrectOK 正常返回分页数据
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)
}

// ErrorCode 可以定制返回错误码,错误信息
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
/**
* @Author: hqd8080
* @Description: errcode,公共错误码标准化
* @Date: 2022-03-28 09:43
*/

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
/**
* @Author: hqd8080
* @Description: errmsg,公共错误码标准化
* @Date: 2022-03-28 10:07
*/

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)
}