diff --git a/utils/golog/api_mongo.go b/utils/golog/api_mongo.go index fa181ed7..73f4bacb 100644 --- a/utils/golog/api_mongo.go +++ b/utils/golog/api_mongo.go @@ -51,53 +51,53 @@ func (c *ApiClient) MongoQuery() *dorm.MongoClient { // MongoMiddleware 中间件 func (c *ApiClient) MongoMiddleware(request gorequest.Response) { c.mongoRecord(apiMongoLog{ - RequestTime: gotime.SetCurrent(request.RequestTime).Timestamp(), //【请求】时间 - RequestUri: request.RequestUri, //【请求】链接 - RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 - RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口 - RequestMethod: request.RequestMethod, //【请求】方式 - RequestParams: request.RequestParams, //【请求】参数 - RequestHeader: request.RequestHeader, //【请求】头部 - ResponseHeader: request.ResponseHeader, //【返回】头部 - ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 - ResponseBody: request.ResponseBody, //【返回】内容 - ResponseContentLength: request.ResponseContentLength, //【返回】大小 - ResponseTime: gotime.SetCurrent(request.ResponseTime).Timestamp(), //【返回】时间 + RequestTime: gotime.SetCurrent(request.RequestTime).Timestamp, //【请求】时间 + RequestUri: request.RequestUri, //【请求】链接 + RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 + RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口 + RequestMethod: request.RequestMethod, //【请求】方式 + RequestParams: request.RequestParams, //【请求】参数 + RequestHeader: request.RequestHeader, //【请求】头部 + ResponseHeader: request.ResponseHeader, //【返回】头部 + ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 + ResponseBody: request.ResponseBody, //【返回】内容 + ResponseContentLength: request.ResponseContentLength, //【返回】大小 + ResponseTime: gotime.SetCurrent(request.ResponseTime).Timestamp, //【返回】时间 }) } // MongoMiddlewareXml 中间件 func (c *ApiClient) MongoMiddlewareXml(request gorequest.Response) { c.mongoRecord(apiMongoLog{ - RequestTime: gotime.SetCurrent(request.RequestTime).Timestamp(), //【请求】时间 - RequestUri: request.RequestUri, //【请求】链接 - RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 - RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口 - RequestMethod: request.RequestMethod, //【请求】方式 - RequestParams: request.RequestParams, //【请求】参数 - RequestHeader: request.RequestHeader, //【请求】头部 - ResponseHeader: request.ResponseHeader, //【返回】头部 - ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 - ResponseBody: dorm.XmlDecodeNoError(request.ResponseBody), //【返回】内容 - ResponseContentLength: request.ResponseContentLength, //【返回】大小 - ResponseTime: gotime.SetCurrent(request.ResponseTime).Timestamp(), //【返回】时间 + RequestTime: gotime.SetCurrent(request.RequestTime).Timestamp, //【请求】时间 + RequestUri: request.RequestUri, //【请求】链接 + RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 + RequestApi: gorequest.UriParse(request.RequestUri).Path, //【请求】接口 + RequestMethod: request.RequestMethod, //【请求】方式 + RequestParams: request.RequestParams, //【请求】参数 + RequestHeader: request.RequestHeader, //【请求】头部 + ResponseHeader: request.ResponseHeader, //【返回】头部 + ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 + ResponseBody: dorm.XmlDecodeNoError(request.ResponseBody), //【返回】内容 + ResponseContentLength: request.ResponseContentLength, //【返回】大小 + ResponseTime: gotime.SetCurrent(request.ResponseTime).Timestamp, //【返回】时间 }) } // MongoMiddlewareCustom 中间件 func (c *ApiClient) MongoMiddlewareCustom(api string, request gorequest.Response) { c.mongoRecord(apiMongoLog{ - RequestTime: gotime.SetCurrent(request.RequestTime).Timestamp(), //【请求】时间 - RequestUri: request.RequestUri, //【请求】链接 - RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 - RequestApi: api, //【请求】接口 - RequestMethod: request.RequestMethod, //【请求】方式 - RequestParams: request.RequestParams, //【请求】参数 - RequestHeader: request.RequestHeader, //【请求】头部 - ResponseHeader: request.ResponseHeader, //【返回】头部 - ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 - ResponseBody: request.ResponseBody, //【返回】内容 - ResponseContentLength: request.ResponseContentLength, //【返回】大小 - ResponseTime: gotime.SetCurrent(request.ResponseTime).Timestamp(), //【返回】时间 + RequestTime: gotime.SetCurrent(request.RequestTime).Timestamp, //【请求】时间 + RequestUri: request.RequestUri, //【请求】链接 + RequestUrl: gorequest.UriParse(request.RequestUri).Url, //【请求】链接 + RequestApi: api, //【请求】接口 + RequestMethod: request.RequestMethod, //【请求】方式 + RequestParams: request.RequestParams, //【请求】参数 + RequestHeader: request.RequestHeader, //【请求】头部 + ResponseHeader: request.ResponseHeader, //【返回】头部 + ResponseStatusCode: request.ResponseStatusCode, //【返回】状态码 + ResponseBody: request.ResponseBody, //【返回】内容 + ResponseContentLength: request.ResponseContentLength, //【返回】大小 + ResponseTime: gotime.SetCurrent(request.ResponseTime).Timestamp, //【返回】时间 }) } diff --git a/utils/golog/gin_mongo.go b/utils/golog/gin_mongo.go index 978f664a..0475371b 100644 --- a/utils/golog/gin_mongo.go +++ b/utils/golog/gin_mongo.go @@ -131,7 +131,7 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc { if len(jsonBody) > 0 { c.mongoRecord(ginMongoLog{ TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号 - RequestTime: gotime.SetCurrent(requestTime).Timestamp(), //【请求】时间 + RequestTime: gotime.SetCurrent(requestTime).Timestamp, //【请求】时间 RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接 RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接 RequestApi: gorequest.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口 @@ -148,7 +148,7 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc { RequestIpCity: requestClientIpCity, //【请求】请求客户端城市 RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商 RequestHeader: ginCtx.Request.Header, //【请求】请求头 - ResponseTime: gotime.SetCurrent(gotime.Current().Time).Timestamp(), //【返回】时间 + ResponseTime: gotime.SetCurrent(gotime.Current().Time).Timestamp, //【返回】时间 ResponseCode: responseCode, //【返回】状态码 ResponseData: c.jsonUnmarshal(responseBody), //【返回】数据 CostTime: endTime - startTime, //【系统】花费时间 @@ -156,7 +156,7 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc { } else { c.mongoRecord(ginMongoLog{ TraceId: ginCtx.MustGet("trace_id").(string), //【系统】链编号 - RequestTime: gotime.SetCurrent(requestTime).Timestamp(), //【请求】时间 + RequestTime: gotime.SetCurrent(requestTime).Timestamp, //【请求】时间 RequestUri: host + ginCtx.Request.RequestURI, //【请求】请求链接 RequestUrl: ginCtx.Request.RequestURI, //【请求】请求链接 RequestApi: gorequest.UriFilterExcludeQueryString(ginCtx.Request.RequestURI), //【请求】请求接口 @@ -173,7 +173,7 @@ func (c *GinClient) MongoMiddleware() gin.HandlerFunc { RequestIpCity: requestClientIpCity, //【请求】请求客户端城市 RequestIpIsp: requestClientIpIsp, //【请求】请求客户端运营商 RequestHeader: ginCtx.Request.Header, //【请求】请求头 - ResponseTime: gotime.SetCurrent(gotime.Current().Time).Timestamp(), //【返回】时间 + ResponseTime: gotime.SetCurrent(gotime.Current().Time).Timestamp, //【返回】时间 ResponseCode: responseCode, //【返回】状态码 ResponseData: c.jsonUnmarshal(responseBody), //【返回】数据 CostTime: endTime - startTime, //【系统】花费时间 diff --git a/utils/gotime/gotime.go b/utils/gotime/gotime.go index 3bd7dfe0..4dc0825b 100644 --- a/utils/gotime/gotime.go +++ b/utils/gotime/gotime.go @@ -23,9 +23,10 @@ const ( // Pro 结构体 type Pro struct { - Time time.Time - loc *time.Location - Error error + Time time.Time + Timestamp int64 // Unix + loc *time.Location + Error error } // NewPro 初始化结构体 diff --git a/utils/gotime/today.go b/utils/gotime/today.go index f19f505e..4917b999 100644 --- a/utils/gotime/today.go +++ b/utils/gotime/today.go @@ -9,8 +9,11 @@ import ( // Current 获取当前的时间 func Current() Pro { + p := NewPro() + p.loc, p.Error = time.LoadLocation("Asia/Shanghai") + if p.Error != nil { // Docker部署golang应用时时区问题 https://www.ddhigh.com/2018/03/01/golang-docker-timezone.html log.Printf("【gotime】时区错误:%v\n", p.Error) @@ -18,13 +21,20 @@ func Current() Pro { } else { p.Time = time.Now().In(p.loc) } + + p.Timestamp = p.Time.Unix() return p } // SetCurrent 设置当前的时间 func SetCurrent(sTime time.Time) Pro { + p := NewPro() + p.Time = sTime + + p.Timestamp = p.Time.Unix() + return p } @@ -36,15 +46,19 @@ func SetCurrentParse(str string) Pro { p.loc, p.Error = time.LoadLocation("Asia/Shanghai") layout := DateTimeFormat + if str == "" || str == "0" || str == "0000-00-00 00:00:00" || str == "0000-00-00" || str == "00:00:00" { return p } + if len(str) == 10 && strings.Count(str, "-") == 2 { layout = DateFormat } + if strings.Index(str, "T") == 10 { layout = RFC3339Format } + if _, err := strconv.ParseInt(str, 10, 64); err == nil { switch len(str) { case 8: @@ -53,16 +67,25 @@ func SetCurrentParse(str string) Pro { layout = ShortDateTimeFormat } } + location, _ := time.ParseInLocation(layout, str, p.loc) p.Time = location + + p.Timestamp = p.Time.Unix() + return p } // SetCurrentUnix 设置当前的时间 Unix时间戳 func SetCurrentUnix(ts int64) Pro { + p := NewPro() + p.Time = time.Unix(ts, 0) + + p.Timestamp = p.Time.Unix() + return p } @@ -86,11 +109,6 @@ func (p Pro) ToTimeFormat() string { return p.Time.Format(TimeFormat) } -// Timestamp 今天此刻时间戳 -func (p Pro) Timestamp() int64 { - return p.Time.Unix() -} - // TimestampWithSecond 今天此刻时间戳 func (p Pro) TimestampWithSecond() int64 { return p.Time.Unix() diff --git a/utils/gotime/tomorrow.go b/utils/gotime/tomorrow.go index e91268f5..303d135e 100644 --- a/utils/gotime/tomorrow.go +++ b/utils/gotime/tomorrow.go @@ -4,12 +4,17 @@ import "time" // Tomorrow 明天 func Tomorrow() Pro { + p := NewPro() + location, err := time.LoadLocation("Asia/Shanghai") + if err != nil { p.Time = time.Now().Add(time.Hour*8).AddDate(0, 0, +1) } else { p.Time = time.Now().In(location).AddDate(0, 0, +1) } + + p.Timestamp = p.Time.Unix() return p } diff --git a/utils/gotime/yesterday.go b/utils/gotime/yesterday.go index be1cd6c6..5bb44a61 100644 --- a/utils/gotime/yesterday.go +++ b/utils/gotime/yesterday.go @@ -6,12 +6,17 @@ import ( // Yesterday 昨天 func Yesterday() Pro { + p := NewPro() + location, err := time.LoadLocation("Asia/Shanghai") + if err != nil { p.Time = time.Now().Add(time.Hour*8).AddDate(0, 0, -1) } else { p.Time = time.Now().In(location).AddDate(0, 0, -1) } + + p.Timestamp = p.Time.Unix() return p }