forked from shenghui0779/sdk-go
/
security.go
177 lines (148 loc) · 5.25 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package minip
import (
"encoding/json"
"io"
"os"
"path/filepath"
"github.com/liniu/gochat/urls"
"github.com/liniu/gochat/wx"
)
// SecMediaType 检测的素材类型
type SecMediaType int
// 微信支持的素材类型
const (
SecMediaAudio SecMediaType = 1 // 音频
SecMediaImage SecMediaType = 2 // 图片
)
// SecCheckScene 检测场景
type SecCheckScene int
const (
SecSceneDoc SecCheckScene = 1 // 资料
SecSceneComment SecCheckScene = 2 // 评论
SecSceneForum SecCheckScene = 3 // 论坛
SecSceneLog SecCheckScene = 4 // 社交日志
)
// SecCheckSuggest 建议
type SecCheckSuggest string
const (
SecSuggestRisky SecCheckSuggest = "risky"
SecSuggestPass SecCheckSuggest = "pass"
SecSuggestReview SecCheckSuggest = "review"
)
// ImageSecCheck 校验一张图片是否含有违法违规内容
func ImageSecCheck(imgPath string) wx.Action {
_, filename := filepath.Split(imgPath)
return wx.NewPostAction(urls.MinipImageSecCheck,
wx.WithUpload(func() (wx.UploadForm, error) {
path, err := filepath.Abs(filepath.Clean(imgPath))
if err != nil {
return nil, err
}
return wx.NewUploadForm(
wx.WithFormFile("media", filename, func(w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if _, err = io.Copy(w, f); err != nil {
return err
}
return nil
}),
), nil
}),
)
}
type ParamsMediaCheckAsync struct {
MediaType SecMediaType `json:"media_type"`
MediaURL string `json:"media_url"`
Version int `json:"version"` // 接口版本号,2.0版本为固定值2
Scene SecCheckScene `json:"scene"` // 场景枚举值
OpenID string `json:"openid"` // 用户的openid(用户需在近两小时访问过小程序)
}
// ResultMediaCheckAsync 异步校验结果
type ResultMediaCheckAsync struct {
TraceID string `json:"trace_id"` // 任务id,用于匹配异步推送结果
}
// MediaCheckAsync 异步校验图片/音频是否含有违法违规内容
func MediaCheckAsync(params *ParamsMediaCheckAsync, result *ResultMediaCheckAsync) wx.Action {
return wx.NewPostAction(urls.MinipMediaCheckAsync,
wx.WithBody(func() ([]byte, error) {
return json.Marshal(params)
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
type ParamsMsgCheck struct {
Content string `json:"content"` // 需检测的文本内容,文本字数的上限为2500字,需使用UTF-8编码
Version int `json:"version"` // 接口版本号,2.0版本为固定值2
Scene SecCheckScene `json:"scene"` // 场景枚举值
OpenID string `json:"openid"` // 用户的openid(用户需在近两小时访问过小程序)
Title string `json:"title,omitempty"` // 文本标题,需使用UTF-8编码
Nickname string `json:"nickname,omitempty"` // 用户昵称,需使用UTF-8编码
Signature string `json:"signature,omitempty"` // 个性签名,该参数仅在资料类场景有效(scene=1),需使用UTF-8编码
}
type ResultMsgCheck struct {
TraceID string `json:"trace_id"`
Result *MsgCheckRet `json:"result"`
Detail []*MsgCheckItem `json:"detail"`
}
type MsgCheckRet struct {
Suggest string `json:"suggest"`
Label int `json:"label"`
}
type MsgCheckItem struct {
Strategy string `json:"strategy"`
ErrCode int `json:"errcode"`
Suggest string `json:"suggest"`
Label int `json:"label"`
Keyword string `json:"keyword"`
Prob int `json:"prob"`
}
// MsgSecCheck 检查一段文本是否含有违法违规内容
func MsgSecCheck(params *ParamsMsgCheck, result *ResultMsgCheck) wx.Action {
return wx.NewPostAction(urls.MinipMsgSecCheck,
wx.WithBody(func() ([]byte, error) {
return json.Marshal(params)
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}
// RiskScene 风控场景
type RiskScene int
// 微信支持的风控场景值
const (
RiskRegister RiskScene = 0 // 注册
RiskCheat RiskScene = 1 // 营销作弊
)
// ParamsUserRisk 用户风控参数
type ParamsUserRisk struct {
AppID string `json:"appid"` // 小程序appid
OpenID string `json:"openid"` // 用户的openid
Scene RiskScene `json:"scene"` // 场景值,0:注册,1:营销作弊
MobileNO string `json:"mobile_no,omitempty"` // 用户手机号
ClientIP string `json:"client_ip"` // 用户访问源ip
EmailAddress string `json:"email_address,omitempty"` // 用户邮箱地址
ExtendedInfo string `json:"extended_info,omitempty"` // 额外补充信息
IsTest bool `json:"is_test,omitempty"` // false:正式调用,true:测试调用
}
// ResultUserRisk 用户风控结果
type ResultUserRisk struct {
RiskRank int `json:"risk_rank"`
}
// GetUserRiskRank 安全风控 - 获取用户的安全等级(无需用户授权)
func GetUserRiskRank(params *ParamsUserRisk, result *ResultUserRisk) wx.Action {
return wx.NewPostAction(urls.MinipUserRiskRank,
wx.WithBody(func() ([]byte, error) {
return wx.MarshalNoEscapeHTML(params)
}),
wx.WithDecode(func(b []byte) error {
return json.Unmarshal(b, result)
}),
)
}