generated from NSObjects/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
85 lines (70 loc) · 1.6 KB
/
response.go
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Created by lintao on 2023/7/18 下午3:56
* Copyright © 2020-2023 LINTAO. All rights reserved.
*
*/
package resp
import (
"net/http"
"reflect"
"github.com/NSObjects/echo-admin/internal/log"
"github.com/labstack/echo/v4"
"github.com/marmotedu/errors"
)
type ListResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data ListData `json:"data"`
}
type ListData struct {
Total int64 `json:"total"`
List interface{} `json:"list" `
}
type DataResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
// APIError 返回API错误
func APIError(err error, c echo.Context) error {
if err == nil {
return errors.New("error can't be nil")
}
var rjson struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
codeError := errors.ParseCoder(err)
rjson.Code = codeError.Code()
rjson.Msg = codeError.String()
log.Error(err)
return c.JSON(codeError.HTTPStatus(), rjson)
}
func OperateSuccess(c echo.Context) error {
var rjson struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
rjson.Msg = "success"
return c.JSON(http.StatusOK, rjson)
}
func ListDataResponse(arr interface{}, total int64, c echo.Context) error {
if arr == nil {
arr = make([]interface{}, 0)
} else if reflect.ValueOf(arr).IsNil() {
arr = make([]interface{}, 0)
}
r := ListResponse{
Data: ListData{
List: arr,
Total: total,
},
}
return c.JSONPretty(http.StatusOK, r, " ")
}
func OneDataResponse(data interface{}, c echo.Context) error {
r := DataResponse{
Data: data,
}
return c.JSON(http.StatusOK, r)
}