package system import ( "context" "net/http" "strconv" "strings" "time" "management/internal/erpserver/model/dto" ) type AuditLogRepository interface { Create(ctx context.Context, obj *AuditLog) error List(ctx context.Context, q dto.SearchDto) ([]*AuditLog, int64, error) } type AuditLog struct { ID int64 `json:"id"` CreatedAt time.Time `json:"created_at"` Email string `json:"email"` StartAt time.Time `json:"start_at"` EndAt time.Time `json:"end_at"` Duration string `json:"duration"` Url string `json:"url"` Method string `json:"method"` Parameters string `json:"parameters"` RefererUrl string `json:"referer_url"` Os string `json:"os"` Ip string `json:"ip"` Browser string `json:"browser"` Remark string `json:"remark"` } func (AuditLog) TableName() string { return "sys_audit_log" } 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, } }