package getWay import ( "encoding/json" "fmt" "git.168cad.top/zhengqiuyun/rym-util/conf" "git.168cad.top/zhengqiuyun/rym-util/es" "git.168cad.top/zhengqiuyun/rym-util/log" db "git.168cad.top/zhengqiuyun/rym-util/mysqlrym" "git.168cad.top/zhengqiuyun/rym-util/redis" "git.168cad.top/zhengqiuyun/rym-util/util" "github.com/gin-gonic/gin" "net/http" "time" ) type ServerResponse struct { Code State `json:"code"` //0为成功,其他都是失败 Msg string `json:"msg,omitempty"` //非1000情况下的错误描述 } type ServerDataResponse struct { ServerResponse Data interface{} `json:"data,omitempty"` } func response(state State) ServerResponse { return ServerResponse{Code: state, Msg: state.String()} } func FailGetWay(c *gin.Context, state State) { resp := response(state) c.JSON(http.StatusBadGateway, resp) c.Abort() logEnd(c, resp) } func FailAndMsg(c *gin.Context, msg string) { db.Rollback() resp := ServerResponse{Code: BusinessError, Msg: msg} c.JSON(http.StatusOK, resp) c.Abort() logEnd(c, resp) } func Success(c *gin.Context) { db.Commit() resp := response(OK) c.JSON(http.StatusOK, resp) c.Abort() logEnd(c, resp) } func SuccessData(c *gin.Context, data interface{}) { db.Commit() resp := ServerDataResponse{response(OK), data} c.JSON(http.StatusOK, resp) c.Abort() logEnd(c, resp) } type WxResponse struct { Code string `json:"code"` Message string `json:"message"` } func WxSuccess(c *gin.Context) { resp := WxResponse{"SUCCESS", "成功"} c.JSON(http.StatusOK, resp) logEnd(c, resp) } func AliSuccess(c *gin.Context) { resp := "success" c.JSON(http.StatusOK, resp) logEnd(c, resp) } func logEnd(c *gin.Context, data interface{}) { esSwitch := redis.SwitchOpen(redis.ES日志开关 + conf.ServerName) startTime := GetStartTime(c) endTime := time.Now().UnixNano() var responseStr string if data != nil { dataJson, _ := json.Marshal(data) rs := string(dataJson) excludeUrl := []string{""} if esSwitch { if util.ArrayContains(excludeUrl, GetURL(c)) && len(rs) > conf.LogResponseLength { responseStr = rs[0:conf.LogResponseLength] + "......" } else { responseStr = rs } } else { if len(rs) > conf.LogResponseLength { responseStr = rs[0:conf.LogResponseLength] + "......" } else { responseStr = rs } } } token := GetToken(c) if esSwitch && token != "" { esLog(c, responseStr) log.Info(fmt.Sprintf("结束,耗时%d毫秒", (endTime-startTime)/1e6)) } else { log.Info(fmt.Sprintf("返回数据:%s", responseStr)) log.Info(fmt.Sprintf("结束,耗时%d毫秒 %s %s %s %s %s", (endTime-startTime)/1e6, GetMethod(c), GetURL(c), token, GetClientIP(c), GetParams(c))) } } func esLog(c *gin.Context, response string) { log := es.ServerLogModel{ ServerName: conf.ServerName, Env: conf.Env, TimeStamp: util.MilSecond(time.Now()), ThreadNo: fmt.Sprintf("%d", util.GetGID()), ServerIp: GetServerIP(), ClientIp: GetClientIP(c), Token: GetToken(c), CustomerId: GetCurrentUser(c).Id, ClientSource: GetClientSource(c), Url: GetURL(c), Method: GetMethod(c), Request: GetParams(c), Response: response, DealTimes: util.MilSecond(time.Now()) - GetStartTime(c)/1e6, } es.InsertEsLog(log) }