1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package security
- import (
- "encoding/json"
- "errors"
- "fmt"
- "github.com/silenceper/wechat/v2/miniprogram"
- "github.com/silenceper/wechat/v2/miniprogram/content"
- "github.com/silenceper/wechat/v2/util"
- )
- // 检查一段文本是否含有违法违规内容。
- // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.msgSecCheck.html
- const SecurityMsgCheckUrl = "https://api.weixin.qq.com/wxa/msg_sec_check"
- type Security struct {
- *content.Content
- }
- type MyMiniprogram struct {
- *miniprogram.MiniProgram
- }
- func NewMyMiniprogram(miniprogram *miniprogram.MiniProgram) *MyMiniprogram {
- return &MyMiniprogram{miniprogram}
- }
- type BodyContent struct {
- Version int8 `json:"version"` // 接口版本号,2.0版本为固定值2
- Openid string `json:"openid"` // 用户的openid(用户需在近两小时访问过小程序)
- Scene int8 `json:"scene"` // 场景枚举值(1 资料;2 评论;3 论坛;4 社交日志)
- Content string `json:"content"` // 需检测的文本内容,文本字数的上限为2500字,需使用UTF-8编码
- Nickname string `json:"nickname"` // 用户昵称,需使用UTF-8编码
- Title string `json:"title"` // 文本标题,需使用UTF-8编码
- Signature string `json:"signature"` // 个性签名,该参数仅在资料类场景有效(scene=1),需使用UTF-8编码
- }
- func (s *MyMiniprogram) MsgSecCheckWithResult(bodyContent *BodyContent) (result Result, err error) {
- var accessToken string
- accessToken, err = s.GetContentSecurity().GetAccessToken()
- if err != nil {
- return
- }
- uri := fmt.Sprintf("%s?access_token=%s", SecurityMsgCheckUrl, accessToken)
- response, err := util.PostJSON(uri, bodyContent)
- if err != nil {
- return
- }
- return DecodeWithResult(response, "MsgSecCheck")
- }
- type Result struct {
- Result *ResultBody
- }
- type ResultBody struct {
- Suggest string //建议,有risky、pass、review三种值
- Label int //命中标签枚举值,100 正常;10001 广告;20001 时政;20002 色情;20003 辱骂;20006 违法犯罪;20008 欺诈;20012 低俗;20013 版权;21000 其他
- }
- func DecodeWithResult(response []byte, apiName string) (result Result, err error) {
- var commError util.CommonError
- err = json.Unmarshal(response, &commError)
- if err != nil {
- return
- }
- if commError.ErrCode != 0 {
- err = errors.New(fmt.Sprintf("%s Error , errcode=%d , errmsg=%s", apiName, commError.ErrCode, commError.ErrMsg))
- return
- }
- err = json.Unmarshal(response, &result)
- if err != nil {
- return
- }
- return
- }
|