Files
xk-hy-transit-go/internal/logweb/jsonfmt.go

63 lines
1.9 KiB
Go
Raw Normal View History

2026-05-22 09:17:39 +08:00
package logweb
import (
"encoding/json"
"strings"
)
// JSONBlock 格式化 JSON 展示块。
type JSONBlock struct {
Raw string `json:"raw"`
Formatted string `json:"formatted,omitempty"`
Valid bool `json:"valid"`
ParseError string `json:"parseError,omitempty"`
}
// FormatJSON 尝试美化 JSON失败则返回原文。
func FormatJSON(s string) JSONBlock {
s = strings.TrimSpace(s)
if s == "" {
return JSONBlock{Raw: "", Valid: true}
}
var v any
if err := json.Unmarshal([]byte(s), &v); err != nil {
return JSONBlock{Raw: s, Valid: false, ParseError: err.Error()}
}
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
return JSONBlock{Raw: s, Valid: false, ParseError: err.Error()}
}
return JSONBlock{Raw: s, Formatted: string(b), Valid: true}
}
// CallbackView 推断的回调结构(与 xkapi.CallbackItem 对齐)。
type CallbackView struct {
RecordID int `json:"record_id,omitempty"`
BizKey string `json:"biz_key"`
PushStatus string `json:"push_status"`
PushStatusLabel string `json:"push_status_label,omitempty"`
CallbackStatus string `json:"callback_status"`
CallbackStatusLabel string `json:"callback_status_label,omitempty"`
HTTPCode int `json:"http_code,omitempty"`
MsgCode int `json:"msg_code,omitempty"`
Msg string `json:"msg,omitempty"`
TraceID string `json:"trace_id,omitempty"`
ResponseBody string `json:"response_body,omitempty"`
ErrorMessage string `json:"error_message,omitempty"`
Note string `json:"note,omitempty"`
}
// InferCallbackStatus 由 push_status 推断 callback_status。
func InferCallbackStatus(pushStatus string) string {
switch pushStatus {
case "success":
return "success"
case "skipped":
return "failed"
case "failed":
return "failed"
default:
return "waiting"
}
}