开发的微信小程序,如果包含用户发表、创建内容等,将会要求进行内容的安全性检查。如用户创建投票、创建活动、发布活动、发表评论等等
TX官方有出的官方的【珊瑚安全】进行文本、图片的识别。
文本内容安全:
https://developers.weixin.qq.com/community/servicemarket/detail/00040275a14468e0e689194b251015
图片内容安全:
https://developers.weixin.qq.com/community/servicemarket/detail/00040275a14468e0e689194b251015
utils,加入安全请求的处理方法,以备使用
// ########################################################## 信息健康检查
function contentIsHealthyCheck(type,content) {
return new Promise(function(resolve, reject) {
if(type=='text'){
wx.serviceMarket.invokeService({
service: 'wxee446d7507c68b11',
api: 'msgSecCheck',
data: {
"Action": "TextApproval",
"Text": content
}
}).then(res => {
resolve(res.data)
}).catch(err => {
reject("error")
})
}else if(type=='img'){
wx.serviceMarket.invokeService({
service: 'wxee446d7507c68b11',
api: 'imgSecCheck',
data: {
"Action": "ImageModeration",
"Scenes": ["PORN","POLITICS","TERRORISM"],
"ImageUrl": content
}
}).then(res => {
resolve(res.data)
}).catch(err => {
reject("error")
})
}else{
reject("error")
}
})
}
module.exports = {
contentIsHealthyCheck:contentIsHealthyCheck
}
页面使用:
页面里我是这样做的,文本的内容,就在输入框失去焦点的事件下去检测,是否合格记录到js的data里的相关字段。
这样等最后用户最终提交发布时,必须所有待检测的字段都合格才允许发布。
<van-field value="{{voteName}}" label="活动标题" placeholder="请输入活动名称" border="{{ false }}" bind:change="onInputVoteName" bind:blur="checkTextHealthy" data-ctype='name' />
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
const app = getApp()
const util = require('../../../utils/util.js')
const qiniu = require('../../../utils/qiniuUploader.js')
Page({
data: {
// ###############################################
// 记录用户输入的各项信息是否已经完成筛查,用户发布时必须全部true才可发布
creatHealthyCheck: {
name: false,
introduceText: false,
introduceImg: false,
swiImg: false
}
},
//################################################## 敏感内容检测
// 这里文本内容,我是在用户输入时,输入框失去焦点事件下进行检测,如果合格,把这项输入的合格标记记录到data里的字段里。如果不合格进行一个提示(这里用的vant的组件进行修改提示)
checkTextHealthy: function(e) {
if (e.detail.value==''){
return
}
var that = this
var aa = util.contentIsHealthyCheck('text', e.detail.value)
aa.then(function(res) {
if (res.Response.EvilTokens.length == 0) {
if (e.currentTarget.dataset.ctype == 'name') {
that.data.creatHealthyCheck.name = true
}
} else {
Dialog.alert({
message: '请勿发表敏感内容!',
}).then(() => {
// on close
});
if (e.currentTarget.dataset.ctype == 'name') {
that.data.creatHealthyCheck.name = false
}
}
}).catch(function(res) {
Dialog.alert({
message: '敏感内容监测失败,请修改!',
}).then(() => {
// on close
});
if (e.currentTarget.dataset.ctype == 'name') {
that.data.creatHealthyCheck.name = false
}
})
},
//################################################### 上传图片,并检测
// 这里是上传到的七牛云储存
onChooseSwiImg(event) {
var that = this
for (var i in event.detail.file) {
qiniu.upload(event.detail.file[i].path, (res) => {
var file = {}
var swiImg = that.data.voteSwiImg
file.url = app.globalData.qiniu.domain + res.name
// !!! 上传图片 安全性 审核
var aa = util.contentIsHealthyCheck('img', file.url)
aa.then(function (res) {
if (res.Response.Suggestion == 'PASS') {
that.data.creatHealthyCheck.swiImg = true
swiImg.push(file)
that.setData({
voteSwiImg: swiImg
})
} else {
that.data.creatHealthyCheck.swiImg = false
Dialog.alert({
message: '请勿发表敏感或过大的图片!',
}).then(() => {
// on close
});
return
}
}).catch(function (res) {
Dialog.alert({
message: '图片审核失败,请重试!',
}).then(() => {
// on close
});
that.data.creatHealthyCheck.swiImg = false
return
})
}, (error) => {
wx.showToast({
title: '上传失败',
})
});
}
}
})