|
@@ -2,20 +2,36 @@ package third_party
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"bytes"
|
|
"bytes"
|
|
|
- "fmt"
|
|
|
|
|
|
|
+ "strings"
|
|
|
"text/template"
|
|
"text/template"
|
|
|
"time"
|
|
"time"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+type WeMessageContent struct {
|
|
|
|
|
+ Nickname string
|
|
|
|
|
+ DateTime string
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
func TextContentParse(content string, nickname string) string {
|
|
func TextContentParse(content string, nickname string) string {
|
|
|
- t, _ := template.New("text").Parse(content)
|
|
|
|
|
|
|
+ defer func() {
|
|
|
|
|
+ if err := recover(); err != nil { //产生了panic异常
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ }()
|
|
|
|
|
+ if strings.Contains(content, "{{.}}") {
|
|
|
|
|
+ content = strings.Replace(content, "{{.}}", "", -1)
|
|
|
|
|
+ }
|
|
|
|
|
+ t, err := template.New("text").Parse(content)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return content
|
|
|
|
|
+ }
|
|
|
buf := new(bytes.Buffer)
|
|
buf := new(bytes.Buffer)
|
|
|
- err := t.Execute(buf, map[string]interface{}{
|
|
|
|
|
- "DateTime": time.Now().Format("1月2日"),
|
|
|
|
|
- "Nickname": nickname,
|
|
|
|
|
|
|
+ err = t.Execute(buf, WeMessageContent{
|
|
|
|
|
+ Nickname: nickname,
|
|
|
|
|
+ DateTime: time.Now().Format("1月2日"),
|
|
|
})
|
|
})
|
|
|
|
|
+
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- fmt.Printf("格式转换出错了,content:%s,err:%s", content, err)
|
|
|
|
|
return content
|
|
return content
|
|
|
}
|
|
}
|
|
|
return string(buf.Bytes())
|
|
return string(buf.Bytes())
|