// 【出发点】
// 装饰器 封装通用逻辑
func merCtrlProcessor(c *gin.Context, req generic.CommonReqInterface, resp *generic.CommonRespInterface, f func()) {
merAddCommonInfo(c, req)
if err := c.ShouldBindJSON(req); err != nil {
errs := validate.ReadableError(err)
c.AbortWithStatusJSON(http.StatusNotAcceptable, errs)
return
}
transLog.FrontIn(time.Now(), req.(model.Trace), req) // ###
f()
baseResp := (*resp).Get()
// baseResp.Base = baseReq.Base
transLog.FrontOut(time.Now(), (*resp).(model.Trace), resp) // ###
c.Set(common.HTTP_CODE, baseResp.HttpStatus)
c.Set(common.RESP_ITF, resp)
}
//-------------------------------
type CommonReqInterface interface {
Get() *CommonRequest
}
func (r *CommonRequest) Get() *CommonRequest {
return r
}
type CommonRequest struct {
MsgID string `json:"-" bson:"-"`
TransType string `json:"-" bson:"-"`
ApiVer string `json:"-" bson:"-"`
ApiMode string `json:"-" bson:"-"`
GwID string `json:"-" bson:"-"`
CID string `json:"-" bson:"-"`
OID string `json:"-" bson:"-"`
SID string `json:"-" bson:"-"`
TID string `json:"-" bson:"-"`
InsCode string `json:"-" bson:"-"`
}
// -------
type CommonRespInterface interface {
Get() *CommonResponse
}
func (r *CommonResponse) Get() *CommonResponse {
return r
}
type CommonResponse struct {
MsgID string `json:"-" bson:"-"`
TransType string `json:"-" bson:"-"`
HttpStatus int `json:"-" bson:"-"`
OrderNum string `json:"-" bson:"-"`
}
//-------------------------------
type Trace interface {
TraceInfo() TraceInfo
}
func (r *ApplePayReq) TraceInfo() model.TraceInfo {
return model.TraceInfo{
TransId: r.OrderNum,
OrigTransId: "",
TransType: r.TransType,
}
}
// 附注Trace接口不同的实现类,发现其中字段的赋值不一样,这也体现了“transLog.FrontIn(time.Now(), req.(model.Trace), req)”这样做的意义
// func (r *AddPaymentMethodReq) TraceInfo() model.TraceInfo {
// return model.TraceInfo{
// TransId: r.MsgID,
// OrigTransId: "",
// TransType: r.TransType,
// }
// }
type TraceInfo struct {
TransId string
OrigTransId string
TransType string
}
//-------------------------------
func FrontIn(transTime time.Time, trace model.Trace, data interface{}) {
insert(transTime, _FRONT, _IN, trace, data)
}
func insert(transTime time.Time, msgType int, direction string, trace model.Trace, data interface{}) {
info := trace.TraceInfo()
l := &model.TransLog{
TransTime: transTime,
MsgType: msgType,
Direction: direction,
TransId: info.TransId,
OrigTransId: info.OrigTransId,
TransType: info.TransType,
Msg: data,
}
logChan <- l
}
//-------------------------------