代码优化

This commit is contained in:
meilin.huang
2021-04-21 10:22:09 +08:00
parent ec1001d88b
commit d5fc1b6f52
14 changed files with 64 additions and 226 deletions

View File

@@ -23,13 +23,12 @@ func init() {
}
type LogInfo struct {
NeedLog bool // 是否需要记录日志
LogResp bool // 是否记录返回结果
Description string // 请求描述
}
func NewLogInfo(description string) *LogInfo {
return &LogInfo{NeedLog: true, Description: description, LogResp: false}
return &LogInfo{Description: description, LogResp: false}
}
func (i *LogInfo) WithLogResp(logResp bool) *LogInfo {
@@ -39,7 +38,7 @@ func (i *LogInfo) WithLogResp(logResp bool) *LogInfo {
func (l *LogInfo) AfterHandle(rc *ReqCtx) {
li := rc.LogInfo
if li == nil || !li.NeedLog {
if li == nil {
return
}
@@ -60,7 +59,7 @@ func (l *LogInfo) AfterHandle(rc *ReqCtx) {
}
func getLogMsg(rc *ReqCtx) string {
msg := rc.LogInfo.Description
msg := rc.LogInfo.Description + fmt.Sprintf(" ->%dms", rc.timed)
if !utils.IsBlank(reflect.ValueOf(rc.ReqParam)) {
rb, _ := json.Marshal(rc.ReqParam)
msg = msg + fmt.Sprintf("\n--> %s", string(rb))

View File

@@ -1,15 +0,0 @@
package ctx
type AppContext struct {
}
type LoginAccount struct {
Id uint64
Username string
}
type Permission struct {
CheckToken bool // 是否检查token
Code string // 权限码
Name string // 描述
}

View File

@@ -4,16 +4,11 @@ import (
"mayfly-go/base/ginx"
"mayfly-go/base/model"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
// // 获取数据函数
// type getDataFunc func(*ReqCtx) interface{}
// // 操作函数,无返回数据
// type operationFunc func(*ReqCtx)
// 处理函数
type HandlerFunc func(*ReqCtx)
@@ -28,6 +23,7 @@ type ReqCtx struct {
ReqParam interface{} // 请求参数,主要用于记录日志
ResData interface{} // 响应结果
err interface{} // 请求错误
timed int64 // 执行时间
}
func (rc *ReqCtx) Handle(handler HandlerFunc) {
@@ -56,7 +52,9 @@ func (rc *ReqCtx) Handle(handler HandlerFunc) {
panic(err)
}
begin := time.Now()
handler(rc)
rc.timed = time.Now().Sub(begin).Milliseconds()
ginx.SuccessRes(ginCtx, rc.ResData)
}