云开发检验图片是否有违规内容

客户端

mIsImg() {
var that = this
wx.chooseImage({
count: 1,
sourceType: ['album'],
success: function(res) {
if (!res.tempFilePaths[0]) {
return;
}
console.log(JSON.stringify(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: 'imgCheck',
data: {
value: buffer.data
}
}).then(
imgRes => {
console.log(JSON.stringify(imgRes))
if (imgRes.result.errorCode == '87014') {
wx.showToast({
title: '图片含有违法违规内容',
icon: 'none'
})
return
} else {
that.$store.state.isImg = res.tempFilePaths[0]
//图片正常
}

}
)
},
fail: err => {
console.log(err)
}
})
}
})
},

 

云函数

config.json

{
  "permissions": {
    "openapi": [
      "security.imgSecCheck"
    ]
  }
}

js

const cloud = require('wx-server-sdk')
cloud.init({
  env: 'test-vc0ep' // 你的环境ID
})
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;
  }
}

 

posted @ 2019-08-09 23:45  一夜幽  阅读(929)  评论(0编辑  收藏  举报