package system import ( "context" "net/http" "strconv" "strings" "time" "management/internal/erpserver/model/dto" ) type AuditLogRepository interface { Create(ctx context.Context, obj *AuditLog) error BatchCreate(ctx context.Context, objs []*AuditLog) error Count(ctx context.Context, filter dto.SearchDto) (int64, error) List(ctx context.Context, q dto.SearchDto) ([]*AuditLog, error) } type AuditLog struct { ID int64 `db:"id" json:"id"` CreatedAt time.Time `db:"created_at" json:"created_at"` Email string `db:"email" json:"email"` StartAt time.Time `db:"start_at" json:"start_at"` EndAt time.Time `db:"end_at" json:"end_at"` Duration string `db:"duration" json:"duration"` Url string `db:"url" json:"url"` Method string `db:"method" json:"method"` Parameters string `db:"parameters" json:"parameters"` RefererUrl string `db:"referer_url" json:"referer_url"` Os string `db:"os" json:"os"` Ip string `db:"ip" json:"ip"` Browser string `db:"browser" json:"browser"` Remark string `db:"remark" json:"remark"` } func NewAuditLog(r *http.Request, email, os, browser string, start, end time.Time) *AuditLog { var params string method := r.Method if method == "GET" { params = r.URL.Query().Encode() } else if method == "POST" { contentType := r.Header.Get("Content-Type") if strings.Contains(contentType, "application/json") { body := make([]byte, r.ContentLength) _, _ = r.Body.Read(body) params = string(body) } else if strings.Contains(contentType, "application/x-www-form-urlencoded") { params = r.Form.Encode() } } duration := end.Sub(start) return &AuditLog{ CreatedAt: time.Now(), Email: email, StartAt: start, EndAt: end, Duration: strconv.FormatInt(duration.Milliseconds(), 10), Url: r.URL.RequestURI(), Method: method, Parameters: params, RefererUrl: r.Header.Get("Referer"), Os: os, Ip: r.RemoteAddr, Browser: browser, } }