在小程序開發中,如果小程序涉及到文本或者圖片的上傳,以前沒有這層限制,但是如今微信官方已經要求必須在小程序中加入內容審覈,不然就審覈拒絕,拒絕原因是用戶上傳圖片可能存在違法違規問題,程序必須有審覈機制。
基於以上問題,參考了微信官方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)
}
})
}
});
},
以上就是微信小程序圖片校驗的全部過程。