解决方法如下(云开发):
config.json
{ 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; }}
js
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 { //图片正常 if (this.data.imglist.length != 0) { this.setdata({ imglist: this.data.imglist.concat(res.tempfilepaths) }) } else { this.setdata({ imglist: res.tempfilepaths }) } } } ) }, fail: err => { console.log(err) } }) } }); },
推荐教程:《微信小程序》
以上就是微信小程序调用图片安全api的详细内容。
