在小程序开发中,如果小程序涉及到文本或者图片的上传,以前没有这层限制,但是如今微信官方已经要求必须在小程序中加入内容审核,不然就审核拒绝,拒绝原因是用户上传图片可能存在违法违规问题,程序必须有审核机制。
基于以上问题,参考了微信官方API文档,其中提到了图片检测和文本检测
图片检测
文本检测
[小程序文档]:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/sec-check/security.imgSecCheck.html
今天要弄的就是图片的检测,方法如下,是基于云开发的。
首先在云开发模式下的小程序中,新建一个云函数。
一、config配置
在
{
"permissions": {
"openapi": [
"security.imgSecCheck"
]
}
}
二、云函数
const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event, context) => {
const { value } = event;
try {
const res = await cloud.openapi.security.imgSecCheck({
media: {
header: {
'Content-Type': 'application/octet-stream'},
contentType: 'image/png',
value: Buffer.from(value)
}
})
return res;
} catch (err) {
return err;
}
}
三、图片验证
ChooseImage() {
wx.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album'],
success: (res) => {
if (res.tempFiles[0] && res.tempFiles[0].size > 1024 * 1024) {
wx.showToast({
title: '图片不能大于1M',
icon: 'none'
})
return;
}
//校验图片
wx.getFileSystemManager().readFile({
filePath: res.tempFilePaths[0],
success: buffer => {
console.log(buffer.data)
wx.cloud.callFunction({
name: 'checkImg',
data: {
value: buffer.data
}
}).then(
imgRes => {
if (imgRes.result.errCode == '87014') {
wx.showToast({
title: '图片含有违法违规内容',
icon: 'none'
})
return
} else {
//图片正常,do something
}
}
)
},
fail: err => {
console.log(err)
}
})
}
});
},
以上就是微信小程序图片校验的全部过程。