微信内容安全过滤
最近有需求做文本安全过滤,服务平台的暂时没有可以用的了,综合考虑了一下,还是决定使用微信服务端的内容安全API(毕竟免费)微信文本安全文档
这里未使用云开发,就直接使用https调用,这里封装成一个方法
//调用时传入要检测的文本text
Vue.prototype.wxaMsgSecCheck = function (text) {
return new Promise((resolve, reject) => {
uni.request({
url:'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=小程序密钥',
success: (res) => {
console.log('testtoken',res.data.access_token)
uni.request({
url:'https://api.weixin.qq.com/wxa/msg_sec_check?access_token='+res.data.access_token,
method:'POST',
data:{
version:2,
openid:uni.getStorageSync('myInfo').openId//用户的openid,
scene:2,
content:text
},
success: (res2) => {
if(res2.data.result.suggest!='pass'){
let keywordsArr=res2.data.detail.filter(value=>(value.suggest!='pass'&&value.keyword))
keywordsArr=Array.from(new Set(keywordsArr))
let keywords=[]
console.log('keywordsArr',keywordsArr)
keywordsArr.map(value=>{
if(value.suggest!='pass'){
keywords.push(value.keyword)
}
})
//返回敏感词组成的数组
resolve(Array.from(new Set(keywords)))
}else{
resolve(1)
}
}
})
}
})
})
}
//使用
this.wxaMsgSecCheck(text).then(res=>{
console.log('文本检测',res)
})
另外,因为微信阻止前端直接调用微信api,实际使用的时候需要后端封装一下。这里还有个缺点就是需要自己添加关键词,否则检测结果里哪个关键词有问题,具体配置如下
支持手动添加和文件(csv)导入