小程序审核增加敏感词汇校验,避免违法违规而被停用服务或者投诉。
本文例子引用的百度AI开发者提供的API:
链接位置:https://console.bce.baidu.com/ai/#/ai/ocr/overview/index
创建应用后生成应用对应的id和key
应用到小程序中,,简单的写一个公用的方法:
//-----------敏感词检测
textTest (obj){
wx.request({
//获取access_token
url: ‘https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=你的应用id&client_secret=你申请的key’,
method: ‘GET’,
data: {},
success: res => {
//文本内容审核
wx.request({
url:‘https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=’+res.data.access_token,
method: ‘POST’,
header: {
‘Content-Type’: ‘application/x-www-form-urlencoded’
},
data:{
‘text’:JSON.stringify(obj) //表单内容转换json字符串提交过滤敏感词
},
success: res => {
console.log(res)
if(res.data.conclusionType==1){
//状态为1 内容合规 没有敏感词 则提交
this.accept = true;
}else{
this.modalShow({
msg: “输入内容含有敏感信息,请修改”
});
this.accept = false;
}
},
fail: () => {
console.log(Error)
},
complete: () => {}
});
},
fail: () => {
console.log(Error)
},
complete: () => {}
});
},
具体使用:
例如输入框或者文本框,在赋值之前将输入内容发送校验
//意见建议
textBlur: function (e) {
if (e.detail && e.detail.value.length > 0) {
if (e.detail.value.length < 1 || e.detail.value.length > 500) {
wx.showToast(‘内容为12-500个字符’,‘loading’,1200);
} else {
this.textTest(e.detail.value)
if(!this.accept){
return;
}
this.setData({
addMsgs: e.detail.value
});
}
} else {
this.setData({
addMsgs: ‘’
});
evaData.addMsgs = ‘’;
app.func.showToast(‘请输入投诉内容’, ‘loading’, 1200);
}
},
使用的小程序:用户满意调查小程序