/
retcode.go
156 lines (128 loc) · 3.68 KB
/
retcode.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package utils
import (
"fmt"
)
// retCodeDict return code directory
var retCodeDict = map[string]string{
"0000": "成功",
"9999": "未知错误",
}
// AjaxReturn struct
type AjaxReturn struct {
RetCode string `json:"retcode"`
RetMsg string `json:"retmsg"`
}
// // AjaxReturnWithData struct
// type AjaxReturnWithData struct {
// RetCode string `json:"retcode"`
// RetMsg string `json:"retmsg"`
// RetData interface{} `json:"retdata"`
// }
// AjaxReturnWithData struct
type AjaxReturnWithData struct {
AjaxReturn
RetData interface{} `json:"retdata"`
}
// GetRetMsg func(retcode string) string
// Get Return Message By Return Code
func GetRetMsg(retcode string) string {
return retCodeDict[retcode]
}
/*
GetAjaxRetObj func(retcode string, errmsg error) AjaxReturn
Get return code and errors
Convert to AjaxReturn struct object
*/
func GetAjaxRetObj(retcode string, errmsg error) AjaxReturn {
var retobj AjaxReturn
if errmsg != nil {
retobj = AjaxReturn{
RetCode: retcode,
RetMsg: fmt.Sprintf("%v, %v", GetRetMsg(retcode), errmsg),
}
} else {
retobj = AjaxReturn{
RetCode: retcode,
RetMsg: GetRetMsg(retcode),
}
}
return retobj
}
/*
GetAjaxRetJSON func(retcode string, errmsg error) []byte
Get return code and errors
Convert to AjaxReturn struct and Marshal to JSON
*/
func GetAjaxRetJSON(retcode string, errmsg error) []byte {
var retobj AjaxReturn
if errmsg != nil {
retobj = AjaxReturn{
RetCode: retcode,
RetMsg: fmt.Sprintf("%v, %v", GetRetMsg(retcode), errmsg.Error()),
}
} else {
retobj = AjaxReturn{
RetCode: retcode,
RetMsg: GetRetMsg(retcode),
}
}
ret := Convert2JSON(retobj)
return ret
}
/*
GetAjaxRetWithDataObj func(retcode string, errmsg error, data []byte) AjaxReturnWithData
Get return message and data
Convert to AjaxReturnWithData struct object
*/
func GetAjaxRetWithDataObj(retcode string, errmsg error, data interface{}) AjaxReturnWithData {
var retobj AjaxReturnWithData
if errmsg != nil {
retobj.RetCode = retcode
retobj.RetMsg = fmt.Sprintf("%v, %v", GetRetMsg(retcode), errmsg)
retobj.RetData = data
// retobj = AjaxReturnWithData{
// RetCode: retcode,
// RetMsg: fmt.Sprintf("%v, %v", GetRetMsg(retcode), errmsg),
// RetData: data,
// }
} else {
retobj.RetCode = retcode
retobj.RetMsg = GetRetMsg(retcode)
retobj.RetData = data
// retobj = AjaxReturnWithData{
// RetCode: retcode,
// RetMsg: GetRetMsg(retcode),
// RetData: data,
// }
}
return retobj
}
/*
GetAjaxRetWithDataJSON func(retcode string, errmsg error, data []byte) []byte
Get return message and data
Convert to AjaxReturnWithData struct and Marshal to JSON
*/
func GetAjaxRetWithDataJSON(retcode string, errmsg error, data interface{}) []byte {
var retobj AjaxReturnWithData
if errmsg != nil {
retobj.RetCode = retcode
retobj.RetMsg = fmt.Sprintf("%v, %v", GetRetMsg(retcode), errmsg)
retobj.RetData = data
// retobj = AjaxReturnWithData{
// RetCode: retcode,
// RetMsg: fmt.Sprintf("%v, %v", GetRetMsg(retcode), errmsg),
// RetData: data,
// }
} else {
retobj.RetCode = retcode
retobj.RetMsg = GetRetMsg(retcode)
retobj.RetData = data
// retobj = AjaxReturnWithData{
// RetCode: retcode,
// RetMsg: GetRetMsg(retcode),
// RetData: data,
// }
}
ret := Convert2JSON(retobj)
return ret
}