-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
386 lines (358 loc) · 9.36 KB
/
report.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package Router2Mid
import (
"fmt"
CoreLanguage "github.com/fotomxq/weeekj_core/v5/core/language"
CoreLog "github.com/fotomxq/weeekj_core/v5/core/log"
OrgCore "github.com/fotomxq/weeekj_core/v5/org/core"
UserCore "github.com/fotomxq/weeekj_core/v5/user/core"
"github.com/gin-gonic/gin"
"net/http"
)
// 内部反馈头上下文
type reportContext struct {
//总的上下文
Context any
//路由上下文
RouterContext *gin.Context
//业务上下文
HeaderContext DataGetContextData
//反馈头部类型
HttpCode int
//反馈编码
Code string
//状态
Status bool
//错误
Err error
//消息
Msg string
//覆盖消息
ReplaceMsg string
//数量集合
Count int64
//数据集合
Data any
}
// reportDataType 通用数据反馈头
type reportDataType struct {
//错误信息
Status bool `json:"status"`
//错误信息
Code string `json:"code"`
//错误描述
Msg string `json:"msg"`
//数据个数
Count int64 `json:"count"`
//数据集合
Data any `json:"data"`
}
// ReportErrorBadRequest 反馈错误但不带JSON重新标定
func ReportErrorBadRequest(context any, code string) {
//实现实例
ctx := reportGetCtx(context, http.StatusBadRequest, nil, "", false, code, 0, nil)
//反馈数据
reportBaseReport(&ctx)
}
func ReportErrorBadRequestLog(context any, message string, err error, code string) {
//实现实例
ctx := reportGetCtx(context, http.StatusBadRequest, err, message, false, code, 0, nil)
//反馈数据
reportLogWarn(&ctx)
}
func ReportErrorBadRequestLogToParams(context any, message string, err error, code string, newMsg string) {
//实现实例
ctx := reportGetCtx(context, http.StatusBadRequest, err, message, false, code, 0, nil)
ctx.ReplaceMsg = newMsg
//反馈数据
reportLogWarn(&ctx)
}
// ReportErrorLog 反馈错误并抛出日志
func ReportErrorLog(context any, message string, err error, code string) {
//实现实例
ctx := reportGetCtx(context, 0, err, message, false, code, 0, nil)
//反馈数据
reportLogErr(&ctx)
}
// ReportWarnLog 反馈警告并抛出错误
func ReportWarnLog(context any, message string, err error, code string) {
//实现实例
ctx := reportGetCtx(context, 0, err, message, false, code, 0, nil)
//反馈数据
reportLogWarn(&ctx)
}
func ReportActionCreateNoData(context any, logMsg string, err error, code string) {
//修正code
if err != nil && code == "" {
code = "report_create_failed"
}
if err == nil {
code = ""
}
//实现实例
ctx := reportGetCtx(context, 0, err, logMsg, false, code, 0, nil)
//判断错误
ctx.Status = ctx.Err == nil
//反馈数据
reportAuto(&ctx)
}
func ReportActionCreate(context any, logMsg string, err error, code string, data interface{}) {
//修正code
if err != nil && code == "" {
code = "report_create_failed"
}
if err == nil {
code = ""
}
//实现实例
ctx := reportGetCtx(context, 0, err, logMsg, false, code, 0, data)
//判断错误
ctx.Status = ctx.Err == nil
//反馈数据
reportAuto(&ctx)
}
func ReportActionUpdate(context any, logMsg string, err error, code string) {
//修正code
if err != nil && code == "" {
code = "report_update_failed"
}
if err == nil {
code = ""
}
//实现实例
ctx := reportGetCtx(context, 0, err, logMsg, false, code, 0, nil)
//判断错误
ctx.Status = ctx.Err == nil
//反馈数据
reportAuto(&ctx)
}
func ReportActionDelete(context any, logMsg string, err error, code string) {
//修正code
if err != nil && code == "" {
code = "report_delete_failed"
}
if err == nil {
code = ""
}
//实现实例
ctx := reportGetCtx(context, 0, err, logMsg, false, code, 0, nil)
//判断错误
ctx.Status = ctx.Err == nil
//反馈数据
reportAuto(&ctx)
}
// ReportData 通用反馈单一数据
func ReportData(context any, errMessage string, err error, code string, data interface{}) {
//修正code
if err != nil && code == "" {
code = "report_data_empty"
}
if err == nil {
code = ""
}
//实现实例
ctx := reportGetCtx(context, 0, err, errMessage, false, code, 0, data)
//判断错误
ctx.Status = ctx.Err == nil
//反馈数据
reportAuto(&ctx)
}
func ReportDataNoErr(context any, err error, code string, data interface{}) {
//修正code
if err != nil && code == "" {
code = "report_data_empty"
}
if err == nil {
code = ""
}
//实现实例
ctx := reportGetCtx(context, 0, err, "", false, code, 0, data)
//判断错误
ctx.Status = true
//反馈数据
reportAuto(&ctx)
}
// ReportDataList 通用反馈列表方案
func ReportDataList(context any, errMessage string, err error, code string, dataList interface{}, dataCount int64) {
//修正code
if err != nil && code == "" {
code = "report_data_empty"
}
if err == nil {
code = ""
}
//实现实例
ctx := reportGetCtx(context, 0, err, "", false, code, dataCount, dataList)
//判断错误
ctx.Status = ctx.Err == nil
//反馈数据
reportAuto(&ctx)
}
// ReportBaseSuccess 反馈成功
func ReportBaseSuccess(context any) {
//实现实例
ctx := reportGetCtx(context, 0, nil, "", true, "", 0, nil)
//反馈数据
reportAuto(&ctx)
}
// ReportBaseBool 反馈成功或失败
func ReportBaseBool(context any, code string, b bool) {
if b {
code = ""
}
//实现实例
ctx := reportGetCtx(context, 0, nil, "", b, code, 0, nil)
//反馈数据
reportAuto(&ctx)
}
// BaseData 反馈一般数据
func BaseData(context any, data interface{}) {
//实现实例
ctx := reportGetCtx(context, 0, nil, "", true, "", 0, data)
//反馈数据
reportAuto(&ctx)
}
// BaseDataCount 反馈数量类数据
func BaseDataCount(context any, count int64) {
//实现实例
ctx := reportGetCtx(context, 0, nil, "", true, "", count, nil)
//反馈数据
reportAuto(&ctx)
}
// ReportBaseDataList 反馈列队数据
func ReportBaseDataList(context any, count int64, data interface{}) {
//实现实例
ctx := reportGetCtx(context, 0, nil, "", true, "", count, data)
//反馈数据
reportAuto(&ctx)
}
// ReportBaseError 反馈错误
func ReportBaseError(context any, code string) {
//修正错误代码
if code == "" {
code = "report_error"
}
//实现实例
ctx := reportGetCtx(context, 0, nil, "", false, code, 0, nil)
//反馈数据
reportAuto(&ctx)
}
// reportGetLogMsg 日志内部附加处理
func reportGetLogMsg(c *gin.Context, cData DataGetContextData, msg string) string {
//捕捉异常
defer func() {
if r := recover(); r != nil {
CoreLog.Error("log failed,", r, ",url:", c.Request.RequestURI, ",", cData.LogAppend, ",msg:", msg)
}
}()
appendMsg := fmt.Sprint("url:", c.Request.RequestURI, ".")
if cData.LogAppend != "" {
appendMsg = fmt.Sprint(appendMsg, cData.LogAppend, ".")
}
userDataIn, b := c.Get("UserData")
if b {
userData := userDataIn.(UserCore.DataUserDataType)
appendMsg = fmt.Sprint("userID:", userData.Info.ID, ",userPhone:", userData.Info.Phone, ",", appendMsg)
}
orgDataIn, b := c.Get("OrgData")
if b {
orgData := orgDataIn.(OrgCore.FieldsOrg)
appendMsg = fmt.Sprint("orgID:", orgData.ID, ",", appendMsg)
}
orgBindDataIn, b := c.Get("OrgBindData")
if b {
bindData := orgBindDataIn.(OrgCore.FieldsBind)
appendMsg = fmt.Sprint("orgBindID:", bindData.ID, ",", appendMsg)
}
if appendMsg != "" {
msg = fmt.Sprint(appendMsg, msg)
}
return msg
}
// reportLogWarn 反馈警告错误
func reportLogWarn(ctx *reportContext) {
//输出日志
if ctx.Err != nil {
CoreLog.Warn(reportGetLogMsg(ctx.RouterContext, ctx.HeaderContext, ctx.Msg), ",", ctx.Err)
} else {
CoreLog.Warn(reportGetLogMsg(ctx.RouterContext, ctx.HeaderContext, ctx.Msg))
}
//反馈
reportBaseReport(ctx)
}
// reportLogErr 反馈错误
func reportLogErr(ctx *reportContext) {
//输出日志
if ctx.Err != nil {
CoreLog.Error(reportGetLogMsg(ctx.RouterContext, ctx.HeaderContext, ctx.Msg), ",", ctx.Err)
} else {
CoreLog.Error(reportGetLogMsg(ctx.RouterContext, ctx.HeaderContext, ctx.Msg))
}
//反馈
reportBaseReport(ctx)
}
// reportAuto 自动失败是否失败并反馈
func reportAuto(ctx *reportContext) {
if ctx.Err != nil {
reportLogWarn(ctx)
return
}
reportBaseReport(ctx)
}
// reportBaseReport 通过反馈结构
func reportBaseReport(ctx *reportContext) {
//反馈数据
msg := ""
if ctx.ReplaceMsg != "" {
msg = ctx.ReplaceMsg
} else {
if ctx.Code != "" {
msg = CoreLanguage.GetLanguageText(ctx.RouterContext, ctx.Code)
}
}
res := reportDataType{
Status: ctx.Status,
Code: ctx.Code,
Msg: msg,
Count: ctx.Count,
Data: ctx.Data,
}
ctx.RouterContext.JSON(ctx.HttpCode, &res)
ctx.RouterContext.Abort()
}
// reportGin gin直接反馈头
func reportGin(ctxGin *gin.Context, haveLog bool, httpCode int, err error, msg string, status bool, code string, count int64, data interface{}) {
//实现实例
ctx := reportGetCtx(&RouterURLPublicC{
Context: ctxGin,
LogAppend: "",
}, httpCode, err, msg, status, code, count, data)
//如果需要记录日志
if haveLog {
reportAuto(&ctx)
return
}
//反馈数据
reportBaseReport(&ctx)
}
// reportGetCtx 组装上下文
func reportGetCtx(context any, httpCode int, err error, msg string, status bool, code string, count int64, data interface{}) reportContext {
//修正http状态
if httpCode < 1 {
httpCode = http.StatusOK
}
//获取上下文
ctx, ctxData := GetContextData(context)
//组装并反馈数据
return reportContext{
Context: context,
RouterContext: ctx,
HeaderContext: ctxData,
HttpCode: httpCode,
Code: code,
Status: status,
Err: err,
Msg: msg,
Count: count,
Data: data,
}
}