浏览代码

feat: add async biz audit log write client for SystemData.

Co-authored-by: Cursor <cursoragent@cursor.com>
郭铭泽 2 周之前
父节点
当前提交
60d8aa91f5
共有 2 个文件被更改,包括 72 次插入3 次删除
  1. 3 3
      README.md
  2. 69 0
      biz_audit.go

+ 3 - 3
README.md

@@ -53,14 +53,14 @@ err := mbsdsdk.Update(mbsdsdk.ConfigInfo{
 ## 版本发布(Gogs)
 
 ```bash
-git tag v0.1.1
-git push origin v0.1.1
+git tag v0.1.2
+git push origin v0.1.2
 ```
 
 业务服务:
 
 ```bash
-go get git.sxidc.com/health-checkup-system/mbsd-sdk@v0.1.1
+go get git.sxidc.com/health-checkup-system/mbsd-sdk@v0.1.2
 ```
 
 私服需配置 `GOPRIVATE=git.sxidc.com`。

+ 69 - 0
biz_audit.go

@@ -0,0 +1,69 @@
+package mbsdsdk
+
+import (
+	"encoding/json"
+	"net/url"
+
+	"git.sxidc.com/service-supports/fslog"
+)
+
+const bizAuditLogURLPrefix = "mbsd/api/inner/bizAuditLog"
+
+// Change 单字段变更
+type Change struct {
+	Field string `json:"field"`
+	Label string `json:"label"`
+	Old   string `json:"old"`
+	New   string `json:"new"`
+}
+
+// BizAuditEntry 业务审计日志写入条目(冗余快照,写入 SystemData 中心表)
+type BizAuditEntry struct {
+	SourceService string   `json:"sourceService"`
+	Module        string   `json:"module"`
+	ModuleLabel   string   `json:"moduleLabel"`
+	Action        string   `json:"action"`
+	ActionLabel   string   `json:"actionLabel"`
+	TenantID      string   `json:"tenantId"`
+	TenantName    string   `json:"tenantName"`
+	OperatorID    string   `json:"operatorId"`
+	OperatorName  string   `json:"operatorName"`
+	OperatorRole  string   `json:"operatorRole"`
+	SubjectType   string   `json:"subjectType"`
+	SubjectID     string   `json:"subjectId"`
+	SubjectLabel  string   `json:"subjectLabel"`
+	TargetType    string   `json:"targetType"`
+	TargetID      string   `json:"targetId"`
+	TargetLabel   string   `json:"targetLabel"`
+	Summary       string   `json:"summary"`
+	Changes       []Change `json:"changes,omitempty"`
+	Source        string   `json:"source,omitempty"`
+}
+
+// WriteBizAuditAsync 异步写入业务审计日志;失败仅打日志,不影响业务事务
+func WriteBizAuditAsync(entry BizAuditEntry) {
+	if entry.Summary == "" {
+		return
+	}
+	go func() {
+		if err := writeBizAudit(entry); err != nil {
+			fslog.Warn("mbsdsdk: biz audit log write failed: " + err.Error())
+		}
+	}()
+}
+
+func writeBizAudit(entry BizAuditEntry) error {
+	if invokeAPI == nil {
+		return nil
+	}
+	body, err := json.Marshal(entry)
+	if err != nil {
+		return err
+	}
+	path, err := url.JoinPath(bizAuditLogURLPrefix)
+	if err != nil {
+		return err
+	}
+	_, err = invokeAPI.PostJSON(path, body, nil)
+	return err
+}