biz_audit.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package mbsdsdk
  2. import (
  3. "encoding/json"
  4. "net/url"
  5. "git.sxidc.com/service-supports/fslog"
  6. )
  7. const bizAuditLogURLPrefix = "mbsd/api/inner/bizAuditLog"
  8. // Change 单字段变更
  9. type Change struct {
  10. Field string `json:"field"`
  11. Label string `json:"label"`
  12. Old string `json:"old"`
  13. New string `json:"new"`
  14. }
  15. // BizAuditEntry 业务审计日志写入条目(冗余快照,写入 SystemData 中心表)
  16. type BizAuditEntry struct {
  17. SourceService string `json:"sourceService"`
  18. Module string `json:"module"`
  19. ModuleLabel string `json:"moduleLabel"`
  20. Action string `json:"action"`
  21. ActionLabel string `json:"actionLabel"`
  22. TenantID string `json:"tenantId"`
  23. TenantName string `json:"tenantName"`
  24. OperatorID string `json:"operatorId"`
  25. OperatorName string `json:"operatorName"`
  26. OperatorRole string `json:"operatorRole"`
  27. SubjectType string `json:"subjectType"`
  28. SubjectID string `json:"subjectId"`
  29. SubjectLabel string `json:"subjectLabel"`
  30. TargetType string `json:"targetType"`
  31. TargetID string `json:"targetId"`
  32. TargetLabel string `json:"targetLabel"`
  33. Summary string `json:"summary"`
  34. Changes []Change `json:"changes,omitempty"`
  35. Source string `json:"source,omitempty"`
  36. }
  37. // WriteBizAuditAsync 异步写入业务审计日志;失败仅打日志,不影响业务事务
  38. func WriteBizAuditAsync(entry BizAuditEntry) {
  39. if entry.Summary == "" {
  40. return
  41. }
  42. go func() {
  43. if err := writeBizAudit(entry); err != nil {
  44. fslog.Warn("mbsdsdk: biz audit log write failed: " + err.Error())
  45. }
  46. }()
  47. }
  48. func writeBizAudit(entry BizAuditEntry) error {
  49. if invokeAPI == nil {
  50. return nil
  51. }
  52. body, err := json.Marshal(entry)
  53. if err != nil {
  54. return err
  55. }
  56. path, err := url.JoinPath(bizAuditLogURLPrefix)
  57. if err != nil {
  58. return err
  59. }
  60. _, err = invokeAPI.PostJSON(path, body, nil)
  61. return err
  62. }