forked from shenghui0779/sdk-go
/
security.go
60 lines (51 loc) · 1.43 KB
/
security.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package mp
import (
"encoding/json"
"github.com/accaolei/gochat/wx"
"github.com/tidwall/gjson"
)
// SecMediaType 检测的素材类型
type SecMediaType int
// 微信支持的素材类型
var (
SecMediaAudio SecMediaType = 1 // 音频
SecMediaImage SecMediaType = 2 // 图片
)
// ImageSecCheck 校验一张图片是否含有违法违规内容
func ImageSecCheck(filename string) wx.Action {
return wx.NewAPI(ImageSecCheckURL,
wx.WithMethod(wx.MethodUpload),
wx.WithUploadForm("media", filename, nil),
)
}
// MediaSecAsyncResult 异步校验结果
type MediaSecAsyncResult struct {
TraceID string // 任务id,用于匹配异步推送结果
}
// MediaSecCheckAsync 异步校验图片/音频是否含有违法违规内容
func MediaSecCheckAsync(dest *MediaSecAsyncResult, mediaType SecMediaType, mediaURL string) wx.Action {
return wx.NewAPI(MediaCheckAsyncURL,
wx.WithMethod(wx.MethodPost),
wx.WithBody(func() ([]byte, error) {
return json.Marshal(wx.X{
"media_type": mediaType,
"media_url": mediaURL,
})
}),
wx.WithDecode(func(resp []byte) error {
dest.TraceID = gjson.GetBytes(resp, "trace_id").String()
return nil
}),
)
}
// MsgSecCheck 检查一段文本是否含有违法违规内容
func MsgSecCheck(content string) wx.Action {
return wx.NewAPI(MsgSecCheckURL,
wx.WithMethod(wx.MethodPost),
wx.WithBody(func() ([]byte, error) {
return json.Marshal(wx.X{
"content": content,
})
}),
)
}