82 lines
1.7 KiB
Go
82 lines
1.7 KiB
Go
package util
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/v2/encoding/gjson"
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
)
|
|
|
|
// MergeConsult 将 consult 附件合并到模型生成的 messages 结构中
|
|
func MergeConsult(req map[string]any, messages map[string]any, extendMapping map[string]any) map[string]any {
|
|
if len(req) == 0 || len(messages) == 0 || len(extendMapping) == 0 {
|
|
return messages
|
|
}
|
|
|
|
consult := gconv.Interfaces(req["consult"])
|
|
if len(consult) == 0 {
|
|
return messages
|
|
}
|
|
|
|
targetPath := gconv.String(extendMapping["target_content_path"])
|
|
templates := gconv.Map(extendMapping["attachment_templates"])
|
|
if targetPath == "" || len(templates) == 0 {
|
|
return messages
|
|
}
|
|
|
|
msgJson := gjson.New(messages)
|
|
|
|
// rounds 路径修正
|
|
if !msgJson.Get("rounds.0").IsNil() {
|
|
targetPath = "rounds.0." + targetPath
|
|
}
|
|
|
|
// 遍历追加
|
|
for _, item := range consult {
|
|
itemJson := gjson.New(item)
|
|
itemType := itemJson.Get("type").String()
|
|
tmpl := gconv.Map(templates[itemType])
|
|
if itemType == "" || len(tmpl) == 0 {
|
|
continue
|
|
}
|
|
|
|
attachment := buildAttachment(tmpl, itemJson.Get("url").String())
|
|
if attachment == nil {
|
|
continue
|
|
}
|
|
|
|
idx := len(msgJson.Get(targetPath).Array())
|
|
_ = msgJson.Set(fmt.Sprintf("%s.%d", targetPath, idx), attachment)
|
|
}
|
|
|
|
return msgJson.Map()
|
|
}
|
|
|
|
func buildAttachment(tmpl map[string]any, url string) map[string]any {
|
|
typ := gconv.String(tmpl["type"])
|
|
if typ == "" || url == "" {
|
|
return nil
|
|
}
|
|
|
|
body := gconv.Map(tmpl["body"])
|
|
fillEmptyInPlace(body, url)
|
|
|
|
return map[string]any{
|
|
"type": typ,
|
|
typ: body,
|
|
}
|
|
}
|
|
|
|
func fillEmptyInPlace(m map[string]any, value string) {
|
|
for k, v := range m {
|
|
switch vv := v.(type) {
|
|
case string:
|
|
if vv == "" {
|
|
m[k] = value
|
|
}
|
|
case map[string]any:
|
|
fillEmptyInPlace(vv, value)
|
|
}
|
|
}
|
|
}
|