Browse Source

留言敏感词过滤

xiexiaoyuan 2 years ago
parent
commit
36bf6b69aa
3 changed files with 103 additions and 1 deletions
  1. 12 1
      services/comment/comment.go
  2. 77 0
      services/wx_app/security/security.go
  3. 14 0
      services/wx_app/wx_app.go

+ 12 - 1
services/comment/comment.go

@@ -11,6 +11,7 @@ import (
 	"hongze/hongze_yb/models/tables/wx_user"
 	"hongze/hongze_yb/models/tables/yb_comment"
 	"hongze/hongze_yb/services/user"
+	"hongze/hongze_yb/services/wx_app"
 	"hongze/hongze_yb/utils"
 	"time"
 )
@@ -78,7 +79,17 @@ func Comment(user user.UserInfo, req reqComment.ReqComment) (ret response.RespCo
 		}
 	}
 
-	//todo 敏感词过滤
+	// 敏感词过滤
+	checkResult, err := wx_app.MsgSecCheck(user.OpenID, req.Content)
+	if err != nil {
+		errMsg = err.Error()
+		err = errors.New("敏感词过滤失败")
+		return
+	}
+	if checkResult.Result.Suggest == "risky" {
+		err = errors.New(checkResult.Result.Suggest)
+		return
+	}
 	//新增留言
 	now := time.Now()
 	commentInfo := &yb_comment.YbComment{

+ 77 - 0
services/wx_app/security/security.go

@@ -0,0 +1,77 @@
+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
+}
+

+ 14 - 0
services/wx_app/wx_app.go

@@ -10,6 +10,7 @@ import (
 	"github.com/silenceper/wechat/v2/miniprogram/encryptor"
 	"github.com/silenceper/wechat/v2/miniprogram/qrcode"
 	"hongze/hongze_yb/global"
+	"hongze/hongze_yb/services/wx_app/security"
 )
 
 //微信小程序配置信息
@@ -82,3 +83,16 @@ func GetSunCode(page, scene string) (resp []byte, err error) {
 	qr := wechatClient.GetQRCode()
 	return qr.GetWXACodeUnlimit(codePars)
 }
+
+// MsgSecCheck 检查一段文本是否含有违法违规内容。
+func MsgSecCheck(openid string,content string) (result security.Result, err error) {
+	wechatClient := GetWxApp()
+	myMiniprogram := security.NewMyMiniprogram(wechatClient)
+	bodyContent := &security.BodyContent{
+		Version: 2,
+		Content: content,
+		Openid: openid,
+		Scene: 2,
+	}
+	return myMiniprogram.MsgSecCheckWithResult(bodyContent)
+}